Create a connection object

To connect to Bigtable using the Cloud Bigtable HBase clientfor Java, you'll need to set configuration properties, then create aConnection object. Creating aConnection object isa heavyweight operation, so create as few of these objects as possible:

  • If you usereplication, or if you useappprofiles to identify different types of traffic to your instance,create oneConnection object per app profile and share the objects amongthreads in your application.
  • If you don't use replication or app profiles, create a singleConnectionobject and share it among threads in your application.

There are a few ways to specify the configuration settings for aConnectionobject:

  • Include the settings in your code. If your application uses multipleappprofiles (for example, if it performs several differentfunctions, with a separate app profile for each function), you must use thisoption.

    You can also use this option if you prefer to keep configuration settings inyour application's code, or if it's not practical to include an externalconfiguration file as a resource in your application.

  • Use anhbase-site.xml file to store settings. Use this option if yourapplication uses both HBase and Bigtable, or if you prefer to keepyour Bigtable configuration settings in a standalone file.

Note: The Cloud Bigtable HBase client for Java is for customers who are migratingto Bigtable from HBase and want to continue using the HBase API. In all other cases,Java developers should instead use theCloud Bigtable clientfor Java, which calls the Bigtable APIs.

The following sections explain each way to configure and create aConnectionobject.

Including settings in your code

The following example shows how you might create aConnection object in yourown application. Replace[VALUES_IN_BRACKETS] with the correct values for yourinstance:

packagecom.example.helloworld;importcom.google.cloud.bigtable.hbase.BigtableConfiguration;importcom.google.cloud.bigtable.hbase.BigtableOptionsFactory;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.client.Connection;publicclassBigtableHelper{privatestaticfinalStringPROJECT_ID="[YOUR_PROJECT_ID]";privatestaticfinalStringINSTANCE_ID="[YOUR_INSTANCE_ID]";//Includethefollowinglineifyouareusingappprofiles.//Ifyoudonotincludethefollowingline,theconnectionusesthe//defaultappprofile.privatestaticfinalSTRINGAPP_PROFILE_ID="[YOUR_APP_PROFILE_ID]";privatestaticConnectionconnection=null;publicstaticvoidconnect()throwsIOException{Configurationconfig=BigtableConfiguration.configure(PROJECT_ID,INSTANCE_ID);//Includethefollowinglineifyouareusingappprofiles.//Ifyoudonotincludethefollowingline,theconnectionusesthe//defaultappprofile.config.set(BigtableOptionsFactory.APP_PROFILE_ID_KEY,APP_PROFILE_ID);connection=BigtableConfiguration.connect(config);}}

Using an hbase-site.xml file

This section explains how to create aConnection object by including theconfiguration settings in anhbase-site.xml file.

Sample code

The following example shows how you might configure and create aConnectionobject in your own application:

packagecom.example.helloworld;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;publicclassBigtableHelper{privatestaticConnectionconnection=null;publicstaticvoidconnect()throwsIOException{Configurationconfig=HBaseConfiguration.create();connection=ConnectionFactory.createConnection(config);}}

A separatehbase-site.xml file is included in the JAR file's resources. Whenyou create theConfiguration object, the Cloud Bigtable HBase clientfor Java automatically reads the settings from thehbase-site.xml file.

The following sections explain how to implement this method in your application.

Creating the hbase-site.xml file

In your project'ssrc/main/resources directory, create a file calledhbase-site.xml. The file should contain all of the properties shown in thefollowing example. Replace[VALUES_IN_BRACKETS] with the correct values foryour instance:

<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><!--Includethefollowingpropertyifyouareusingappprofiles.Ifyoudonotincludethefollowingproperty,theconnectionusesthedefaultappprofile.--><property><name>google.bigtable.app_profile.id</name><value>[YOUR_APP_PROFILE_ID]</value></property></configuration>

Adding hbase-site.xml to the JAR file

After you create thehbase-site.xml file, you'll need to update your buildscript to include thesrc/main/resources directory in your project's JAR file.

If you're using Maven, edit the<build> element of yourpom.xml file tocopy the resources to your JAR file:

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

Creating the Connection object

Now you can update your code to create aConfiguration object. When you createthis object, the Cloud Bigtable HBase client for Java automaticallyreads the settings from thehbase-site.xml file. You can then use thesesettings to create aConnection object:

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

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.