【发布时间】:2013-09-27 21:04:21
【问题描述】:
我在 hbase 中有以下格式的日志数据。
hbase 源表
---------------------
date(table key) word count
---------------------
2013/09/25 apple 5
2013/09/25 mangoes 2
2013/09/25 oranges 6
2013/09/25 apple 2
2013/09/25 mangoes 3
2013/09/25 mangoes 1
dest table(2013/09/25运行mapreduce后,目标表中word作为key,count和为column.data)
------------------
word(table key) count
------------------
apple 7
oranges 6
mangoes 6
数据将每天添加到源表中。但我不想对所有源表数据进行 map reduce。所以我尝试只为当天添加的数据做 map reduce。
在 2013 年 9 月 26 日添加了新数据的源表。
---------------------
date(table key) word count
---------------------
2013/09/25 apple 5
2013/09/25 mangoes 2
2013/09/25 oranges 6
2013/09/25 apple 2
2013/09/25 mangoes 3
2013/09/25 mangoes 1
2013/09/26 apple 10
2013/09/26 oranges 20
当我只为 2013/09/26 数据做 mapreduce 时。我在 dest 表中得到以下内容。
包含新数据的目标表(由于键相同,Apple 和 Oranges 的计数已更新为 2013/09/26 数据。截至 2013/09/25 的旧数据已消失):
------------------
word(table key) count
------------------
apple 10
oranges 10
mangoes 6
预期的目标表:
------------------
word(table key) count
------------------
apple 17
oranges 16
mangoes 6
我可以映射reduce部分数据并将计数添加到dest表计数列还是每次都需要映射reduce所有数据?
如果我可以映射减少部分数据并更新计数,我该怎么做。这是我的映射减少功能。
地图功能:
public void map(ImmutableBytesWritable row,Result value,Context context) throws IOException {
ImmutableBytesWritable key = new ImmutableBytesWritable(row.get());
String cf = "data";
String column1 = "word";
String column2 = "count";
String word = new String(result.getValue(Bytes.toBytes(cf),Bytes.toBytes(column1)));
Text t = new Text(word);
context.write(t,value);
}
减少功能:
public void reduce(Text key,Iterable<Result> values,Context context) throws IOException,InterruptedException {
int count=0;
String cf = "data";
String column = "count";
for(Result val :values) {
int d = Integer.parseInt(new String(result.getValue(Bytes.toBytes(cf),Bytes.toBytes(column))))
count += d;
}
Put put = new Put(Bytes.toBytes(key.toString()));
put.add(cf.getBytes(), column.getBytes(), String.valueOf(count).getBytes());
context.write(null, put);
}
【问题讨论】:
标签: java hadoop mapreduce hbase