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
 * 
 */

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM