[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eA \u003ccode\u003eConnection\u003c/code\u003e object is needed to connect to Bigtable using the Cloud Bigtable HBase client for Java, and it's recommended to create as few of these objects as possible due to their heavyweight nature.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure settings for a \u003ccode\u003eConnection\u003c/code\u003e object either directly within your application's code, or by storing the configuration in an external \u003ccode\u003ehbase-site.xml\u003c/code\u003e file, with the latter being suggested for applications using both HBase and Bigtable.\u003c/p\u003e\n"],["\u003cp\u003eIf your application uses multiple app profiles or replication, you should create one \u003ccode\u003eConnection\u003c/code\u003e object per app profile and share it among threads, while a single shared \u003ccode\u003eConnection\u003c/code\u003e object is sufficient if these are not used.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ehbase-site.xml\u003c/code\u003e file, when used, should be placed in the \u003ccode\u003esrc/main/resources\u003c/code\u003e directory of your project and include properties such as project ID, instance ID, and optionally, the app profile ID, while also ensuring it is included in the JAR file.\u003c/p\u003e\n"],["\u003cp\u003eWhile this client is primarily for those migrating from HBase, Java developers starting fresh with Bigtable should instead use the Cloud Bigtable client for Java, as it directly calls the Bigtable APIs.\u003c/p\u003e\n"]]],[],null,["Create a connection object\n\nTo connect to Bigtable using the Cloud Bigtable HBase client\nfor Java, you'll need to set configuration properties, then create a\n[`Connection`](https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Connection.html) object. Creating a `Connection` object is\na heavyweight operation, so create as few of these objects as possible:\n\n- If you use [replication](/bigtable/docs/replication-overview), or if you use [app\n profiles](/bigtable/docs/app-profiles) to identify different types of traffic to your instance, create one `Connection` object per app profile and share the objects among threads in your application.\n- If you don't use replication or app profiles, create a single `Connection` object and share it among threads in your application.\n\nThere are a few ways to specify the configuration settings for a `Connection`\nobject:\n\n- **Include the settings in your code.** If your application uses multiple [app\n profiles](/bigtable/docs/app-profiles) (for example, if it performs several different\n functions, with a separate app profile for each function), you must use this\n option.\n\n You can also use this option if you prefer to keep configuration settings in\n your application's code, or if it's not practical to include an external\n configuration file as a resource in your application.\n- **Use an `hbase-site.xml` file to store settings.** Use this option if your\n application uses both HBase and Bigtable, or if you prefer to keep\n your Bigtable configuration settings in a standalone file.\n\n| **Note:** The Cloud Bigtable HBase client for Java is for customers who are migrating to Bigtable from HBase and want to continue using the HBase API. In all other cases, Java developers should instead use the [Cloud Bigtable client\n| for Java](/java/docs/reference/google-cloud-bigtable/latest/overview), which calls the Bigtable APIs.\n\nThe following sections explain each way to configure and create a `Connection`\nobject.\n\nIncluding settings in your code\n\nThe following example shows how you might create a `Connection` object in your\nown application. Replace `[VALUES_IN_BRACKETS]` with the correct values for your\ninstance: \n\n package com.example.helloworld;\n\n import com.google.cloud.bigtable.hbase.BigtableConfiguration;\n import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;\n import java.io.IOException;\n import org.apache.hadoop.conf.Configuration;\n import org.apache.hadoop.hbase.client.Connection;\n\n public class BigtableHelper {\n private static final String PROJECT_ID = \"[YOUR_PROJECT_ID]\";\n private static final String INSTANCE_ID = \"[YOUR_INSTANCE_ID]\";\n // Include the following line if you are using app profiles.\n // If you do not include the following line, the connection uses the\n // default app profile.\n private static final STRING APP_PROFILE_ID = \"[YOUR_APP_PROFILE_ID]\";\n\n private static Connection connection = null;\n\n public static void connect() throws IOException {\n Configuration config = BigtableConfiguration.configure(PROJECT_ID, INSTANCE_ID);\n // Include the following line if you are using app profiles.\n // If you do not include the following line, the connection uses the\n // default app profile.\n config.set(BigtableOptionsFactory.APP_PROFILE_ID_KEY, APP_PROFILE_ID);\n\n connection = BigtableConfiguration.connect(config);\n }\n }\n\nUsing an hbase-site.xml file\n\nThis section explains how to create a `Connection` object by including the\nconfiguration settings in an `hbase-site.xml` file.\n\nSample code\n\nThe following example shows how you might configure and create a `Connection`\nobject in your own application: \n\n package com.example.helloworld;\n\n import java.io.IOException;\n import org.apache.hadoop.conf.Configuration;\n import org.apache.hadoop.hbase.HBaseConfiguration;\n import org.apache.hadoop.hbase.client.Connection;\n import org.apache.hadoop.hbase.client.ConnectionFactory;\n\n public class BigtableHelper {\n private static Connection connection = null;\n\n public static void connect() throws IOException {\n Configuration config = HBaseConfiguration.create();\n connection = ConnectionFactory.createConnection(config);\n }\n }\n\nA separate `hbase-site.xml` file is included in the JAR file's resources. When\nyou create the `Configuration` object, the Cloud Bigtable HBase client\nfor Java automatically reads the settings from the `hbase-site.xml` file.\n\nThe following sections explain how to implement this method in your application.\n\nCreating the hbase-site.xml file\n\nIn your project's `src/main/resources` directory, create a file called\n`hbase-site.xml`. The file should contain all of the properties shown in the\nfollowing example. Replace `[VALUES_IN_BRACKETS]` with the correct values for\nyour instance: \n\n \u003cconfiguration\u003e\n \u003cproperty\u003e\n \u003cname\u003ehbase.client.connection.impl\u003c/name\u003e\n \u003cvalue\u003ecom.google.cloud.bigtable.hbase1_x.BigtableConnection\u003c/value\u003e\n \u003c/property\u003e\n \u003cproperty\u003e\n \u003cname\u003egoogle.bigtable.project.id\u003c/name\u003e\n \u003cvalue\u003e[YOUR_PROJECT_ID]\u003c/value\u003e\n \u003c/property\u003e\n \u003cproperty\u003e\n \u003cname\u003egoogle.bigtable.instance.id\u003c/name\u003e\n \u003cvalue\u003e[YOUR_INSTANCE_ID]\u003c/value\u003e\n \u003c/property\u003e\n \u003c!--\n Include the following property if you are using app profiles.\n If you do not include the following property, the connection uses the\n default app profile.\n --\u003e\n \u003cproperty\u003e\n \u003cname\u003egoogle.bigtable.app_profile.id\u003c/name\u003e\n \u003cvalue\u003e[YOUR_APP_PROFILE_ID]\u003c/value\u003e\n \u003c/property\u003e\n \u003c/configuration\u003e\n\nAdding hbase-site.xml to the JAR file\n\nAfter you create the `hbase-site.xml` file, you'll need to update your build\nscript to include the `src/main/resources` directory in your project's JAR file.\n\nIf you're using Maven, edit the `\u003cbuild\u003e` element of your `pom.xml` file to\ncopy the resources to your JAR file: \n\n \u003cbuild\u003e\n \u003cresources\u003e\n \u003cresource\u003e\n \u003cdirectory\u003esrc/main/resources\u003c/directory\u003e\n \u003cfiltering\u003etrue\u003c/filtering\u003e\n \u003c/resource\u003e\n \u003c/resources\u003e\n \u003c/build\u003e\n\nCreating the Connection object\n\nNow you can update your code to create a `Configuration` object. When you create\nthis object, the Cloud Bigtable HBase client for Java automatically\nreads the settings from the `hbase-site.xml` file. You can then use these\nsettings to create a `Connection` object: \n\n Configuration config = HBaseConfiguration.create();\n connection = ConnectionFactory.createConnection(config);\n\nWhat's next\n\n- [Learn more about the HBase client for Java](/bigtable/docs/hbase-bigtable).\n- [View a \"hello world\" example](/bigtable/docs/samples-java-hello).\n- [Find out how to use the client library](/bigtable/docs/reference/libraries)."]]