Hbase学习03-api操作02

1、根据表名查询他所有的rowkey

 /*--------------根据表名查询所有的rowkey*/
    @Test
    public void getRowKey() throws IOException {
        String tableName="WATER_BILL";
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner results = table.getScanner(scan);
        ArrayList<Object> list = new ArrayList<>();
        for (Result result : results) {
            list.add(Bytes.toString(result.getRow()));
            System.out.println(Bytes.toString(result.getRow()));
        }
    }

Hbase学习03-api操作02

 

 

 2、根据表名和rowkey获取一行所有数据

 /*--------------根据表名和rowkey获取一行所有数据*/
    @Test
    public void getRoWDetail() throws IOException {
        String tablename,rowkey;
        rowkey="4944191";
        Table table = connection.getTable(TABLE_NAME);
        byte[] row = Bytes.toBytes(rowkey);
        Get get = new Get(row);
        Result r = table.get(get);

        HashMap<String, String> rst = new HashMap<>();
        List<Map.Entry> ans = new ArrayList<>();
        if(null!=r && !r.isEmpty())
        {
            for (Cell cell : r.listCells()) {
                String key = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                rst.put(key,value);//保存操作
                System.out.println(key+" -> "+value);
            }
            ans.addAll(rst.entrySet());
        }
    }

Hbase学习03-api操作02

 

 

 3、根据表名和rowkey和列蔟获取列的个数

  /*根据表名和rowkey和列蔟查询当前的列的数量*/
    @Test
    public void getCountKeyColumnFamily() throws IOException {
        String tablename,rowkey,columnFamily;
        rowkey="4944191";
        columnFamily="C1";
        Table table = connection.getTable(TABLE_NAME);
        byte[] row = Bytes.toBytes(rowkey);
        Get get = new Get(row);
        Result result = table.get(get);
        if(null!=result && !result.isEmpty())
        {
            NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(columnFamily));
            System.out.println(familyMap.size());
        }

    }

Hbase学习03-api操作02

 

 4、根据key的范围查询数据

   /*根据key的范围查询数据*/
    @Test
    public void getRowRangeData() throws IOException {
        Table table = connection.getTable(TABLE_NAME);
        String tablename,startRowkey,endRowkey;
        startRowkey="4944192";
        endRowkey="4944193";
        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);
            System.out.println(key+ "  -> " +result);
        }
        List<Map.Entry> list = new ArrayList<>();
        list.addAll(map.entrySet());
    }

Hbase学习03-api操作02

 5、查询复合范围的数据。参数有表名、列蔟、列名、开始值、结束值

   /*询复合范围的数据。参数有表名、列蔟、列名、开始值、结束值*/
    @Test
    public void getRangeData() throws IOException {
        String tablename,columnFamily,column,begin,end;
        columnFamily="C1";
        column="NUM_USAGE";
        begin="24";
        end="26";
        Table table = connection.getTable(TABLE_NAME);
        Scan scan = new Scan();
        List<Filter> filters = new ArrayList<>();
        SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(column), CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(begin));
        SingleColumnValueFilter 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());
            System.out.println(key+" - > "+result);
            map.put(key,result);
        }
        List<Map.Entry> list = new ArrayList<>();
        list.addAll(map.entrySet());
    }

Hbase学习03-api操作02

 

 6、根据条件查询

 

  /*根据条件查询*/
    @Test
    public void getData() throws IOException {
        String tablename,columnFamily;
        columnFamily="C1";
//        Map<String,String> columnAndValue = null;
//        columnAndValue.put("NUM_USAGE","25");
        Table table = connection.getTable(TABLE_NAME);
        Scan scan = new Scan();
        List<Filter> filters = new ArrayList<Filter>();
        String k,v;
        k="NUM_USAGE";
        v="25";
        Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(k),
                CompareFilter.CompareOp.EQUAL,Bytes.toBytes(v));
        filters.add(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);
            System.out.println(key+" -> "+result);
        }
        List<Map.Entry> list = new ArrayList<>();
        list.addAll(map.entrySet());
    }

Hbase学习03-api操作02

 

Hbase学习03-api操作02

上一篇:完美解决pyinstaller 打包报错找不到依赖pypiwin32 或pywin32-ctypes的错误


下一篇:WPF/C# Treeview 禁用双击折叠展开