HBase Java API 示例

#博学谷IT学习技术支持#

环境准备

在idea中创建Maven工程,导入HBase 客户端相关jar包,pom文件配置如下所示:

 <repositories><!--代码库-->
        <repository>
            <id>aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots>
                <enabled>false</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version></dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

并在resouces下引入log4j.properties文件,可以在控制台清晰地看到日志信息:

log4j.rootLogger=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n

常用类介绍

  1. HBaseConfiguration
    该类是用来创建HBase配置对象,常用的是静态create方法。该方法中返回了Hadoop Configuration 对象,同时加载了HBase中的资源文件。如下:
   Configuration conf = new Configuration();
   conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
   return addHbaseResources(conf);
  1. 管理对象HBaseAdmin和HTable
    两者都是从Connection对象中获取,其中HBaseAdmin对象用于管理表,包括表的创建,删除、禁用等;
    HTable对象主要用于操作表中数据,包括数据的添加、查询、修改、删除等操作
  2. TableDescriptor
    TableDescriptor 对象用于描述表,包括表名、表中列族等。
  3. ColumnFamilyDescriptor
    ColumnFamilyDescriptor 对象用于表述列族信息,包括列族名称、列族中各种属性
  4. Put、Get、Delete
    这些对象用于封装对表中数据的操作,构造方法中传入rowkey信息,根据rowkey对一行数据进行操作,同时还可以添加Column信息,对特定列进行操作。Put用于添加和修改数据,Get用于查询数据,Delete用于删除数据。

常见操作示例

封装创建链接和释放连接方法,创建连接时需要指定ZK集群的IP和端口信息,从而通过ZK集群找到HBase的元数据信息,建立连接:

  public static Connection getConnection(String zkIPStr) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", zkIPStr);
        return ConnectionFactory.createConnection(conf);
    }

    public static void closeConn(Connection conn, Admin admin, Table table) {
        if (table != null) {
            try {
                table.close();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
        }
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
  1. 创建表
 @Test
    public void createTable() throws IOException {
        //创建连接
        //获取管理对象
        Admin admin = conn.getAdmin();
        //进行操作
        if (admin.tableExists(TableName.valueOf("WATER_BILL"))) {
            return;
        }
        TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf("WATER_BILL"))
                .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("C1".getBytes()).build()).build();
        admin.createTable(tableDesc);
        //查询结果集
        //释放连接
    }
  1. 添加数据
    @Test
    public void putData() throws IOException {
        table = conn.getTable(TableName.valueOf("WATER_BILL"));
        Put put = new Put("rk001".getBytes());
        put.addColumn("f1".getBytes(), "name".getBytes(), "登卫红".getBytes());
        put.addColumn("f1".getBytes(), "address".getBytes(), "贵州省铜仁市".getBytes());
        put.addColumn("f1".getBytes(), "sex".getBytes(), "男".getBytes());
        table.put(put);
    }
  1. 查询数据
 @Test
    public void queryData() throws IOException {
        table = conn.getTable(TableName.valueOf("WATER_BILL"));
        Get get = new Get("rk001".getBytes());
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        if (cells != null) {
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String columnName = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("rowkey: " + row + ", family:column " + family + ":"
                        + columnName + ", value: " + value );
            }
        } else {
            System.out.println("No data");
        }

    }
  1. 删除数据
    @Test
    public void deleteData() throws IOException {
        table = conn.getTable(TableName.valueOf("WATER_BILL"));
        Delete delete = new Delete("rk001".getBytes());
        table.delete(delete);
    }

 @Test
    public void deleteTable() throws IOException {
        admin = conn.getAdmin();
        if (admin.isTableEnabled(TableName.valueOf("WATER_BILL"))) {
            admin.disableTable(TableName.valueOf("WATER_BILL"));
        }
        admin.deleteTable(TableName.valueOf("WATER_BILL"));
    }

  1. 基于Scan的扫描查询
 @Test
    public void scanTable() throws IOException {
        Scan scan = new Scan();
        table = conn.getTable(TableName.valueOf("WATER_BILL"));
        BinaryComparator startTimeComp = new BinaryComparator("2020-06-01".getBytes());
        BinaryComparator endTimeComp = new BinaryComparator("2020-06-30".getBytes());
        SingleColumnValueFilter startTimeFilter
                = new SingleColumnValueFilter("C1".getBytes(), "RECORD_DATE".getBytes(),
                CompareOperator.GREATER_OR_EQUAL, startTimeComp);
        SingleColumnValueFilter endTimeFilter
                = new SingleColumnValueFilter("C1".getBytes(), "RECORD_DATE".getBytes(),
                CompareOperator.LESS_OR_EQUAL, endTimeComp);
        FilterList filterList = new FilterList();
        filterList.addFilter(startTimeFilter);
        filterList.addFilter(endTimeFilter);
        scan.setFilter(filterList);
        scan.addColumn("C1".getBytes(), "RECORD_DATE".getBytes());
        scan.addColumn("C1".getBytes(), "NAME".getBytes());
        scan.addColumn("C1".getBytes(), "NUM_USAGE".getBytes());
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()) {
            Result next = iterator.next();
            List<Cell> cells = next.listCells();
            if (cells != null) {
                for (Cell cell : cells) {
                    String row = Bytes.toString(CellUtil.cloneRow(cell));
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    String columnName = Bytes.toString(CellUtil.cloneQualifier(cell));
                    Object value;
                    if ("NUM_USAGE".equals(columnName)) {
                        value = Bytes.toDouble(CellUtil.cloneValue(cell));
                    } else {
                        value = Bytes.toString(CellUtil.cloneValue(cell));
                    }
                    System.out.println("rowkey: " + row + ", family:column " + family + ":"
                            + columnName + ", value: " + value );
                }
            } else {
                System.out.println("No data");
            }
        }
    }

你可能感兴趣的:(大数据)