简体   繁体   English

不带减速器的HBase读/写,异常

[英]Read/Write Hbase without reducer, exceptions

I want to read and write hbase without using any reducer. 我想读写hbase而不使用任何reducer。

I followed the example in "The Apache HBase™ Reference Guide", but there are exceptions. 我按照“ Apache HBase™参考指南”中的示例进行操作,但是有一些例外。

Here is my code: 这是我的代码:

public class CreateHbaseIndex { 
static final String SRCTABLENAME="sourceTable";
static final String SRCCOLFAMILY="info";
static final String SRCCOL1="name";
static final String SRCCOL2="email";
static final String SRCCOL3="power";

static final String DSTTABLENAME="dstTable";
static final String DSTCOLNAME="index";
static final String DSTCOL1="key";
public static void main(String[] args) {
    System.out.println("CreateHbaseIndex Program starts!...");
    try {
        Configuration config = HBaseConfiguration.create();
        Scan scan = new Scan();
        scan.setCaching(500);
        scan.setCacheBlocks(false);
        scan.addColumn(Bytes.toBytes(SRCCOLFAMILY), Bytes.toBytes(SRCCOL1));//info:name
        HBaseAdmin admin = new HBaseAdmin(config);
        if (admin.tableExists(DSTTABLENAME)) {
            System.out.println("table Exists.");
        }
        else{
            HTableDescriptor tableDesc = new HTableDescriptor(DSTTABLENAME);
            tableDesc.addFamily(new HColumnDescriptor(DSTCOLNAME));
            admin.createTable(tableDesc);
            System.out.println("create table ok.");
        }
        Job job = new Job(config, "CreateHbaseIndex");
        job.setJarByClass(CreateHbaseIndex.class);
        TableMapReduceUtil.initTableMapperJob(
                SRCTABLENAME, // input HBase table name
                scan, // Scan instance to control CF and attribute selection
                HbaseMapper.class, // mapper
                ImmutableBytesWritable.class, // mapper output key
                Put.class, // mapper output value
                job);
        job.waitForCompletion(true);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("Program ends!...");
}

public static class HbaseMapper extends TableMapper<ImmutableBytesWritable, Put> {
    private HTable dstHt;
    private Configuration dstConfig;
    @Override
    public void setup(Context context) throws IOException{
        dstConfig=HBaseConfiguration.create();
        dstHt = new HTable(dstConfig,SRCTABLENAME);
    }

    @Override
    public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
        // this is just copying the data from the source table...
        context.write(row, resultToPut(row,value));
    }

    private static Put resultToPut(ImmutableBytesWritable key, Result result) throws IOException {
        Put put = new Put(key.get());
        for (KeyValue kv : result.raw()) {
            put.add(kv);
        }
        return put;
    }

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

By the way, "souceTable" is like this: 顺便说一句,“ souceTable”是这样的:

key  name    email
1    peter   a@a.com
2    sam     b@b.com

"dstTable" will be like this: “ dstTable”将如下所示:

key    value
peter  1
sam    2

I am a newbie in this field and need you help. 我是该领域的新手,需要您的帮助。 Thx~ 谢谢

You are correct that you don't need a reducer to write to HBase, but there are some instances where a reducer might help. 您是正确的,不需要减速器写入HBase,但是在某些情况下,减速器可能会有所帮助。 If you are creating an index, you might run into situations where two mappers are trying to write the same row. 如果创建索引,则可能会遇到两个映射器试图写入同一行的情况。 Unless you are careful to ensure that they are writing into different column qualifiers, you could overwrite one update with another due to race conditions. 除非您谨慎确保它们写入不同的列限定符,否则由于竞争条件,您可能会用另一个覆盖新的覆盖。 While HBase does do row level locking, it won't help if your application logic is faulty. 虽然HBase确实进行行级锁定,但是如果您的应用程序逻辑错误,它将无济于事。

Without seeing your exceptions, I would guess that you are failing because you are trying to write key-value pairs from your source table into your index table, where the column family doesn't exist. 如果没有看到异常,我会猜测您失败了,因为您试图将键值对从源表写入索引表,而该索引表不存在列族。

In this code you are not specifying the output format. 在此代码中,您未指定输出格式。 You need to add the following code 您需要添加以下代码

    job.setOutputFormatClass(TableOutputFormat.class);

    job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE,
            DSTTABLENAME);

Also, we are not supposed to create new configuration in the set up, we need to use the same configuration from context. 另外,我们不应该在设置中创建新配置,我们需要从上下文中使用相同的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM