zoukankan      html  css  js  c++  java
  • 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.util.Bytes;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by similarface on 16/8/22.
     * 校验指定数据是否存在
     * 
     */
    public class CheckExistSpecificData {
        public static void main(String args[]) throws IOException{
            Configuration configuration = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(configuration);
            //建立表的连接
            Table table = connection.getTable(TableName.valueOf("testtable"));
            //
            List<Put> puts = new ArrayList<Put>();
            //
            Put put1 = new Put(Bytes.toBytes("10086"));
            put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("中国移动"));
            puts.add(put1);
    
            Put put2 = new Put(Bytes.toBytes("10010"));
            put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("中国联通"));
            puts.add(put2);
    
            Put put3 = new Put(Bytes.toBytes("10010"));
            put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("中国联通客服"));
            puts.add(put3);
            //插入两行数据
            table.put(puts);
            
            Get get1 = new Get(Bytes.toBytes("10010"));
            get1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
            //Only check for existence of data, but do not return any of it.
            //校验数据是否存在但是不返回任何数据  ==> Get 1 Exists: true
            get1.setCheckExistenceOnly(true);
            //检查存在的第一个数据。==>Get 1 Size: 0 
            Result result1 = table.get(get1);
            //这个地方val并没有值 ==>Get 1 Value: null
            byte[] val = result1.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
            System.out.println("Get 1 Exists: " + result1.getExists());
            System.out.println("Get 1 Size: " + result1.size());
            System.out.println("Get 1 Value: " + Bytes.toString(val));
            //获取行键
            Get get2 = new Get(Bytes.toBytes("10010"));
            //获取列族
            get2.addFamily(Bytes.toBytes("colfam1"));
            //设置校验是否存在 ==>Get 2 Exists: true
            get2.setCheckExistenceOnly(true);
            //获取数据集 ==>Get 2 Size: 0
            Result result2 = table.get(get2);
    
            System.out.println("Get 2 Exists: " + result2.getExists());
            System.out.println("Get 2 Size: " + result2.size());
    
            //获取行键
            Get get3 = new Get(Bytes.toBytes("10010"));
            //获取列族 但是错误的列限定符
            get3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual9999"));
            //校验是否存在 ==>Get 3 Exists: false
            get3.setCheckExistenceOnly(true);
            //Get 3 Size: 0
            Result result3 = table.get(get3);
    
            System.out.println("Get 3 Exists: " + result3.getExists());
            System.out.println("Get 3 Size: " + result3.size());
            //获取行键100010
            Get get4 = new Get(Bytes.toBytes("10010"));
            //获取正确的列族 错误的列限定符
            get4.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual9999"));
            //获取正确的列族 正确的列限定符 ==> Get 4 Exists: true
            get4.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
            get4.setCheckExistenceOnly(true);
            //Get 4 Size: 0
            Result result4 = table.get(get4);
            System.out.println("Get 4 Exists: " + result4.getExists());
            System.out.println("Get 4 Size: " + result4.size());
        }
    }
    
    /**
     result:
     Get 1 Exists: true
     Get 1 Size: 0
     Get 1 Value: null
     Get 2 Exists: true
     Get 2 Size: 0
     Get 3 Exists: false
     Get 3 Size: 0
     Get 4 Exists: true
     Get 4 Size: 0
     * 
     */
    
  • 相关阅读:
    扫移动护理系统mysql数据库视图的Database通讯点报错Caused by: java.sql.SQLException: Value '00000000 00:00:00' can not be represented as java.sql.Timestamp
    ORACLE sql insert case when
    解决超过4000字符的字符串insert的时候报错ORA01461: 仅能绑定要插入LONG列的LONG值
    将92服务器上面的加解密服务run.bat形式改为后台服务形式
    Oracle调整sga_max_size内存参后报ORA00844和ORA00851 SGA_MAX_SIZE 42949672960 cannot be set to more than MEMORY_TARGET 6979321856. 导致数据库连接不上去,提示ORA01034:ORACLE notavailable
    解决MATLAB一直初始化,加速MATLAB(转载)
    WIN7下隐藏或显示Lenovo_Recovery_Q盘(转载)
    flowable流程中心设计(一)
    mysqlgroup by原理
    SpringMVC系列导航
  • 原文地址:https://www.cnblogs.com/similarface/p/5794977.html
Copyright © 2011-2022 走看看