HBase table Get Data 获取数据

HBase 版本: 1.2.1

package com.feng.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class GetData {

    public static void main(String[] args) {

        String talbeName = "test1";
        String columnFamily = "cf";
        String columnFamily2 = "cf2";
        String attribute = "a";
        String rowkey = "row1";

        Connection connection = null;
        Table table = null;
        try {

            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", "localhost");
            connection = ConnectionFactory.createConnection(config);

            table = connection.getTable(TableName.valueOf(talbeName));

            if (table != null) {
                System.out.println("========第一种========");
                // 第一种利用 Get 获取值
                Get get = new Get(Bytes.toBytes(rowkey));
                get.setMaxVersions(1);// will return last 4 versions of row
                Result result = table.get(get);
                if (!result.isEmpty()) {
                    // 根据 columnFamily 和 attribute 取value值
                    byte[] value = result.getValue(Bytes.toBytes(columnFamily),
                            Bytes.toBytes(attribute));
                    String valueStr = Bytes.toString(value);
                    System.out.println(rowkey + " GET value : " + valueStr);

                    // 用cell取的值 不太好用
                    Cell cell = result.getColumnLatestCell(
                            Bytes.toBytes(columnFamily),
                            Bytes.toBytes(attribute));
                    System.out.println("cell.getFamilyLength(): "
                            + cell.getFamilyLength());
                    System.out.println("cell.getValueArray(): "
                            + Bytes.toString(cell.getValueArray()));
                    System.out.println("cell.getQualifierArray(): "
                            + Bytes.toString(cell.getQualifierArray()));

                } else {
                    System.out.println(rowkey + " is null rowkey!");
                }

                System.out.println("========第二种========");
                // 第二种利用 Get 获取值加过滤器获取值 好像是filter 没什么用
                get = new Get(Bytes.toBytes(rowkey));
                get.setMaxVersions(2);
                FilterList listFilter = new FilterList(
                        FilterList.Operator.MUST_PASS_ONE);
                Filter filter = new SingleColumnValueFilter(
                        Bytes.toBytes(columnFamily), Bytes.toBytes(attribute),
                        CompareOp.EQUAL, Bytes.toBytes(""));
                listFilter.addFilter(filter);
                get.setFilter(listFilter);

                result = table.get(get);
                if (!result.isEmpty()) {
                    // 根据 columnFamily 和 attribute 取value值
                    byte[] value = result.getValue(Bytes.toBytes(columnFamily),
                            Bytes.toBytes(attribute));
                    String valueStr = Bytes.toString(value);
                    System.out.println(rowkey + " GET value : " + valueStr);

                } else {
                    System.out.println(rowkey + " is null rowkey!");
                }

            } else {
                System.out.println("There is not " + talbeName);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值