HBase table SCan 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.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
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.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class ScanData {

    public static void main(String[] args) {

        String talbeName = "test1";
        String columnFamily = "cf1";
        String columnFamily2 = "cf3";
        String attribute = "tt";
        String rowkey = "row1";

        Connection connection = null;
        Table table = null;
        ResultScanner resultScanner = null;

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

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

            FilterList listFilter = new FilterList(
                    FilterList.Operator.MUST_PASS_ALL);
            Filter filter = new SingleColumnValueFilter(
                    Bytes.toBytes(columnFamily), Bytes.toBytes(attribute),
                    CompareOp.EQUAL, Bytes.toBytes("value4"));
            // any value that starts with 'my'
            RegexStringComparator comp = new RegexStringComparator("va.");
            Filter filter2 = new SingleColumnValueFilter(
                    Bytes.toBytes(columnFamily2), Bytes.toBytes(attribute),
                    CompareOp.EQUAL, comp);
            listFilter.addFilter(filter);
            listFilter.addFilter(filter2);

            Scan scan = new Scan();
            // 设置缓存条数(不是获取的条数)
            scan.setCaching(100);
            scan.setFilter(listFilter);//不好用
            scan.addFamily(Bytes.toBytes(columnFamily2));
            scan.addColumn(Bytes.toBytes(columnFamily),
            Bytes.toBytes(attribute));
            scan.setRowPrefixFilter(Bytes.toBytes(rowkey));
            resultScanner = table.getScanner(scan);

            for (Result result : resultScanner) {
                /*这个还不好用
                for(Cell cell : result.listCells()){
                    System.out.println("列:" + Bytes.toString(cell.getFamilyArray()) 
                    + ", 值:" + Bytes.toString(cell.getValueArray())  + ", " 
                            + Bytes.toString(cell.getQualifierArray())); 
                }*/
                System.out.println(">>>>Row:" + Bytes.toString(result.getRow())
                        + ", value:" + Bytes.toString(result.value()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultScanner != null) {
                try {
                    resultScanner.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            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、付费专栏及课程。

余额充值