使用mapreduce复制hbase表

准备工作如下:

一.两张hbase表——fruit(有数据)和fruitresult(无数据),我们将使用mapreduce将hbase中的frult以可自定义的方式复制到fruitresult中

二.创建一个hbase工程,导入对应jar包 

代码如下:

map端:

public class FruitMapper extends TableMapper<ImmutableBytesWritable, Put> {
	@Override
	protected void map(
			ImmutableBytesWritable key,
			Result value,
			Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context context)
			throws IOException, InterruptedException {
		Put put = new Put(key.get());
		Cell[] cells = value.rawCells();
		for (Cell cell : cells) {
			if ("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
				put.add(cell);
			} else if ("color".equals(Bytes.toString(CellUtil
					.cloneQualifier(cell)))) {
				put.add(cell);
			}
		}
		context.write(key, put);
	}
}

reduce端如下:

public class FruitReducer extends
		TableReducer<ImmutableBytesWritable, Put, NullWritable> {
	@Override
	protected void reduce(
			ImmutableBytesWritable key,
			Iterable<Put> values,
			Reducer<ImmutableBytesWritable, Put, NullWritable, Mutation>.Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		for (Put value : values) {
			context.write(NullWritable.get(), value);
		}
	}
}

 drive端:

public class FruitDriver extends Configuration implements Tool {
	Configuration configuration = null;

	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		int tool = ToolRunner.run(conf, new FruitDriver(), args);
	}

	@Override
	public void setConf(Configuration conf) {
		// TODO Auto-generated method stub
		this.configuration = conf;
	}

	@Override
	public Configuration getConf() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int run(String[] args) throws Exception {
		Job job = Job.getInstance(configuration);
		// job.setMapperClass(FruitMapper.class);
		// job.setReducerClass(FruitReducer.class);
		job.setJarByClass(FruitDriver.class);
		Scan scan = new Scan();
		TableMapReduceUtil.initTableMapperJob("fruit", scan, FruitMapper.class,
				ImmutableBytesWritable.class, Put.class, job);
		TableMapReduceUtil.initTableReducerJob("fruitresult",
				FruitReducer.class, job);
		System.out.println();
		return job.waitForCompletion(true) ? 0 : 1;
	}
}

运行结果如下: 

除了hbase表之间的复制,还可以使用mapreduce从hdfs本地复制内容到hbase表中,参考:使用MapReduce将Hadoop HDFS中的文件导入HBase 

猜你喜欢

转载自blog.csdn.net/qq_39327985/article/details/84495261