zoukankan      html  css  js  c++  java
  • Hbase 根据rowkey批量读写

    批量查询Hbase 传入一个rowkey List 返回一个嵌套 HashMap<String, HashMap<String, String>>

        public static HashMap<String, HashMap<String, String>> queryTableBatch(String tableName,List<String> rowkeyList) throws IOException {
            Connection connection = ConnectionFactory.createConnection(conf);
            List<Get> getList = new ArrayList();
            HashMap<String, HashMap<String, String>> resultMap = new HashMap();
            Table table = connection.getTable(TableName.valueOf(tableName));// 获取表
            for (String rowkey : rowkeyList) {//把rowkey加到get里,再把get装到list中
                Get get = new Get(Bytes.toBytes(rowkey));
                getList.add(get);
            }
            Result[] results = table.get(getList);
    //        System.out.println("results.length"+results.length);
            for (Result result : results) {
                System.out.println(result.rawCells().length);
                if(result.rawCells().length>0){
                    String rowkey=Bytes.toString(result.getRow());
                    HashMap<String, String> map = new HashMap();
                    for (Cell kv : result.rawCells()) {
                        String value = Bytes.toString(CellUtil.cloneValue(kv));
                        map.put(Bytes.toString(CellUtil.cloneQualifier(kv)),value);
                    }
                    resultMap.put(rowkey,map);
                }
            }
            table.close();
            return resultMap;
        }
    

    根据嵌套HashMap 批量写入Hbase

          public static void writeTable(String tableName, HashMap<String, HashMap<String, String>> inputMap)  {
            try {
                Connection connection = ConnectionFactory.createConnection(conf);
                Table table = connection.getTable(TableName.valueOf(tableName));
                List<Put> list = new ArrayList();
                for (String rowkey : inputMap.keySet()) {
                    Put put = new Put(Bytes.toBytes(rowkey));
                    for (Map.Entry<String, String> entry : inputMap.get(rowkey).entrySet()) {
                        put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()));
                    }
                    if (!put.isEmpty()) {
                        list.add(put);
                    }
                }
                table.put(list);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    测试

      public static void main(String[] args) throws IOException {
    
            List<String> list2 = new ArrayList<String>();
            list2.add("1001");
            list2.add("1002");
    
            HashMap<String, HashMap<String, String>> a = queryTableTestBatch(list2);
            System.out.println(a);
            for (String i : a.keySet()){
                System.out.println(i);
                Set<Map.Entry<String, String>> entries = a.get(i).entrySet();
                System.out.println(entries);
    
            }
            //结果:{1002={sex=female, name=Janna, age=20}, 1001={sex=male, age=18}}
    
    
        }
    
  • 相关阅读:
    ASP.NET中的DataBinder.Eval用法
    jquery Ajax调用asmx和ashx代码示例三级联动
    项目中使用的架构
    asp.net(c#)上传图片到数据库
    asp.net(c#)从数据库里读取图片并显示到页面
    一款好的UI草图设计软件
    Windows Azure云平台(无须提供信用卡)[转]
    推荐8个超棒的学习 jQuery 的网站
    推荐两个界面原型设计工具GUIDesignStudio 和 Mockups For Desktop
    在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
  • 原文地址:https://www.cnblogs.com/successok/p/14737319.html
Copyright © 2011-2022 走看看