Java中Hbase查询,复杂,条件范围,时间范围,精确查询等等

java中Hbase各种查询

根据列族名,key,范围查询等

网上看到很多Hbase查询,没找到一个相对全面的,这里我来总结一下吧

声明下面conn是什么

  public static Connection conn;
  static{
     Configuration conf = HBaseConfiguration.create();
     conf.set("hbase.rootdir", );
    conf.set("hbase.zookeeper.quorum", );
    conf.set("hbase.client.scanner.timeout.period", ));
    conf.set("hbase.rpc.timeout", );
     conn = ConnectionFactory.createConnection(conf);
  }

根据表名获取所有的key

参数:
String tableName 表名

    Table table = conn.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    ResultScanner results =  table.getScanner(scan);
    List<String> list = new ArrayList<>();
    for(Result result:results){
        list.add(new String(result.getRow()));
    }

在这里插入图片描述

根据key查询一条数据

参数:
String tableName 表名
String rowKey 表的key(id)

    Table table = conn.getTable(TableName.valueOf(tableName));
    byte[] row = Bytes.toBytes(rowKey);
    Get get = new Get(row);
    Result r = table.get(get);
    if(null != r && !r.isEmpty()){
        for (Cell cell : r.listCells()){
            //列族名
            String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
            //列名
            String key = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
            //列值
            String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
        }
    }

在这里插入图片描述

根据表名,KEY和列族名 查询一行这个列族中列的数量

参数:
String tableName 表名
String columnFamily 列族名
String rowKey 表的key(id)

   Table table = conn.getTable(TableName.valueOf(tableName));
    byte[] row = Bytes.toBytes(rowKey);
    Get get = new Get(row);
    Result result = table.get(get);
    if(null != result && !result.isEmpty()){
        Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(columnFamily));
        return familyMap.size();
    }

在这里插入图片描述

根据表名和列族名 查询每行这个列族中列的数量

参数:
String tableName 表名
String columnFamily 列族名

   Table table = conn.getTable(TableName.valueOf(tableName));
    ResultScanner results =  table.getScanner(new Scan());
    Map<String,Integer> map = new HashMap<>();
    for (Result result:results){
        String key = new String(result.getRow());
        Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(columnFamily));
        
        //familyMap.size()是columnFamily这个列族中列的数量
        map.put(key,familyMap.size());
    }
    List<Map.Entry<String,Integer>> list = new ArrayList<>();
    list.addAll(map.entrySet());

在这里插入图片描述

根据KEY的范围查询数据

参数:
String tableName 表名
String startRowKey
String endRowKey

    Table table = conn.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    scan.setStartRow(Bytes.toBytes(startRowKey));
    scan.setStopRow(Bytes.toBytes(endRowKey));
    ResultScanner results =  table.getScanner(scan);
    Map<String,Result> map = new HashMap<>();
    for (Result result : results){
        String key = new String(result.getRow());
        map.put(key,result);
    }
    List<Map.Entry> list = new ArrayList<>();
    list.addAll(map.entrySet());

在这里插入图片描述

根据时间戳查询 时间范围内的数据

   Table table = conn.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    scan.setTimeRange(beginTime,endTime);
    ResultScanner results = table.getScanner(scan);
    Map<String,Object> map = new HashMap<>();
    for (Result result : results){
        String key = new String(result.getRow());
        map.put(key,result);
    }
    List<Map.Entry> list = new ArrayList<>();
    list.addAll(map.entrySet());

在这里插入图片描述

根据查询符合范围的数据

    Table table = conn.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    List<Filter> filters = new ArrayList<Filter>();
    Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
            CompareFilter.CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(begin));
    Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
            CompareFilter.CompareOp.LESS,Bytes.toBytes(end));
    filters.add(filter1);
    filters.add(filter2);
    FilterList filterList = new FilterList(filters);
    scan.setFilter(filterList);
    ResultScanner results =  table.getScanner(scan);
    Map<String,Result> map = new HashMap<>();
    for(Result result:results){
        String key = new String(result.getRow());
        map.put(key,result);
    }
    List<Map.Entry> list = new ArrayList<>();
    list.addAll(map.entrySet());

在这里插入图片描述

根据条件查询数据

    Table table = conn.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    List<Filter> filters = new ArrayList<Filter>();
    columnAndValue.forEach((k,v)->{
        Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(k),
                CompareFilter.CompareOp.EQUAL,Bytes.toBytes(v));
        filters.add(filter);
    });
    FilterList filterList = new FilterList(filters);
    scan.setFilter(filterList);
    ResultScanner results =  table.getScanner(scan);
    Map<String,Result> map = new HashMap<>();
    for(Result result:results){
        String key = new String(result.getRow());
        map.put(key,result);
    }
    List<Map.Entry> list = new ArrayList<>();
    list.addAll(map.entrySet());

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值