【问题标题】:Write output to multiple tables from REDUCER从 REDUCER 将输出写入多个表
【发布时间】:2016-09-22 23:54:52
【问题描述】:

我可以从我的 reducer 将输出写入 HBase 中的多个表吗?我浏览了不同的博客文章,但即使使用MultiTableOutputFormat,也找不到方法。

我提到了这个:Write to multiple tables in HBASE

但无法找出 context.write 调用的 API 签名。

减速器代码:

public class MyReducer extends TableReducer<Text, Result, Put> {

    private static final Logger logger = Logger.getLogger( MyReducer.class );

    @SuppressWarnings( "deprecation" )
    @Override
    protected void reduce( Text key, Iterable<Result> data, Context context ) throws IOException, InterruptedException {
        logger.info( "Working on ---> " + key.toString() );
        for ( Result res : data ) {
            Put put = new Put( res.getRow() );
            KeyValue[] raw = res.raw();
            for ( KeyValue kv : raw ) {
                put.add( kv );
            }

            context.write( obj, put );
            **// I dont know how to give table name here.**

        }
    }
}

【问题讨论】:

    标签: hadoop mapreduce hbase


    【解决方案1】:

    要识别表名,您应该将表名作为键传递给context.write(key, put) 方法:

    ImmutableBytesWritable key = new ImmutableBytesWritable(Bytes.toBytes("tableName"));
    context.write(key, put);
    

    但是如果您想一次通过 MapReduce 作业加载大量数据,那么使用MultiTableHFileOutputFormat 可能会很有趣。这种输出格式为您需要的每个 HBase 表创建 HFiles,然后您可以使用 LoadIncrementalHFiles 工具轻松加载这些文件:

    hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /tmp/multiTableJobResult hbaseTable
    

    您可以在文章中阅读更多关于MultiTableHFileOutputFormat的信息:http://tech.adroll.com/blog/data/2014/07/15/multi-table-bulk-import.html

    【讨论】:

    • 是的,这是正确的(+1)我们必须通过上述方式将表作为键传递。如果您是通过映射器编写,那么下面提到的是方法。 // rowKey 是从 lineBytes 生成的 hbase rowKey Put put = new Put(rowKey); // 创建你的 KeyValue 对象 put.add(kv); context.write("动作", put); // 写入动作表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2016-03-04
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多