zoukankan      html  css  js  c++  java
  • JAVA操作Hbase

    获取内容:

    /**
         * 
         * @param zkIp
         * @param zkPort
         * @param tablename
         * @param startRow   传null扫全表
         * @param stopRow 已~结尾
         * @throws Exception
         */
        public static void scanTable(String zkIp,String zkPort,String tablename,String startRow,String stopRow) throws Exception {
             
            HTablePool pool;
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum",zkIp);//
            config.set("hbase.zookeeper.property.clientPort", zkPort);
            pool = new HTablePool(config, 2);
             
            HTableInterface hbTable = null;
            try {
                hbTable = pool.getTable(tablename); // 表名
                ResultScanner rs = null;
                Scan scan = new Scan();
                // scan.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("qual1"));扫某一列
                if (startRow != null) { // 设置扫描的范围
                    scan.setStartRow(Bytes.toBytes(startRow));
                }
                if (stopRow != null) {
                    scan.setStopRow(Bytes.toBytes(stopRow));
                }
     
                rs = hbTable.getScanner(scan);
                hbTable.close();
                for (Result r : rs) {// 按行去遍历
                    for (KeyValue kv : r.raw()) {// 遍历每一行的各列
                        StringBuffer sb = new StringBuffer()
                                .append(Bytes.toString(kv.getRow())).append("	")
                                .append(Bytes.toString(kv.getFamily()))
                                .append("	")
                                .append(Bytes.toString(kv.getQualifier()))
                                .append("	").append(Bytes.toString(kv.getValue()));
                        System.out.println(sb.toString());
                        // kv.getRow() key
                        // kv.getFamily() cf1
                        // kv.getQualifier() 列名
                        // kv.getValue() value
     
                    }
     
                }
     
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }finally{
                pool.close();
            }
               
        }

     添加内容:

        public static void put(String tableName, String rowKey, String family, String column, String value) {
            HTable htable = null;
            try {
                htable = new HTable(HBASE_CONFIG, Bytes.toBytes(tableName));
                // 设置rowkey的值
                Put put = new Put(Bytes.toBytes(rowKey));
                // 设置family:qualifier:value
                put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
                // 使用put类, 写入hbase对应的表中
                htable.put(put);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (htable != null) {
                    try {
                        htable.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
  • 相关阅读:
    京东分页
    相册分类列表页
    在线运行Javascript,Jquery,HTML,CSS代码
    点击事件后动画提示
    一些广告代码
    爱可有—之最经典
    爱可有网络社区需要定义
    鼠标移动时缩小图片显示说明
    Flask-RESTful 快速入门
    Sequelize 关系模型简介
  • 原文地址:https://www.cnblogs.com/liqiu/p/4118284.html
Copyright © 2011-2022 走看看