hbase 到 hbase 练习

 最近在学hbase 编码,想从hbase 到hbase 怎么编码。因此写了一个小的demo。

本练习是从wc 表到wc_out表。

个人理解,TableMapReduceUtil.initTableMapperJob应该是关联输入表和mapper (继承TableMapper)的

initTableReducerJob 是关联输出表和reducer(继承TableReducer)的。

public class Hbase2HbaseRunner{
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://localhost:8020");
		conf.set("hbase.zookeeper.quorum", "localhost");
	   
		Job  job = Job.getInstance(conf);
		
		
	   job.setJarByClass(Hbase2HbaseRunner.class);
   	job.setMapOutputKeyClass(Text.class);   	
   	job.setMapOutputValueClass(IntWritable.class);
	   		
	   Scan scan = new Scan();
//	   job.setReducerClass(Hbase2HdfsReducer.class);
	   
	   TableMapReduceUtil.initTableMapperJob("wc",
			   scan, 
			   Hbase2HbaseMapper.class,
			   Text.class, 
			   IntWritable.class, 
			   job);
	   
	   TableMapReduceUtil.initTableReducerJob("wc_out", 
			   Hbase2HbaseRecucer.class, job);
	   
	  
	  job.waitForCompletion(true);
	}
}

  • mapper 需要集成tablemapper表。

读取表中的数据。

public class Hbase2HbaseMapper 
extends TableMapper<Text, IntWritable>{

	@Override
	protected void map(ImmutableBytesWritable key, Result value,
			Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		
		NavigableMap<byte[], byte[]> map = value.getFamilyMap("cf".getBytes());
		for(Entry<byte[], byte[]> entry:map.entrySet()) {
			int val = Integer.parseInt(new String(entry.getValue()));
			String keyV = new String(entry.getKey());
			System.out.print("key ="+keyV +":::  val ="+val);
			context.write(new Text(new String(value.getRow())), 
					new IntWritable(val));
		}
	}
	
	

}
  • reducer 需要继承TableReducer。

将数据写入wc_out表。

public class Hbase2HbaseRecucer 
extends TableReducer<Text, IntWritable,
ImmutableBytesWritable>{

	@Override
	protected void reduce(Text key, Iterable<IntWritable> iterable,
			Reducer<Text, IntWritable, 
			ImmutableBytesWritable, Mutation>.Context context)
			throws IOException, InterruptedException {
		
		int sum=0;
		for(IntWritable it:iterable) {
			sum+=it.get();
		}
		// write into hbase
		Put put = new Put(key.toString().getBytes());
		put.add("cf".getBytes(),"count".getBytes(),(sum+"").getBytes());
		
		context.write(null, put);
		
		
	}

猜你喜欢

转载自blog.csdn.net/qq_28059559/article/details/86233943
今日推荐