大数据开发进阶笔记——HBase的常用原子操作Java代码

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();

<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值