建立連線物件
如要使用 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);