Skip to content

Commit 4b5bd42

Browse files
committed
Add a simple java Hello World application using HBase API
0 parents  commit 4b5bd42

File tree

1 file changed

+131
-0
lines changed
  • bigtable/hbase/snippets/src/main/java/com/example/cloud/bigtable/helloworld

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
23+
24+
import org.apache.hadoop.hbase.HColumnDescriptor;
25+
import org.apache.hadoop.hbase.HTableDescriptor;
26+
import org.apache.hadoop.hbase.TableName;
27+
import org.apache.hadoop.hbase.client.Admin;
28+
import org.apache.hadoop.hbase.client.Connection;
29+
import org.apache.hadoop.hbase.client.Get;
30+
import org.apache.hadoop.hbase.client.Put;
31+
import org.apache.hadoop.hbase.client.Result;
32+
import org.apache.hadoop.hbase.client.ResultScanner;
33+
import org.apache.hadoop.hbase.client.Scan;
34+
import org.apache.hadoop.hbase.client.Table;
35+
import org.apache.hadoop.hbase.util.Bytes;
36+
37+
import java.io.IOException;
38+
39+
/**
40+
* A minimal application that connects to Cloud Bigtable using the native HBase API
41+
* and performs some basic operations.
42+
*/
43+
public class HelloWorld {
44+
45+
// Refer to table metadata names by byte array in the HBase API
46+
private static final byte[] TABLE_NAME = Bytes.toBytes("Hello-Bigtable");
47+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf1");
48+
private static final byte[] COLUMN_NAME = Bytes.toBytes("greeting");
49+
50+
// Write some friendly greetings to Cloud Bigtable
51+
private static final String[] GREETINGS =
52+
{ "Hello World!", "Hello Cloud Bigtable!", "Hello HBase!" };
53+
54+
/**
55+
* Connects to Cloud Bigtable, runs some basic operations and prints the results
56+
*/
57+
private static void doHelloWorld(String projectId, String zone, String clusterId) {
58+
59+
// Create the Bigtable connection, use try-with-resources to make sure it gets closed
60+
try (Connection connection = BigtableConfiguration.connect(projectId, zone, clusterId)) {
61+
62+
// The admin API lets us create, manage and delete tables
63+
Admin admin = connection.getAdmin();
64+
65+
// Create a table with a single column family
66+
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
67+
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
68+
69+
print("Create table " + descriptor.getNameAsString());
70+
admin.createTable(descriptor);
71+
72+
// Retrieve the table we just created so we can do some reads and writes
73+
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
74+
75+
// Write some rows to the table
76+
print("Write some greetings to the table");
77+
for (int i = 0; i < GREETINGS.length; i++) {
78+
// Each row has a unique row key
79+
String rowKey = "greeting" + i;
80+
81+
// Put a single row into the table. We could also pass a list of Puts to write a batch.
82+
Put put = new Put(Bytes.toBytes(rowKey));
83+
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(GREETINGS[i]));
84+
table.put(put);
85+
}
86+
87+
// Get the first greeting by row key
88+
String rowKey = "greeting0";
89+
Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
90+
String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
91+
System.out.println(
92+
String.format("Get a single greeting by row key: %s = %s", rowKey, greeting));
93+
94+
// Now scan across all rows.
95+
Scan scan = new Scan();
96+
97+
print("Scan for all greetings:");
98+
ResultScanner scanner = table.getScanner(scan);
99+
for (Result row : scanner) {
100+
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
101+
System.out.println('\t' + Bytes.toString(valueBytes));
102+
}
103+
104+
// Clean up by disabling and then deleting the table
105+
print("Delete the table");
106+
admin.disableTable(table.getName());
107+
admin.deleteTable(table.getName());
108+
109+
} catch (IOException e) {
110+
System.err.println("Exception while running HelloWorld: " + e.getMessage());
111+
e.printStackTrace();
112+
System.exit(1);
113+
}
114+
115+
System.exit(0);
116+
}
117+
118+
private static void print(String msg) {
119+
System.out.println("HelloWorld: " + msg);
120+
}
121+
122+
public static void main(String[] args) {
123+
if (args.length < 3) {
124+
System.err.println("Usage: HelloWorld <projectId> <zone> <clusterId>");
125+
System.exit(1);
126+
}
127+
128+
doHelloWorld(args[0], args[1], args[2]);
129+
}
130+
131+
}

0 commit comments

Comments
 (0)