Connecting to Cloud Bigtable

To connect to Cloud Bigtable, you'll need to set several configuration properties, then create a Connection object. Creating a Connection object is a heavyweight operation; you should create a single Connection and share it among threads in your application.

There are two ways to specify the configuration settings for a Connection:

  • Include the settings in your code. Use this option if you prefer to keep configuration settings in your application's code, or if it's not practical to include an external configuration file as a resource in your application.
  • Use an hbase-site.xml file to store settings. Use this option if your application uses both HBase and Cloud Bigtable, or if you prefer to keep your Cloud Bigtable configuration settings in a standalone file.

The following sections explain each way to configure and create a Connection.

Including settings in your code

The following example shows how you might create a Connection object in your own application:

package com.example.helloworld;

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import java.io.IOException;
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";

  private static Connection connection = null;

  public static void connect() throws IOException {
    connection = BigtableConfiguration.connect(PROJECT_ID, INSTANCE_ID);
  }
}

Using an hbase-site.xml file

This section explains how to create a Connection by including the configuration settings in an hbase-site.xml file.

Sample code

The following example shows how you might configure and create a Connection object in your own application:

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);
  }
}

A separate hbase-site.xml file is included in the JAR file's resources. When you create the Configuration object, the Cloud Bigtable HBase client for Java automatically reads the settings from the hbase-site.xml file.

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

Creating the hbase-site.xml file

In your project's src/main/resources directory, create a file called hbase-site.xml. The file should contain all of the properties shown in the following example. Replace [VALUES_IN_BRACKETS] with the correct values for your cluster:

<configuration>
  <property>
    <name>hbase.client.connection.impl</name>
    <value>com.google.cloud.bigtable.hbase1_2.BigtableConnection</value>
  </property>
  <property>
    <name>google.bigtable.instance.id</name>
    <value>[YOUR-INSTANCE-ID]</value>
  </property>
  <property>
    <name>google.bigtable.project.id</name>
    <value>[YOUR-PROJECT-ID]</value>
  </property>
</configuration>

Adding hbase-site.xml to the JAR file

Once you've created the hbase-site.xml file, you'll need to update your build script to include the src/main/resources directory in your project's JAR file.

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

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

Creating the Connection object

Once you've set up the hbase-site.xml file and added it to your build script, it's simple to create a Connection object:

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

When you create the Configuration object, the Cloud Bigtable HBase client for Java automatically reads the settings from the hbase-site.xml file. You can then use these settings to create a Connection.

Send feedback about...

Cloud Bigtable Documentation