HBase的常用原子操作Java代码:
Append:追加,类似于put操作。
Configurationconf =HBaseConfiguration.create();
conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);
conf.set(“hbase.zookeeper.property.clientport”,”2181”);
HConnection conn=HConnectionManager.createConnection(conf);
HTableInterfacetable = conn.getTable(“创建的表名”);
Append append = newAppend(Bytes.toBytes(“r1”));
append.add(Bytes.toBytes(“d”), Bytes.toBytes(“a”),Bytes.toBytes(“abcd”));
table.append(append);
table.close();
conn.close();
checkAndPut:检查是否存在参数描述的记录,如果存在,则不写入数据,如果不存在就写入数据。注意:是对某一个row_key的原子操作,不能跨row_key。
Configurationconf =HBaseConfiguration.create();
conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);
conf.set(“hbase.zookeeper.property.clientport”,”2181”);
HConnection conn=HConnectionManager.createConnection(conf);
HTableInterfacetable = conn.getTable(“创建的表名”);
Put put = new Put(Bytes.toBytes(“r1”));
put.add(Bytes.toBytes(“d”), Bytes.toBytes(“b”),Bytes.toBytes(“bbbb”));
//不会写入,因为之前追加过有记录
table.checkAndPut(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“abcd”),put);// 参数:row_key,family,column,row_value
//会写入
table.checkAndPut(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“ad”),put);
table.close();
conn.close();
checkAndDelete:
Configurationconf =HBaseConfiguration.create();
<