Thursday, 2 May 2013

HBase: Batch API Example


public class HBaseBatch {
    public static void main(String[] args) throws IOException
    {
        Configuration conf = HBaseConfiguration.create();
        HTable table = new HTable(conf,"testtable");
        
        List batch=new ArrayList();
        System.out.println(batch.size());
        
        Put put = new Put(Bytes.toBytes("row-3"));
        put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("quan3"),Bytes.toBytes("val3"));
        batch.add(put);
        
        Get get1 = new Get(Bytes.toBytes("row-1"));
        get1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("quan1"));
        batch.add(get1);
        
        Delete delete = new Delete(Bytes.toBytes("row-3"));
        delete.deleteColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("quan3"));
        batch.add(delete);

        Object[] results = new Object[batch.size()];
        try
        {
            table.batch(batch, results);
                    }
        catch (Exception e)
        {
            e.printStackTrace();
                    }
        
        for (int i = 0; i < results.length; i++) {
            System.out.println("Result[" + i + "]: " + results[i]);
            }
    }
    
}

No comments:

Post a Comment