已复制
全屏展示
复制代码

Hbase JAVA API 接口使用


· 4 min read

下面是常用的 HBase 的交互方式,本文以 Java API 为例,给出常用场景的使用方法。

交互方式 描述
HBase Shell HBase的命令行工具,最简单的接口,适合HBase管理使用。
Java API 最常规和高效的访问方式。
MapReduce 直接使用MapReduce作业处理Hbase数据;
Thrift Gateway 利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据。
REST Gateway 支持REST 风格的Http API访问HBase, 解除了语言限制。
第三方组件集成 Hive\Pig处理HBase中的数据。

一. 添加依赖

根据 hbase 的版本,添加相同版本的 hbase-client 客户端。

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.0.2</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-common</artifactId>
    <version>2.0.2</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>2.0.2</version>
</dependency>

二. 配置文件

将安装Hbase的配置文件hbase-site.xml拷贝到src/main/resources/目录下,如果需要打包然后在其他服务器上跑的话,将该配置文件放置在classpath下即可。

三. 编写代码

编写代码并运行 HbaseHello.java

package com.yuchaoshui;

import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.stream.Collectors;

public class HbaseHello {
    public static void main(String[] args) throws Exception {
        Hbase hbase = new Hbase();
        hbase.listTables();
        hbase.dropTable();
        hbase.createTable();
        hbase.putRows();
        hbase.scanRows();
        hbase.showColumnFamilyNames();
        hbase.getByRowKey("20201223-1");

        hbase.closeHbase();
    }
}

class Hbase {
    Configuration configuration;
    Connection connection;
    Admin admin;

    public Hbase() throws Exception {
        this.configuration = HBaseConfiguration.create();
        this.connection = ConnectionFactory.createConnection(configuration);
        this.admin = connection.getAdmin();
    }

    public void closeHbase() throws IOException {
        this.connection.close();
        this.admin.close();
    }


    // 创建table
    public void createTable() throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        if (admin.tableExists(tableName)) {
            System.err.println(new String(tableName.getName()) + "已存在");
            return;
        }
        List<ColumnFamilyDescriptor> families = new ArrayList<>();
        Collections.addAll(families,
                ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("grade")).build(),
                ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("course")).build(),
                ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build()
        );
        TableDescriptor tableDescriptor = TableDescriptorBuilder
                .newBuilder(tableName)
                .setColumnFamilies(families)
                .build();
        admin.createTable(tableDescriptor);
        System.out.println();
    }


    // 查看所有列族名称
    public void showColumnFamilyNames() throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        TableDescriptor tableDescriptor = this.admin.getDescriptor(tableName);
        for (byte[] b : tableDescriptor.getColumnFamilyNames()) {
            System.out.println(new String(b));
        }
        System.out.println();
    }


    // 扫描整个表
    public void scanRows() throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        HTable hTable = (HTable) this.connection.getTable(tableName);
        ResultScanner scanner = hTable.getScanner(new Scan());
        for (Result r : scanner) {
            for (Cell cell : r.listCells()) {
                printCell(cell);
            }
        }
        scanner.close();
        System.out.println();
    }


    // 打印cell
    public void printCell(Cell cell) {
        String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
        String columnFamily = Bytes.toString(CellUtil.cloneFamily(cell));
        String columnCell = Bytes.toString(CellUtil.cloneQualifier(cell));
        String value = Bytes.toString(CellUtil.cloneValue(cell));
        System.out.println(
                String.join(",", new ArrayList<String>() {{
                    add(rowKey);
                    add(columnFamily);
                    add(columnCell);
                    add(value);
                }}));
    }


    // 获取一行数据
    public void getByRowKey(String rowKey) throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        HTable hTable = (HTable) this.connection.getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = hTable.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            this.printCell(cell);
        }
        System.out.println();
    }


    // 删除表
    public void dropTable() throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        if (!admin.tableExists(tableName)) {
            System.err.println(new String(tableName.getName()) + "不存在");
            return;
        }
        this.admin.disableTable(TableName.valueOf("student"));
        this.admin.deleteTable(TableName.valueOf("student"));
        System.out.println();
    }


    // 插入行
    public void putRows() throws IOException {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        TableName tableName = TableName.valueOf("student");
        HTable hTable = (HTable) this.connection.getTable(tableName);
        for (int i = 0; i < 4; i++) {
            Put put = new Put(Bytes.toBytes("20201223-" + i));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("poker"), Bytes.toBytes("100"));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("phone"), Bytes.toBytes("101"));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("computer"), Bytes.toBytes("102"));
            hTable.put(put);
        }
        hTable.close();
        System.out.println();
    }


    // 打印所有表
    public void listTables() throws Exception {
        System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName());
        List<TableDescriptor> allTable = this.admin.listTableDescriptors();
        for (TableDescriptor hTableDescriptor : allTable) {
            System.out.println(hTableDescriptor.getTableName());
        }
        System.out.println();
    }
}
🔗

文章推荐