HBase客户端API-表管理

时间:2025-03-23 09:45:07

上一篇博客说了怎样搭建HBase环境,今天说说怎样使用 HBase 的客户端 API 来操作 HBase 中的数据。

创建工程

首先创建一个 Maven 工程,然后添加hbase客户端api的依赖项,如下:

<dependency>
    <groupId></groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.0.0-alpha4</version>
</dependency>

添加配置文件

在 Maven 工程的resources目录下添加 文件,hbase客户端 API 默认会在系统的 classpath 下找此文件来加载连接信息。

<configuration>
    <property>
        <name></name>
        <value>file:///apps/hbase-2.0.0-beta-1/data/hbase</value>
    </property>
    <property>
        <name></name>
        <value>/apps/hbase-2.0.0-beta-1/data/zookeeper</value>
    </property>
    <property>
        <name></name>
        <value>bd1,bd2,bd3</value>
    </property>
    <property>
        <name></name>
        <value>true</value>
    </property>
</configuration>

创建 Connection 对象

要操作 HBase 的数据表,也和 JDBC 编程类似,需要创建一个 HBase Connection 对象,然后通过这个 Connection 对象来操作,操作完成后需要关闭此连接。

// 根据  文件初始化 Configuration 对象
Configuration config = ();

// 根据 Configuration 对象初始化 Connection 对象
Connection connection = (config);

// 操作数据
// ...

();

表管理

在 HBase中,要管理表,需要通过 类实现,可以通过 () 方法来获取一个 Admin 对象实例。

查看表

在 HBase 中,表的描述信息保存在 类中,因此我们可以通过 () 来获取 HBase 中所有表的描述信息。然后可以通过 类的一些方法查看或修改表的定义。最后在要记得调用 () 来关闭操作。

    private void listTable() throws IOException {
        Admin admin = ();

        try {
            List<TableDescriptor> tableDescriptors = ();
            for (TableDescriptor tableDescriptor : tableDescriptors) {
                TableName tableName = ();
                ("Table: " + tableName);
                ("\texists: " + (tableName));
                ("\tenabled: " + (tableName));
            }
        } finally {
            ();
        }
    }

创建表

创建表的操作主要是通过 TableDescriptor 和 ColumnFamilyDescriptor 类来实现的,而要创建 TableDescriptor 和 ColumnFamilyDescriptor 对象需要使用 (…) 和 (…) 方法来创建。下面代码演示了怎样创建一个 user 表,其中包含了 base 和 address 两个列族。

    private void createTable() throws IOException {
        Admin admin = ();

        try {
            TableDescriptor tableDesc = (("user"))
                    .addColumnFamily((("base")).build())
                    .addColumnFamily((("address")).build())
                    .build();
            (tableDesc);
        } finally {
            ();
        }
    }

删除表

在 HBase 中对表的删除需要通过 () 和 () 方法来实现,如下:

    private void deleteTable() throws IOException {
        Admin admin = ();

        try {
            (("user"));
            (("user"));
        } finally {
            ();
        }
    }

完整代码

最后看一下 HBase 中表管理操作的完整例子代码。

package ;

import ;
import ;
import ;
import .*;
import ;

import ;
import ;
import ;

public class TestAPI {

    private static final String TABLE_NAME = "user";

    private static final String COLUMN_FAMILY_BASE = "base";
    private static final String COLUMN_FAMILY_ADDRESS = "address";

    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_HOME = "home";
    private static final String COLUMN_OFFICE = "office";

    private Connection connection;

    public static void main(String[] args) throws Exception {
        Configuration config = ();
        Connection connection = (config);

        TestAPI t = new TestAPI(connection);

        ();

        ();
        ();
        ();

        ();
    }

    public TestAPI(Connection connection) {
        this.connection = connection;
    }

    private void listTable() throws IOException {
        Admin admin = ();

        try {
            List<TableDescriptor> tableDescriptors = ();
            for (TableDescriptor tableDescriptor : tableDescriptors) {
                TableName tableName = ();
                ("Table: " + tableName);
                ("\texists: " + (tableName));
                ("\tenabled: " + (tableName));
            }
        } finally {
            ();
        }
    }

    private void createTable() throws IOException {
        Admin admin = ();

        try {
            TableDescriptor tableDesc = ((TABLE_NAME))
                    .addColumnFamily(((COLUMN_FAMILY_BASE)).build())
                    .addColumnFamily(((COLUMN_FAMILY_ADDRESS)).build())
                    .build();
            (tableDesc);
        } finally {
            ();
        }
    }

    private void deleteTable() throws IOException {
        Admin admin = ();

        try {
            ((TABLE_NAME));
            ((TABLE_NAME));
        } finally {
            ();
        }
    }
}