【发布时间】:2012-02-28 06:17:41
【问题描述】:
我有一个简单的 map/reduce 作业,它扫描一个 hbase 表,并修改另一个 hbase 表。 hadoop 作业似乎成功完成,但是当我检查 hbase 表时,该条目没有出现在那里。
这里是hadoop程序:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class HBaseInsertTest extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
String table = "duplicates";
Scan scan = new Scan();
scan.setCaching(500);
scan.setCacheBlocks(false);
Job job = new Job(getConf(), "HBaseInsertTest");
job.setJarByClass(HBaseInsertTest.class);
TableMapReduceUtil.initTableMapperJob(table, scan, Mapper.class, /* mapper output key = */null,
/* mapper output value= */null, job);
TableMapReduceUtil.initTableReducerJob("tablecopy", /*output table=*/null, /*reducer class=*/job);
job.setNumReduceTasks(0);
// Note that these are the default.
job.setOutputFormatClass(NullOutputFormat.class);
return job.waitForCompletion(true) ? 0 : 1;
}
private static class Mapper extends TableMapper<ImmutableBytesWritable, Put> {
@Override
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
}
@Override
public void map(ImmutableBytesWritable row, Result columns, Context context) throws IOException {
long id = 1260018L;
try {
Put put = new Put(Bytes.toBytes(id));
put.add(Bytes.toBytes("mapping"), Bytes.toBytes("foo"), Bytes.toBytes("bar"));
context.write(row, put);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
int res = ToolRunner.run(config, new HBaseInsertTest(), args);
System.exit(res);
}
}
来自 HBase 外壳:
hbase(main):008:0> get 'tablecopy', '1260018', 'mapping'
COLUMN CELL
0 row(s) in 0.0100 seconds
我已经对程序进行了很多简化以尝试演示/隔离问题。我对 hadoop/hbase 也比较陌生。我确实验证了映射是 tablecopy 表中存在的列族。
【问题讨论】:
-
可能没有输出?尝试在
context.write之前打印出row和put -
有输出。切换到字符串键可以解决问题。