MapReduce同时写入HBase HDFS
1,job设置:
job.setOutputFormatClass(org.apache.hadoop.hbase.mapreduce.TableOutputFormat.class);
job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, ${hbase表名});
多输出设置
MultipleOutputs.addNamedOutput(job, "hdfs", TextOutputFormat.class, WritableComparable.class, Writable.class);
2,reduce
private MultipleOutputs mos;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
mos = new MultipleOutputs(context);
}
@Override
public void reduce(Text userID, Iterable<Text> itemIDs, Context context) throws IOException, InterruptedException {
Put put = new Put(${rowKey}.getBytes());
//存储key值下不同日期的value值 @param(row, qualifier, value)
put.add(new KeyValue(${rowKey}.getBytes(), "date".getBytes(), ${日期}.getBytes(), ${value}.getBytes()));
context.write(new ImmutableBytesWritable(${rowKey}.getBytes()), put);
//写入HDFS
mos.write("hdfs", ${key}, ${value}, ${hdfspath});
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
mos.close();
}