zoukankan      html  css  js  c++  java
  • Hbase之原子性插入

    /**
     * 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();
    
        }
    }
    

      

  • 相关阅读:
    java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
    STS工具各版本下载网址
    SpringBoot访问不了JSP但却能进入后台
    springboot o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/apache-maven-3.0.5[系统找不到指定路径]
    STS工具:mybayis连接oracle数据库
    springBoot怎样访问静态资源?+静态资源简介
    springboot注解
    8.12-14 df 、mkswap、swapon、swapoff、sync
    8.5-7 mkfs、dumpe2fs、resize2fs
    8.2-3 partprobe、tune2fs
  • 原文地址:https://www.cnblogs.com/similarface/p/5783276.html
Copyright © 2011-2022 走看看