Hbase之原子性插入

本文介绍了一个HBase中使用原子性Put操作的例子,演示了如何在HBase表中进行条件更新,并展示了当更新条件不满足时的操作行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Created by similarface on 16/8/16.
 */

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;

import java.util.List;
import java.util.ArrayList;

/**
 * 批量插入的时候 如果其中一行有问题 该行实效其余的会入库
 */
public class PutDataWithAtomic {
    public static void main(String[] args) throws IOException {
        //获取陪着参数
        Configuration config = HBaseConfiguration.create();
        //建立连接
        Connection connection = ConnectionFactory.createConnection(config);

        //连接表 获取表对象
        Table table = connection.getTable(TableName.valueOf("testtable"));
        Put put1 = new Put(Bytes.toBytes("10000"));
        put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("company"), Bytes.toBytes("dianxin"));
        boolean res1 = table.checkAndPut(Bytes.toBytes("10000"), Bytes.toBytes("colfam1"), Bytes.toBytes("company"), null, put1);
        System.out.println("Put 1a applied: " + res1);

        boolean res2 = table.checkAndPut(Bytes.toBytes("10000"), Bytes.toBytes("colfam1"), Bytes.toBytes("company"), null, put1);
        System.out.println("Put 1b applied: " + res2);

        Put put2 = new Put(Bytes.toBytes("10000"));
        put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("company"), Bytes.toBytes("dx"));
        boolean res3 = table.checkAndPut(Bytes.toBytes("10000"), Bytes.toBytes("colfam1"), Bytes.toBytes("dianxin"), Bytes.toBytes("val1"), put2);
        System.out.println("Put 2 applied: " + res3);

        Put put3 = new Put(Bytes.toBytes("10086"));
        put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("company"), Bytes.toBytes("yidong"));
        boolean res4 = table.checkAndPut(Bytes.toBytes("10000"), Bytes.toBytes("colfam1"), Bytes.toBytes("company"), Bytes.toBytes("val1"), put3);
        System.out.println("Put 3 applied: " + res4);
        connection.close();

    }
}

  

转载于:https://www.cnblogs.com/similarface/p/5783276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值