Mapreduce读取Hbase表,写数据到多个Hbase表中

时间:2022-03-26 23:36:52

Job端的变化:

通过设置conf,配置输出表,在reduce中获取输出表名字

Configuration conf = job.getConfiguration();
//输出表1
conf.set("usertag_output", "usertag");
//输出表2
conf.set("prodtag_output", "prodtag");
     job.setReducerClass(LabelReducer.class);
job.setOutputValueClass(Put.class);
job.setOutputFormatClass(MultiTableOutputFormat.class);

 

LabelReducer:

@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException
{

.......
Put put = new Put(userid.getBytes());
put.add("usertags".getBytes(), "usertags".getBytes(), outputlabel.toString().getBytes());

  

 ............

ImmutableBytesWritable ib = new ImmutableBytesWritable();
ib.set(Bytes.toBytes(usertag_output)); // 将结果存入hbase表
context.write(ib, put);

  

}