|
| 1 | +/* |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.m.examples.bigtable; |
| 18 | + |
| 19 | +// [START bigtable_hw_imports_veneer] |
| 20 | +import com.google.api.gax.rpc.NotFoundException; |
| 21 | +import com.google.api.gax.rpc.ServerStream; |
| 22 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; |
| 23 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; |
| 24 | +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; |
| 25 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 26 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 27 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 28 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 29 | +import com.google.cloud.bigtable.data.v2.models.RowCell; |
| 30 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +// [END bigtable_hw_imports_veneer] |
| 34 | + |
| 35 | +/** |
| 36 | + * An example of using Google Cloud Bigtable. |
| 37 | + * |
| 38 | + * <p>This example is a very simple "hello world" application, that illustrates how to create a new |
| 39 | + * table, write to the table, read the data back, and delete the table. |
| 40 | + * |
| 41 | + * <ul> |
| 42 | + * <li>create table |
| 43 | + * <li>read single row |
| 44 | + * <li>read table |
| 45 | + * <li>delete table |
| 46 | + * </ul> |
| 47 | + */ |
| 48 | +public class HelloWorld { |
| 49
10000
td> | + |
| 50 | + private static final String COLUMN_FAMILY = "cf1"; |
| 51 | + private static final String COLUMN_QUALIFIER = "greeting"; |
| 52 | + private static final String ROW_KEY_PREFIX = "rowKey"; |
| 53 | + private final String tableId; |
| 54 | + private final BigtableDataClient dataClient; |
| 55 | + private final BigtableTableAdminClient adminClient; |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + |
| 59 | + if (args.length != 2) { |
| 60 | + System.out.println("Missing required project id or instance id"); |
| 61 | + return; |
| 62 | + } |
| 63 | + String projectId = args[0]; |
| 64 | + String instanceId = args[1]; |
| 65 | + |
| 66 | + HelloWorld helloWorld = new HelloWorld(projectId, instanceId, "test-table"); |
| 67 | + helloWorld.run(); |
| 68 | + } |
| 69 | + |
| 70 | + public HelloWorld(String projectId, String instanceId, String tableId) throws IOException { |
| 71 | + this.tableId = tableId; |
| 72 | + |
| 73 | + // [START bigtable_hw_connect_veneer] |
| 74 | + // Creates the settings to configure a bigtable data client. |
| 75 | + BigtableDataSettings settings = |
| 76 | + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); |
| 77 | + |
| 78 | + // Creates a bigtable data client. |
| 79 | + dataClient = BigtableDataClient.create(settings); |
| 80 | + |
| 81 | + // Creates the settings to configure a bigtable table admin client. |
| 82 | + BigtableTableAdminSettings adminSettings = |
| 83 | + BigtableTableAdminSettings.newBuilder() |
| 84 | + .setProjectId(projectId) |
| 85 | + .setInstanceId(instanceId) |
| 86 | + .build(); |
| 87 | + |
| 88 | + // Creates a bigtable table admin client. |
| 89 | + adminClient = BigtableTableAdminClient.create(adminSettings); |
| 90 | + // [END bigtable_hw_connect_veneer] |
| 91 | + } |
| 92 | + |
| 93 | + public void run() throws Exception { |
| 94 | + createTable(); |
| 95 | + writeToTable(); |
| 96 | + readSingleRow(); |
| 97 | + readTable(); |
| 98 | + deleteTable(); |
| 99 | + dataClient.close(); |
| 100 | + adminClient.close(); |
| 101 | + } |
| 102 | + |
| 103 | + /** Demonstrates how to create a table. */ |
| 104 | + public void createTable() { |
| 105 | + // [START bigtable_hw_create_table_veneer] |
| 106 | + // Checks if table exists, creates table if does not exist. |
| 107 | + if (!adminClient.exists(tableId)) { |
| 108 | + System.out.printl
B41A
n("Creating table: " + tableId); |
| 109 | + CreateTableRequest createTableRequest = |
| 110 | + CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY); |
| 111 | + adminClient.createTable(createTableRequest); |
| 112 | + System.out.printf("Table %s created successfully%n", tableId); |
| 113 | + } |
| 114 | + // [END bigtable_hw_create_table_veneer] |
| 115 | + } |
| 116 | + |
| 117 | + /** Demonstrates how to write some rows to a table. */ |
| 118 | + public void writeToTable() { |
| 119 | + // [START bigtable_hw_write_rows_veneer] |
| 120 | + try { |
| 121 | + System.out.println("\nWriting some greetings to the table"); |
| 122 | + String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"}; |
| 123 | + for (int i = 0; i < greetings.length; i++) { |
| 124 | + RowMutation rowMutation = |
| 125 | + RowMutation.create(tableId, ROW_KEY_PREFIX + i) |
| 126 | + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER, greetings[i]); |
| 127 | + dataClient.mutateRow(rowMutation); |
| 128 | + System.out.println(greetings[i]); |
| 129 | + } |
| 130 | + } catch (NotFoundException e) { |
| 131 | + System.err.println("Failed to write to non-existent table: " + e.getMessage()); |
| 132 | + } |
| 133 | + // [END bigtable_hw_write_rows_veneer] |
| 134 | + } |
| 135 | + |
| 136 | + /** Demonstrates how to read a single row from a table. */ |
| 137 | + public void readSingleRow() { |
| 138 | + // [START bigtable_hw_get_by_key_veneer] |
| 139 | + try { |
| 140 | + System.out.println("\nReading a single row by row key"); |
| 141 | + Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0); |
| 142 | + System.out.println("Row: " + row.getKey().toStringUtf8()); |
| 143 | + for (RowCell cell : row.getCells()) { |
| 144 | + System.out.printf( |
| 145 | + "Family: %s Qualifier: %s Value: %s%n", |
| 146 | + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); |
| 147 | + } |
| 148 | + } catch (NotFoundException e) { |
| 149 | + System.err.println("Failed to read from a non-existent table: " + e.getMessage()); |
| 150 | + } |
| 151 | + // [END bigtable_hw_get_by_key_veneer] |
| 152 | + } |
| 153 | + |
| 154 | + /** Demonstrates how to read an entire table. */ |
| 155 | + public void readTable() { |
| 156 | + // [START bigtable_hw_scan_all_veneer] |
| 157 | + try { |
| 158 | + System.out.println("\nReading the entire table"); |
| 159 | + Query query = Query.create(tableId); |
| 160 | + ServerStream<Row> rowStream = dataClient.readRows(query); |
| 161 | + for (Row r : rowStream) { |
| 162 | + System.out.println("Row Key: " + r.getKey().toStringUtf8()); |
| 163 | + for (RowCell cell : r.getCells()) { |
| 164 | + System.out.printf( |
| 165 | + "Family: %s Qualifier: %s Value: %s%n", |
| 166 | + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); |
| 167 | + } |
| 168 | + } |
| 169 | + } catch (NotFoundException e) { |
| 170 | + System.err.println("Failed to read a non-existent table: " + e.getMessage()); |
| 171 | + } |
| 172 | + // [END bigtable_hw_scan_all_veneer] |
| 173 | + } |
| 174 | + |
| 175 | + /** Demonstrates how to delete a table. */ |
| 176 | + public void deleteTable() { |
| 177 | + // [START bigtable_hw_delete_table_veneer] |
| 178 | + System.out.println("\nDeleting table: " + tableId); |
| 179 | + try { |
| 180 | + adminClient.deleteTable(tableId); |
| 181 | + System.out.printf("Table %s deleted successfully%n", tableId); |
| 182 | + } catch (NotFoundException e) { |
| 183 | + System.err.println("Failed to delete a non-existent table: " + e.getMessage()); |
| 184 | + } |
| 185 | + // [END bigtable_hw_delete_table_veneer] |
| 186 | + } |
| 187 | +} |
0 commit comments