Skip to content

Commit 2b721e4

Browse files
Fix table clean up (#332)
* fix table cleanup * whitespace
1 parent ad5746d commit 2b721e4

File tree

1 file changed

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

1 file changed

+72
-68
lines changed

bigtable/hbase/snippets/src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -65,78 +65,82 @@ private static void doHelloWorld(String projectId, String instanceId) {
6565
Admin admin = connection.getAdmin();
6666
// [END bigtable_hw_connect]
6767

68-
// [START bigtable_hw_create_table]
69-
// Create a table with a single column family
70-
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
71-
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
72-
73-
print("Create table " + descriptor.getNameAsString());
74-
admin.createTable(descriptor);
75-
// [END bigtable_hw_create_table]
76-
77-
// [START bigtable_hw_write_rows]
78-
// Retrieve the table we just created so we can do some reads and writes
79-
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
80-
81-
// Write some rows to the table
82-
print("Write some greetings to the table");
83-
for (int i = 0; i < GREETINGS.length; i++) {
84-
// Each row has a unique row key.
85-
//
86-
// Note: This example uses sequential numeric IDs for simplicity, but
87-
// this can result in poor performance in a production application.
88-
// Since rows are stored in sorted order by key, sequential keys can
89-
// result in poor distribution of operations across nodes.
90-
//
91-
// For more information about how to design a Bigtable schema for the
92-
// best performance, see the documentation:
93-
//
94-
// https://cloud.google.com/bigtable/docs/schema-design
95-
String rowKey = "greeting" + i;
96-
97-
// Put a single row into the table. We could also pass a list of Puts to write a batch.
98-
Put put = new Put(Bytes.toBytes(rowKey));
99-
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(GREETINGS[i]));
100-
table.put(put);
101-
}
102-
// [END bigtable_hw_write_rows]
103-
104-
// [START bigtable_hw_get_by_key]
105-
// Get the first greeting by row key
106-
String rowKey = "greeting0";
107-
Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
108-
String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
109-
System.out.println("Get a single greeting by row key");
110-
System.out.printf("\t%s = %s\n", rowKey, greeting);
111-
// [END bigtable_hw_get_by_key]
112-
113-
// [START bigtable_hw_scan_all]
114-
// Now scan across all rows.
115-
Scan scan = new Scan();
116-
117-
print("Scan for all greetings:");
118-
ResultScanner scanner = table.getScanner(scan);
119-
for (Result row : scanner) {
120-
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
121-
System.out.println('\t' + Bytes.toString(valueBytes));
68+
try {
69+
// [START bigtable_hw_create_table]
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]
77+
78+
// [START bigtable_hw_write_rows]
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]
104+
105+
// [START bigtable_hw_get_by_key]
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]
113+
114+
// [START bigtable_hw_scan_all]
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]
125+
126+
// [START bigtable_hw_delete_table]
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]
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;
122139
}
123-
// [END bigtable_hw_scan_all]
124-
125-
// [START bigtable_hw_delete_table]
126-
// Clean up by disabling and then deleting the table
127-
print("Delete the table");
128-
admin.disableTable(table.getName());
129-
admin.deleteTable(table.getName());
130-
// [END bigtable_hw_delete_table]
131-
132140
} catch (IOException e) {
133141
System.err.println("Exception while running HelloWorld: " + e.getMessage());
134142
e.printStackTrace();
135-
if (admin.tableExists(tableName.getName())) {
136-
print("Cleaning up table");
137-
admin.disableTable(table.getName());
138-
admin.deleteTable(table.getName());
139-
}
143+
140144
System.exit(1);
141145
}
142146

0 commit comments

Comments
 (0)