Skip to content

Commit 227dd88

Browse files
committed
Merge remote-tracking branch 'dpe/main' into cbt-examples-java/hello-world
2 parents ab20245 + 508ae9d commit 227dd88

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

  • bigtable/hbase/snippets/src/main/java/com/example/cloud/bigtable/helloworld
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package com.example.cloud.bigtable.helloworld;
21+
22+
// [START bigtable_hw_imports_hbase]
23+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
24+
25+
import org.apache.hadoop.hbase.HColumnDescriptor;
26+
import org.apache.hadoop.hbase.HTableDescriptor;
27+
import org.apache.hadoop.hbase.TableName;
28+
import org.apache.hadoop.hbase.client.Admin;
29+
import org.apache.hadoop.hbase.client.Connection;
30+
import org.apache.hadoop.hbase.client.Get;
31+
import org.apache.hadoop.hbase.client.Put;
32+
import org.apache.hadoop.hbase.client.Result;
33+
import org.apache.hadoop.hbase.client.ResultScanner;
34+
import org.apache.hadoop.hbase.client.Scan;
35+
import org.apache.hadoop.hbase.client.Table;
36+
import org.apache.hadoop.hbase.util.Bytes;
37+
38+
import java.io.IOException;
39+
// [END bigtable_hw_imports_hbase]
40+
41+
/**
42+
* A minimal application that connects to Cloud Bigtable using the native HBase API and performs
43+
* some basic operations.
44+
*/
45+
public class HelloWorld {
46+
47+
// Refer to table metadata names by byte array in the HBase API
48+
private static final byte[] TABLE_NAME = Bytes.toBytes("Hello-Bigtable");
49+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf1");
50+
private static final byte[] COLUMN_NAME = Bytes.toBytes("greeting");
51+
52+
// Write some friendly greetings to Cloud Bigtable
53+
private static final String[] GREETINGS = {
54+
"Hello World!", "Hello Cloud Bigtable!", "Hello HBase!"
55+
};
56+
57+
/** Connects to Cloud Bigtable, runs some basic operations and prints the results. */
58+
private static void doHelloWorld(String projectId, String instanceId) {
59+
60+
// [START bigtable_hw_connect_hbase]
61+
// Create the Bigtable connection, use try-with-resources to make sure it gets closed
62+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
63+
64+
// The admin API lets us create, manage and delete tables
65+
Admin admin = connection.getAdmin();
66+
// [END bigtable_hw_connect_hbase]
67+
68+
try {
69+
// [START bigtable_hw_create_table_hbase]
70+
// Create a table with a single column family
71+
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
72+
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
73+
74+
print("Create table " + descriptor.getNameAsString());
75+
admin.createTable(descriptor);
76+
// [END bigtable_hw_create_table_hbase]
77+
78+
// [START bigtable_hw_write_rows_hbase]
79+
// Retrieve the table we just created so we can do some reads and writes
80+
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
81+
82+
// Write some rows to the table
83+
print("Write some greetings to the table");
84+
for (int i = 0; i < GREETINGS.length; i++) {
85+
// Each row has a unique row key.
86+
//
87+
// Note: This example uses sequential numeric IDs for simplicity, but
88+
// this can result in poor performance in a production application.
89+
// Since rows are stored in sorted order by key, sequential keys can
90+
// result in poor distribution of operations across nodes.
91+
//
92+
// For more information about how to design a Bigtable schema for the
93+
// best performance, see the documentation:
94+
//
95+
// https://cloud.google.com/bigtable/docs/schema-design
96+
String rowKey = "greeting" + i;
97+
98+
// Put a single row into the table. We could also pass a list of Puts to write a batch.
99+
Put put = new Put(Bytes.toBytes(rowKey));
100+
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(GREETINGS[i]));
101+
table.put(put);
102+
}
103+
// [END bigtable_hw_write_rows_hbase]
104+
105+
// [START bigtable_hw_get_by_key_hbase]
106+
// Get the first greeting by row key
107+
String rowKey = "greeting0";
108+
Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
109+
String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
110+
System.out.println("Get a single greeting by row key");
111+
System.out.printf("\t%s = %s\n", rowKey, greeting);
112+
// [END bigtable_hw_get_by_key_hbase]
113+
114+
// [START bigtable_hw_scan_all_hbase]
115+
// Now scan across all rows.
116+
Scan scan = new Scan();
117+
118+
print("Scan for all greetings:");
119+
ResultScanner scanner = table.getScanner(scan);
120+
for (Result row : scanner) {
121+
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
122+
System.out.println('\t' + Bytes.toString(valueBytes));
123+
}
124+
// [END bigtable_hw_scan_all_hbase]
125+
126+
// [START bigtable_hw_delete_table_hbase]
127+
// Clean up by disabling and then deleting the table
128+
print("Delete the table");
129+
admin.disableTable(table.getName());
130+
admin.deleteTable(table.getName());
131+
// [END bigtable_hw_delete_table_hbase]
132+
} catch (IOException e) {
133+
if (admin.tableExists(TableName.valueOf(TABLE_NAME))) {
134+
print("Cleaning up table");
135+
admin.disableTable(TableName.valueOf(TABLE_NAME));
136+
admin.deleteTable(TableName.valueOf(TABLE_NAME));
137+
}
138+
throw e;
139+
}
140+
} catch (IOException e) {
141+
System.err.println("Exception while running HelloWorld: " + e.getMessage());
142+
e.printStackTrace();
143+
144+
System.exit(1);
145+
}
146+
147+
System.exit(0);
148+
}
149+
150+
private static void print(String msg) {
151+
System.out.println("HelloWorld: " + msg);
152+
}
153+
154+
public static void main(String[] args) {
155+
// Consult system properties to get project/instance
156+
String projectId = requiredProperty("bigtable.projectID");
157+
String instanceId = requiredProperty("bigtable.instanceID");
158+
159+
doHelloWorld(projectId, instanceId);
160+
}
161+
162+
private static String requiredProperty(String prop) {
163+
String value = System.getProperty(prop);
164+
if (value == null) {
165+
throw new IllegalArgumentException("Missing required system property: " + prop);
166+
}
167+
return value;
168+
}
169+
}

0 commit comments

Comments
 (0)