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();
                    }
                }
            }
        }
  • 相关阅读:
    如何搭建环境---初识mybatis
    切片,元组,字典字,符串
    关于Linux安装的Python和miniconda
    认识Linux工具
    Linux下安装mysql
    Linux软件的安装
    关于Linux的简单介绍
    如果要做优化,CSS提高性能的方法有哪些?
    文本溢出显示省略号css
    响应式设计?响应式设计的基本原理是什么?如何做?
  • 原文地址:https://www.cnblogs.com/liqiu/p/4118284.html
Copyright © 2011-2022 走看看