• Hbase之原子性更新数据


    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.filter.CompareFilter;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    /**
     * 修改数据实现原子性
     *
     * 重点:
     * checkAndMutate
     */
    public class UpdateDataWithAtomic {
        public static void main(String[] args) throws IOException{
            Configuration configuration = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(configuration);
            //建立表的连接
            Table table = connection.getTable(TableName.valueOf("testtable"));
            //获取put实例
            Put put = new Put(Bytes.toBytes("10086"));
            put.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual7"),4,Bytes.toBytes("UpdateDataWithAtomic"));
            put.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual8"),4,Bytes.toBytes("UpdateDataWithAtomicTest"));
            //删除
            Delete delete = new Delete(Bytes.toBytes("10086"));
            delete.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"));
            //更新实例
            RowMutations mutations = new RowMutations(Bytes.toBytes("10086"));
            mutations.add(put);
            mutations.add(delete);
            //Mutate 1 successful: false
            //checkAndMutate (行键,列族,列分隔) =>比对操作<= (期望值) ====> T mutations F not mutations
            //(china mobile 1)<(val1) 为假没有进行更新
            boolean res1 = table.checkAndMutate(Bytes.toBytes("10086"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations);
            System.out.println("Mutate 1 successful: " + res1);
            //这儿插入了一行数据会导致10086-colfam1-qual1的val2比期望的val1大
            Put put2 = new Put(Bytes.toBytes("10086"));
            put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), 4, Bytes.toBytes("val2"));
            table.put(put2);
            //Mutate 2 successful: true
            //(val2)>(val1) 为真 进行了跟新
            boolean res2 = table.checkAndMutate(Bytes.toBytes("10086"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations);
            System.out.println("Mutate 2 successful: " + res2);
        }
    }
    //olddata
    /**
     10086                                           column=colfam1:qual1, timestamp=4, value=china mobile 1
     10086                                           column=colfam1:qual4, timestamp=4, value=china mobile 4
     */
    //newdata
    /**
     10086                                           column=colfam1:qual4, timestamp=4, value=china mobile 4
     10086                                           column=colfam1:qual7, timestamp=4, value=UpdateDataWithAtomic
     10086                                           column=colfam1:qual8, timestamp=4, value=UpdateDataWithAtomicTest
    **/
    
  • 相关阅读:
    WebSocket
    Spring Web MVC
    对象关系映射(ORM)数据访问
    使用JDBC进行数据访问
    【Mysql】【基础综合题解】大一下学期数据库综合训练题解
    【Mysql】【解决问题】[Err] 1055
    【JavaSE】【IO流】复制单级或多级文件夹
    C#使用FFmpeg的总结
    C#一个IM系统简单总结
    重新开启博客
  • 原文地址:https://www.cnblogs.com/similarface/p/5798751.html
Copyright © 2020-2023  润新知