Todos os produtos
Search
Central de documentação

Lindorm:Use the HBase Java API to connect to and use LindormTable

Última atualização: Jun 28, 2026

Conecte-se ao LindormTable pela sua aplicação Java usando a HBase Java API. Este tópico aborda a configuração de conexão, operações DDL (criar, desabilitar, truncar e excluir tabelas) e operações DML (inserir, ler, excluir e escanear dados).

Pré-requisitos

Antes de começar, verifique se você tem:

Parâmetros de conexão

Parâmetro

Chave de configuração

Exemplo

Descrição

host:port

hbase.zookeeper.quorum

ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30020

Endpoint do LindormTable exibido após clicar em Access by Using HBase Java API na aba Wide Table Engine do console do Lindorm. Para mais informações, consulte Visualizar endpoints.

username

hbase.client.username

testuser

Nome de usuário da instância do Lindorm. O valor padrão é root. Altere a senha no sistema de gerenciamento de cluster do LindormTable. Para mais detalhes, consulte Alterar a senha de um usuário.

password

hbase.client.password

password

Senha da instância do Lindorm. O valor padrão é root.

Conectar ao LindormTable

Configure o cliente para conectar a uma instância do Lindorm usando um dos métodos a seguir.

Método 1: Configuração programática

Crie um objeto Configuration no código:

// Create a Configuration object.
Configuration conf = HBaseConfiguration.create();
// The endpoint of the Lindorm instance. Obtain it from the Database Connection page in the console.
conf.set("hbase.zookeeper.quorum", "host:port");
// The username and password. The default value for both is root.
conf.set("hbase.client.username", "username");
conf.set("hbase.client.password", "password");

Método 2: Arquivo de configuração XML

Adicione as propriedades a seguir ao arquivo de configuração hbase-site.xml:

<configuration>
    <!--
    The endpoint of the Lindorm instance.
    Obtain it from the Database Connection page in the console.
    Note the difference between public and VPC endpoints.
    -->
    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>ld-xxxx-proxy-hbaseue.lindormue.xxx.rds.aliyuncs.com:30020</value>
    </property>
    <!--
    The username and password. The default value for both is root.
    -->
    <property>
        <name>hbase.client.username</name>
        <value>testuser</value>
    </property>
    <property>
        <name>hbase.client.password</name>
        <value>password</value>
    </property>
</configuration>

Criar uma conexão

Crie uma conexão com o banco de dados:

Connection connection = ConnectionFactory.createConnection(conf);
Nota

Crie o objeto Connection apenas uma vez durante o ciclo de vida do programa. Esse objeto é thread-safe e pode ser compartilhado entre todas as threads. Feche o objeto Connection ao encerrar o programa para evitar vazamentos de conexão. Use uma instrução try-finally para garantir o fechamento da conexão.

Operações DDL

Use a interface Admin para criar, desabilitar, truncar e excluir tabelas.

try (Admin admin = connection.getAdmin()) {
    // Create a table with a single region.
    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("tablename"));
    htd.addFamily(new HColumnDescriptor(Bytes.toBytes("family")));
    admin.createTable(htd);

    // Create a table with multiple pre-split regions.
    // A single region limits parallel processing and load balancing across the cluster.
    // Pre-split the table based on data characteristics to avoid performance bottlenecks
    // and data hot spots.
    // The following example creates two regions: [-inf, 10) and [10, inf).
    // int numRegions = 2;
    // byte[][] splitKeys = new byte[numRegions - 1][];
    // for (int i = 1; i < numRegions; i++) {
    //     splitKeys[i - 1] = new byte[]{(byte)(i * 10)};
    // }
    // admin.createTable(htd, splitKeys);

    // Disable the table.
    admin.disableTable(TableName.valueOf("tablename"));

    // Truncate the table. The table must be disabled before truncation.
    admin.truncateTable(TableName.valueOf("tablename"), true);

    // Delete the table. The table must be disabled before deletion.
    admin.deleteTable(TableName.valueOf("tablename"));
}

Operações DML

Use a interface Table para inserir, ler, excluir e escanear dados.

Nota

O objeto Table não é thread-safe. Cada thread deve obter seu próprio objeto Table da Connection.

try (Table table = connection.getTable(TableName.valueOf("tablename"))) {
    // Insert data.
    Put put = new Put(Bytes.toBytes("row"));
    put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value"));
    table.put(put);

    // Read a single row.
    Get get = new Get(Bytes.toBytes("row"));
    Result res = table.get(get);

    // Delete a row.
    Delete delete = new Delete(Bytes.toBytes("row"));
    table.delete(delete);

    // Scan a range of data.
    Scan scan = new Scan(Bytes.toBytes("startRow"), Bytes.toBytes("endRow"));
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        // Process the result.
    }
    scanner.close();
}