HBase table Get Data 获取数据

来源:互联网 发布:加油卡办卡通知 编辑:程序博客网 时间:2024/06/01 19:49

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