如何使用MapReduce查询HBase数据?

时间:2014-01-27 11:17:47

标签: mapreduce hbase

您好我是MapReduce和HBase的新手。请指导。我正在使用MapReduce将表格数据移动到HBase。现在HBase中已达到数据(因此在HDFS中)。我创建了mapreduce作业,它将从文件中读取表格数据并使用HBase API将其放入Hbase。

现在我怀疑是否可以使用MapReduce查询HBase数据?我不想执行HBase命令来查询数据。是否可以使用MapReduce查询HBase的数据?

请帮助或建议。

1 个答案:

答案 0 :(得分:3)

当然可以,HBase附带TableMapReduceUtil来帮助您配置MapReduce作业以扫描数据。它将自动为每个区域创建一个地图任务。

请查看此示例 extracted from the HBase book

Configuration config = HBaseConfiguration.create();
Job job = new Job(config, "ExampleRead");
job.setJarByClass(MyReadJob.class);     // class that contains mapper

Scan scan = new Scan();
scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobs
scan.setCacheBlocks(false);  // don't set to true for MR jobs
// set other scan attrs
...

TableMapReduceUtil.initTableMapperJob(
  tableName,        // input HBase table name
  scan,             // Scan instance to control CF and attribute selection
  MyMapper.class,   // mapper
  null,             // mapper output key
  null,             // mapper output value
  job);
job.setOutputFormatClass(NullOutputFormat.class);   // because we aren't emitting anything from mapper

boolean b = job.waitForCompletion(true);
if (!b) {
  throw new IOException("error with job!");
}

<强> MORE EXAMPLES HERE

相关问题