20,809
社区成员
发帖
与我相关
我的任务
分享
public class TxtHbase {
public static class MapperClass extends TableMapper<Text, IntWritable> {
private final IntWritable ONE = new IntWritable(1);
private Text text = new Text();
@Override
public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
String val = new String(value.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("c1")));
text.set(val); // we can only emit Writables...
context.write(text, ONE);
}
}
public static class ReducerClass extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int i = 0;
for (IntWritable val : values) {
i += val.get();
}
Put put = new Put(Bytes.toBytes(key.toString()));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("count"), Bytes.toBytes(i));
context.write(null, put);
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
//String [] argArray=new GenericOptionsParser(conf, args).getRemainingArgs();
conf.set("from.table", "trade_detail_info");
conf.set("to.table", "trade_total");
//conf.set("family", "family1");
Job job = new Job(conf, "hbase to hbase");
job.setJarByClass(TxtHbase.class);
TableMapReduceUtil.initTableMapperJob(conf.get("from.table"), new Scan(), MapperClass.class, Text.class, IntWritable.class, job);
TableMapReduceUtil.initTableReducerJob(conf.get("to.table"), ReducerClass.class, job);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}