使用MapReduce并行查询HBase的行键列表

时间:2016-05-30 07:00:28

标签: hadoop mapreduce hbase

我想在HBase中执行查询操作,以使用提供的行键列表来获取记录。由于MapReduce中的Mappers并行工作,所以我想使用它。

输入行键列表将在~100000范围内,我为映射器创建了一个customInputFormat,它将为每个映射器提供1000个行键列表,用于查询HBase表。这些查询的记录可能存在于HBase表中,也可能不存在,我只想返回那些存在的记录。

我见过各种examples,我发现执行hbase table scan操作以获取rowkeys的范围,范围由startingRowKeyendingRowKey指定,但我想查询提供的行键列表。

如何使用MapReduce执行此操作? 欢迎任何帮助!

2 个答案:

答案 0 :(得分:2)

您可以在mapper中使用这种方法,这对我很有用 它将返回Result的数组。

/**
     * Method getDetailRecords.
     * 
     * @param listOfRowKeys List<String>
     * @return Result[]
     * @throws IOException
     */
    private Result[] getDetailRecords(final List<String> listOfRowKeys) throws IOException {
        final HTableInterface table = HBaseConnection.getHTable(TBL_DETAIL);
        final List<Get> listOFGets = new ArrayList<Get>();
        Result[] results = null;
        try {
            for (final String rowkey : listOfRowKeys) {// prepare batch of get with row keys
   // System.err.println("get 'yourtablename', '" + saltIndexPrefix + rowkey + "'");
                final Get get = new Get(Bytes.toBytes(saltedRowKey(rowkey)));
                get.addColumn(COLUMN_FAMILY, Bytes.toBytes(yourcolumnname));
                listOFGets.add(get);
            }
            results = table.get(listOFGets);

        } finally {
            table.close();
        }
        return results;
    }

答案 1 :(得分:1)

当您将行键列表传递给映射器时,您应该向HBase发出get个请求。每个get返回所请求密钥的数据,如果密钥不存在则返回任何内容。

首先,您应该在映射器的setup()方法中创建Table实例:

private Table table;

@Override
protected void setup(Context context) throws IOException, InterruptedException {
    Configuration hbaseConfig = HBaseConfiguration.create();
    Connection conn = ConnectionFactory.createConnection(hbaseConfig);
    this.table = conn.getTable(TableName.valueOf("hbaseTable"));
}

然后,您可以在GetResult个实例的帮助下,根据每个密钥get方法向HBase表发出map()个请求:

String key = "keyString";
Get getValue = new Get(key.getBytes());

//add column family and column qualifier if you desire
getValue.addColumn("columnFamily".getBytes(), "columnQual".getBytes());

try {
    Result result = table.get(getValue);
    if (!table.exists(getValue)) {

        //requested key doesn't exist
        return;
    }

    // do what you want with result instance 
}

完成mapper的工作后,您需要使用cleanup()方法关闭与该表的连接;

@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
    table.close();
}

此外,您可以将get请求的结果传递给reducer,或使用cleanup()方法将它们组合起来。这取决于您的目的。