# Example: HBase APIs for Java "Hello World" Application

This example is a very simple "hello world" application, using the Cloud Bigtable HBase client library for Java, that illustrates how to:

• Connect to a Cloud Bigtable instance.
• Create a new table.
• Write data to the table.
• Delete the table.

## Running the sample

The sample uses the HBase APIs to communicate with Cloud Bigtable. The code for this sample is in the GitHub repository GoogleCloudPlatform/cloud-bigtable-examples, in the directory `java/hello-world`.

To run this sample program, follow the instructions for the sample on GitHub.

## Using the HBase APIs

The sample application connects to Cloud Bigtable and demonstrates some simple operations.

### Installing and importing the client library

This sample uses the Cloud Bigtable HBase client for Java, as well as Maven. See the instructions for using the client library.

The sample uses the following imports:

``````import com.google.cloud.bigtable.hbase.BigtableConfiguration;

import java.io.IOException;``````

### Connecting to Cloud Bigtable

Connect to Cloud Bigtable using the `BigtableConfiguration` class.

``````// Create the Bigtable connection, use try-with-resources to make sure it gets closed
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {

// The admin API lets us create, manage and delete tables

### Creating a table

Use the Admin API to create a table.

``````// Create a table with a single column family
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));

print("Create table " + descriptor.getNameAsString());

### Writing rows to a table

Use the `Table` class to put rows to the table.

``````// Retrieve the table we just created so we can do some reads and writes
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));

// Write some rows to the table
print("Write some greetings to the table");
for (int i = 0; i < GREETINGS.length; i++) {
// Each row has a unique row key.
//
// Note: This example uses sequential numeric IDs for simplicity, but
// this can result in poor performance in a production application.
// Since rows are stored in sorted order by key, sequential keys can
// result in poor distribution of operations across nodes.
//
// best performance, see the documentation:
//
String rowKey = "greeting" + i;

// Put a single row into the table. We could also pass a list of Puts to write a batch.
Put put = new Put(Bytes.toBytes(rowKey));
table.put(put);
}``````

### Reading a row by its key

Get a row directly using its key.

``````// Get the first greeting by row key
String rowKey = "greeting0";
Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
System.out.println("Get a single greeting by row key");
System.out.printf("\t%s = %s\n", rowKey, greeting);``````

### Scanning all table rows

Use the `Scan` class to get a range of rows.

``````// Now scan across all rows.
Scan scan = new Scan();

print("Scan for all greetings:");
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
System.out.println('\t' + Bytes.toString(valueBytes));
}``````

### Deleting a table

Delete a table using the Admin API.

``````// Clean up by disabling and then deleting the table
print("Delete the table");

## Putting it all together

Here is the full example without comments.

``````package com.example.cloud.bigtable.helloworld;

import java.io.IOException;

public class HelloWorld {

private static final byte[] TABLE_NAME = Bytes.toBytes("Hello-Bigtable");
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf1");
private static final byte[] COLUMN_NAME = Bytes.toBytes("greeting");

private static final String[] GREETINGS = {
"Hello World!", "Hello Cloud Bigtable!", "Hello HBase!"
};

private static void doHelloWorld(String projectId, String instanceId) {

try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {

try {
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));

print("Create table " + descriptor.getNameAsString());

Table table = connection.getTable(TableName.valueOf(TABLE_NAME));

print("Write some greetings to the table");
for (int i = 0; i < GREETINGS.length; i++) {
String rowKey = "greeting" + i;

Put put = new Put(Bytes.toBytes(rowKey));
table.put(put);
}

String rowKey = "greeting0";
Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
System.out.println("Get a single greeting by row key");
System.out.printf("\t%s = %s\n", rowKey, greeting);

Scan scan = new Scan();

print("Scan for all greetings:");
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
System.out.println('\t' + Bytes.toString(valueBytes));
}

print("Delete the table");
} catch (IOException e) {
print("Cleaning up table");
}
throw e;
}
} catch (IOException e) {
System.err.println("Exception while running HelloWorld: " + e.getMessage());
e.printStackTrace();

System.exit(1);
}

System.exit(0);
}

private static void print(String msg) {
System.out.println("HelloWorld: " + msg);
}

public static void main(String[] args) {
String projectId = requiredProperty("bigtable.projectID");
String instanceId = requiredProperty("bigtable.instanceID");

doHelloWorld(projectId, instanceId);
}

private static String requiredProperty(String prop) {
String value = System.getProperty(prop);
if (value == null) {
throw new IllegalArgumentException("Missing required system property: " + prop);
}
return value;
}
}``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7754738,"math_prob":0.70272064,"size":1353,"snap":"2020-10-2020-16","text_gpt3_token_len":295,"char_repetition_ratio":0.17346182,"word_repetition_ratio":0.008928572,"special_character_ratio":0.19512194,"punctuation_ratio":0.09920635,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95802665,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-29T01:22:16Z\",\"WARC-Record-ID\":\"<urn:uuid:e10660c3-f198-4648-8ee3-216e7dc83971>\",\"Content-Length\":\"277708\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:48a53467-8d76-43f0-9be0-d19dfc1d9da8>\",\"WARC-Concurrent-To\":\"<urn:uuid:b8c56ff5-5db6-49ed-aa2b-76d99950cf26>\",\"WARC-IP-Address\":\"172.217.164.142\",\"WARC-Target-URI\":\"https://cloud.google.com/bigtable/docs/samples-hbase-java-hello?hl=hi\",\"WARC-Payload-Digest\":\"sha1:JEVUPZ7PJMOZBEXEL5LEDRF6U42QX7U3\",\"WARC-Block-Digest\":\"sha1:ZQT3YUAR45KQWZZB3X3FIBVC4K6YP3TY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875148163.71_warc_CC-MAIN-20200228231614-20200229021614-00471.warc.gz\"}"}