hbase batch批处理

hbase的put(List<Put> puts),delete(List<Delete> deletes),get(List<Get> gets)都是基于batch()实现的。

    //批处理数据,测试数据demoTable
    //注意:同一个rowKey不能同时使用put和delete
    private static void batchData() throws IOException{
        Table table = helper.getConnection().getTable(TableName.valueOf(tableNameString));

        byte[] row1 = Bytes.toBytes("row1");
        byte[] row2 = Bytes.toBytes("row2");
        byte[] cf1 = Bytes.toBytes("cf1");
        byte[] cf2 = Bytes.toBytes("cf2");
        byte[] qualifier1 = Bytes.toBytes("qual1");
        byte[] qualifier2 = Bytes.toBytes("qual2");

        List<Row> list = new ArrayList<>();



        Put put = new Put(row1);
        put.addColumn(cf1,qualifier1,5,Bytes.toBytes("row1_batch1"));
        put.addColumn(cf2,qualifier2,5,Bytes.toBytes("row1_batch2"));
        list.add(put);

        Get get = new Get(row1);
        get.addColumn(cf1,qualifier1);
        get.addColumn(cf2,qualifier2);
        list.add(get);

        Delete delete = new Delete(row2);
        delete.addColumns(cf1,qualifier2);
        list.add(delete);

        get = new Get(row2);
        get.addFamily(Bytes.toBytes("noexists")); //列族不存在,这里将抛出异常
        list.add(get);

        Object[] results = new Object[list.size()];

        try {
            table.batch(list,results);
        }catch (Exception e){
            e.printStackTrace();
        }

        for(int i=0;i<results.length;i++){
            System.out.println("result["+i+"]: type = "+results[i].getClass().getSimpleName()+results[i]);
        }

        table.close();
        helper.dump(tableNameString);
        helper.close();
    }
result[0]: type = Resultkeyvalues=NONE //put不返回值
result[1]: type = Resultkeyvalues={row1/cf1:qual1/1/Put/vlen=4/seqid=0, row1/cf2:qual2/2/Put/vlen=4/seqid=0}
result[2]: type = Resultkeyvalues=NONE //delete不返回值
result[3]: type = NoSuchColumnFamilyExceptionorg.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException

 

posted @ 2019-04-07 22:19  我是属车的  阅读(2272)  评论(0编辑  收藏  举报