19
19
*/
20
20
package com .example .cloud .bigtable .helloworld ;
21
21
22
+ // [START bigtable_hw_imports]
22
23
import com .google .cloud .bigtable .hbase .BigtableConfiguration ;
23
24
24
25
import org .apache .hadoop .hbase .HColumnDescriptor ;
35
36
import org .apache .hadoop .hbase .util .Bytes ;
36
37
37
38
import java .io .IOException ;
39
+ // [END bigtable_hw_imports]
38
40
39
41
/**
40
- * A minimal application that connects to Cloud Bigtable using the native HBase API
41
- * and performs some basic operations.
42
+ * A minimal application that connects to Cloud Bigtable using the native HBase API and performs
43
+ * some basic operations.
42
44
*/
43
45
public class HelloWorld {
44
46
@@ -48,32 +50,31 @@ public class HelloWorld {
48
50
private static final byte [] COLUMN_NAME = Bytes .toBytes ("greeting" );
49
51
50
52
// Write some friendly greetings to Cloud Bigtable
51
- private static final String [] GREETINGS =
52
- { "Hello World!" , "Hello Cloud Bigtable!" , "Hello HBase!" };
53
+ private static final String [] GREETINGS = {
54
+ "Hello World!" , "Hello Cloud Bigtable!" , "Hello HBase!"
55
+ };
53
56
54
- /**
55
- * Connects to Cloud Bigtable, runs some basic operations and prints the results.
56
- */
57
+ /** Connects to Cloud Bigtable, runs some basic operations and prints the results. */
57
58
private static void doHelloWorld (String projectId , String instanceId ) {
58
59
59
- // [START connecting_to_bigtable ]
60
+ // [START bigtable_hw_connect ]
60
61
// Create the Bigtable connection, use try-with-resources to make sure it gets closed
61
62
try (Connection connection = BigtableConfiguration .connect (projectId , instanceId )) {
62
63
63
64
// The admin API lets us create, manage and delete tables
64
65
Admin admin = connection .getAdmin ();
65
- // [END connecting_to_bigtable ]
66
+ // [END bigtable_hw_connect ]
66
67
67
- // [START creating_a_table ]
68
+ // [START bigtable_hw_create_table ]
68
69
// Create a table with a single column family
69
70
HTableDescriptor descriptor = new HTableDescriptor (TableName .valueOf (TABLE_NAME ));
70
71
descriptor .addFamily (new HColumnDescriptor (COLUMN_FAMILY_NAME ));
71
72
72
73
print ("Create table " + descriptor .getNameAsString ());
73
74
admin .createTable (descriptor );
74
- // [END creating_a_table ]
75
+ // [END bigtable_hw_create_table ]
75
76
76
- // [START writing_rows ]
77
+ // [START bigtable_hw_write_rows ]
77
78
// Retrieve the table we just created so we can do some reads and writes
78
79
Table table = connection .getTable (TableName .valueOf (TABLE_NAME ));
79
80
@@ -98,18 +99,18 @@ private static void doHelloWorld(String projectId, String instanceId) {
98
99
put .addColumn (COLUMN_FAMILY_NAME , COLUMN_NAME , Bytes .toBytes (GREETINGS [i ]));
99
100
table .put (put );
100
101
}
101
- // [END writing_rows ]
102
+ // [END bigtable_hw_write_rows ]
102
103
103
- // [START getting_a_row ]
104
+ // [START bigtable_hw_get_by_key ]
104
105
// Get the first greeting by row key
105
106
String rowKey = "greeting0" ;
106
107
Result getResult = table .get (new Get (Bytes .toBytes (rowKey )));
107
108
String greeting = Bytes .toString (getResult .getValue (COLUMN_FAMILY_NAME , COLUMN_NAME ));
108
109
System .out .println ("Get a single greeting by row key" );
109
110
System .out .printf ("\t %s = %s\n " , rowKey , greeting );
110
- // [END getting_a_row ]
111
+ // [END bigtable_hw_get_by_key ]
111
112
112
- // [START scanning_all_rows ]
113
+ // [START bigtable_hw_scan_all ]
113
114
// Now scan across all rows.
114
115
Scan scan = new Scan ();
115
116
@@ -119,14 +120,14 @@ private static void doHelloWorld(String projectId, String instanceId) {
119
120
byte [] valueBytes = row .getValue (COLUMN_FAMILY_NAME , COLUMN_NAME );
120
121
System .out .println ('\t' + Bytes .toString (valueBytes ));
121
122
}
122
- // [END scanning_all_rows ]
123
+ // [END bigtable_hw_scan_all ]
123
124
124
- // [START deleting_a_table ]
125
+ // [START bigtable_hw_delete_table ]
125
126
// Clean up by disabling and then deleting the table
126
127
print ("Delete the table" );
127
128
admin .disableTable (table .getName ());
128
129
admin .deleteTable (table .getName ());
129
- // [END deleting_a_table ]
130
+ // [END bigtable_hw_delete_table ]
130
131
131
132
} catch (IOException e ) {
132
133
System .err .println ("Exception while running HelloWorld: " + e .getMessage ());
@@ -156,5 +157,4 @@ private static String requiredProperty(String prop) {
156
157
}
157
158
return value ;
158
159
}
159
-
160
160
}
0 commit comments