zoukankan      html  css  js  c++  java
  • hadoop之根据Rowkey从HBase中查询数据

    1.Hbase 根据rowkey 查询

    conf的配置信息如下:

            conf = new Configuration();
    
            conf.set("hbase.zookeeper.quorum", "192.168.50.253:2181");
            conf.set("hbase.rootdir", "hdfs://192.168.50.253:9000/hbase");

    public ArrayList<String>  getRoutes(String rowkey,String tablename) throws IOException {
    
            HTable hTable = new HTable(conf, tablename);//traffic_route
            Get get = new Get(rowkey.getBytes());
            Result r = hTable.get(get);
    
            KeyValue[] kv = r.raw();  // 行健对应的值.
    
            ArrayList<String> list = new ArrayList<>();
            if (kv.length > 0) {
                String ss = kv[0].getValue().toString();
    
    
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
                Date tempDay = new Date();
                tempDay = null;
                int sum = 0;
    
                String tempDate = "";
                String mapStringValue = "";
                for (int i = 0; i < kv.length; i++) {
                    String value = new String(kv[i].getValue());
    //            System.out.println("value = " + value);
    
    
                    String[] station = value.split("--");
    //            System.out.println(station[0]);
                    String[] date = station[0].split(" ");
                    try {
                        Date day = sdf.parse(date[0]);
    
                        if (tempDay == null) {
                            tempDay = day;
    
                        }
    
                        if ((day.compareTo(tempDay) == 0)) {
    
                            tempDate = date[0];
    
                            mapStringValue = mapStringValue + station[1] + "-";
    
                        } else {
    
                            Map<String, String> map = new HashMap<>();
    
                            map.put(tempDate, mapStringValue);
    
                            list.add(mapStringValue);
                            mapStringValue = "";
    
                            tempDay = day;
                            mapStringValue += station[1] + "-";
    
                        }
    
    
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
    
    
                }
    
    //        System.out.println("list  = " + list.size());
    
                Map<String, String> map = new HashMap<>();
    
                map.put(tempDate, mapStringValue);
    
                list.add(mapStringValue);
                mapStringValue = "";
            }
    
    //        System.out.println("list.get(0)  = " + list.get(0));
    //        System.out.println("list.get(1)  = " + list.get(1));
                if (list.size() == 0) {
                    System.out.println("取出数据为空");
                } else {
                    System.out.println("获取数据成功");
                }
                return list;
            }
    
  • 相关阅读:
    SQlServer 从系统表 sysobjects 中获取数据库中所有表或存储过程等对象
    Win7 Print Spooler服務自动关闭
    C# 数据流操作 Stream 相关
    GRUB引导故障解决
    RAID配置层+配额
    磁盘一
    权限管理及归属
    cenos7关闭防火墙 安全机制
    linux用户管理-----账号管理和权限归属设置
    yum 仓构建,源代码安装
  • 原文地址:https://www.cnblogs.com/chaoren399/p/5004555.html
Copyright © 2011-2022 走看看