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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 02:28   169   0

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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP