MapReduce中实现对HBase中表的操作二

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过程。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值