• 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()));
            }
        }

     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());
            }
        }

     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());
            }
    
        }

     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());
        }

     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());
        }

     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());
        }

  • 相关阅读:
    安全规约
    课时作业1
    C# 操作防火墙 个人类库
    解决WinScp连接被拒绝的问题
    C# 使用WinSCP方法 类库、脚本
    【运维知识】BAT处理 延迟启动程序 临时解决网络IP获取慢导致的网络连接失败
    AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
    用Angular实时获取本地Localstorage数据,实现一个模拟后台数据登入的效果
    AngularJS实现ajax请求的方法
    AngularJS中指令的四种基本形式实例分析
  • 原文地址:https://www.cnblogs.com/dazhi151/p/13843848.html
Copyright © 2020-2023  润新知