1.从hbase中读取数据
2. 写map\reduce过程
3. 输出数据到hdfs中
首先要了解我们需要用TableMapper.class读取hbase中的数据到map\reduce任务中:
注意TableMapper的输出key、value是Writeable,输入key、value是固定的!而且我们必须在程序中指定map的输出key、value类型。
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
通过构建连接hbase的htable来配置job:
Configuration conf = new Configuration();
HTable table = new HTable(conf,tablename);
Job job = new Job(table.getConfiguration(),"ReadDataFromHBase");
通过配置scan来控制需要获取hbase的table中的哪些数据:
Scan scan = createHBaseScan();
if(null==scan) {
System.out.println("error : scan = null");
System.exit(1);
}
TableMapReduceUtil.initTableMapperJob(tablename, scan, Map.class,
ImmutableBytesWritable.class, Put.class, job);
在map函数中,通过分析result来取得结果。
@Override
public void map(ImmutableBytesWritable key, Result value, Context context) throws IOException,InterruptedException{
String row = Bytes.toString(value.getRow());
String val = Bytes.toString(value.getValue(Bytes.toBytes("content"), Bytes.toBytes("count")));
keyout.set(row);
valueout.set(val);
context.write(keyout, valueout);
}
reduce即是正常的reduce过程。