创建连接对象

如需使用 Java 版 Cloud Bigtable HBase 客户端连接到 Bigtable,您需要设置配置属性,然后创建 Connection 对象。创建 Connection 对象是一项开销较大的操作,因此请尽量少创建此类对象:

  • 如果您使用复制功能,或者使用多个应用配置文件来识别进入实例的不同类型流量,请针对每个应用配置文件创建一个 Connection 对象,并在应用中的各线程之间共享这些对象。
  • 如果您不使用复制功能或多个应用配置文件,请创建单个 Connection 对象,并在应用中的各线程之间共享该对象。

您可以通过多种方式指定 Connection 对象的配置设置:

  • 将相关设置加入您的代码中。如果您的应用使用多个应用配置文件(例如,应用执行多种不同功能,并且不同的功能分别使用不同的应用配置文件),则您必须使用此选项。

    如果您希望将配置设置保留在应用代码中,或者将外部配置文件作为资源包含在您的应用中并不可行,也可以使用此选项。

  • 使用 hbase-site.xml 文件存储设置。如果您的应用同时使用了 HBase 和 Bigtable,或者您希望将 Bigtable 配置设置保存在一个独立文件中,请使用此选项。

以下部分介绍了配置和创建 Connection 对象的各种方法。

将设置加入您的代码中

以下示例展示了如何在您自己的应用中创建 Connection 对象。将 [VALUES_IN_BRACKETS] 替换为适合您的实例的值:

package com.example.helloworld;

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Connection;

public class BigtableHelper {
  private static final String PROJECT_ID = "[YOUR_PROJECT_ID]";
  private static final String INSTANCE_ID = "[YOUR_INSTANCE_ID]";
  // Include the following line if you are using app profiles.
  // If you do not include the following line, the connection uses the
  // default app profile.
  private static final STRING APP_PROFILE_ID = "[YOUR_APP_PROFILE_ID]";

  private static Connection connection = null;

  public static void connect() throws IOException {
    Configuration config = BigtableConfiguration.configure(PROJECT_ID, INSTANCE_ID);
    // Include the following line if you are using app profiles.
    // If you do not include the following line, the connection uses the
    // default app profile.
    config.set(BigtableOptionsFactory.APP_PROFILE_ID_KEY, APP_PROFILE_ID);

    connection = BigtableConfiguration.connect(config);
  }
}

使用 hbase-site.xml 文件

本部分介绍了如何通过将配置设置加入 hbase-site.xml 文件中来创建 Connection 对象。

示例代码

以下示例展示了如何在您自己的应用中配置和创建 Connection 对象:

package com.example.helloworld;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class BigtableHelper {
  private static Connection connection = null;

  public static void connect() throws IOException {
    Configuration config = HBaseConfiguration.create();
    connection = ConnectionFactory.createConnection(config);
  }
}

JAR 文件的资源中包含一个单独的 hbase-site.xml 文件。当您创建 Configuration 对象时,Java 版 Cloud Bigtable HBase 客户端会自动读取此 hbase-site.xml 文件中的设置。

以下部分介绍了如何在您的应用中实现这种方法。

创建 hbase-site.xml 文件

在项目的 src/main/resources 目录中,创建一个名为 hbase-site.xml 的文件。此文件应包含以下示例中所示的所有属性。将 [VALUES_IN_BRACKETS] 替换为适合您的实例的值:

<configuration>
  <property>
    <name>hbase.client.connection.impl</name>
    <value>com.google.cloud.bigtable.hbase1_x.BigtableConnection</value>
  </property>
  <property>
    <name>google.bigtable.project.id</name>
    <value>[YOUR_PROJECT_ID]</value>
  </property>
  <property>
    <name>google.bigtable.instance.id</name>
    <value>[YOUR_INSTANCE_ID]</value>
  </property>
  <!--
    Include the following property if you are using app profiles.
    If you do not include the following property, the connection uses the
    default app profile.
  -->
  <property>
    <name>google.bigtable.app_profile.id</name>
    <value>[YOUR_APP_PROFILE_ID]</value>
  </property>
</configuration>

将 hbase-site.xml 添加到 JAR 文件中

创建 hbase-site.xml 文件后,您将需要更新您的构建脚本,才能将 src/main/resources 目录纳入项目的 JAR 文件中。

如果您使用的是 Maven,请修改 pom.xml 文件的 <build> 元素,以将资源复制到您的 JAR 文件中:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

创建 Connection 对象

现在,您可以更新代码来创建 Configuration 对象。当您创建此对象时,Java 版 Cloud Bigtable HBase 客户端会自动读取此 hbase-site.xml 文件中的设置。然后,您可以使用这些设置来创建 Connection 对象:

Configuration config = HBaseConfiguration.create();
connection = ConnectionFactory.createConnection(config);

后续步骤