码迷,mamicode.com
首页 > 其他好文 > 详细

Hbase之校验指定数据是否存在

时间:2016-08-22 12:22:27      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Hbase之校验指定数据是否存在

标签:

原文地址:http://www.cnblogs.com/similarface/p/5794977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!