-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Bigtable Java samples #3946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Bigtable Java samples #3946
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b24d42f
Java samples
elisheva-qlogic 7932079
Samples and tests
elisheva-qlogic 13b5c1c
samples fixes
elisheva-qlogic a5231c3
code format fix
elisheva-qlogic adcafd0
HelloWorld changes
elisheva-qlogic 5cc4f15
system property for tests set up
elisheva-qlogic 7ad20e5
code format changes
elisheva-qlogic e57bb89
changes
elisheva-qlogic b8b4f22
HelloWorld updates
elisheva-qlogic 99b5c63
exceptions specified
elisheva-qlogic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /* | ||
| * Copyright 2018 Google LLC. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.cloud.examples.bigtable; | ||
|
|
||
| import com.google.api.gax.rpc.NotFoundException; | ||
| import com.google.api.gax.rpc.ServerStream; | ||
| import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; | ||
| import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; | ||
| import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; | ||
| import com.google.cloud.bigtable.data.v2.BigtableDataClient; | ||
| import com.google.cloud.bigtable.data.v2.BigtableDataSettings; | ||
| import com.google.cloud.bigtable.data.v2.models.InstanceName; | ||
| import com.google.cloud.bigtable.data.v2.models.Query; | ||
| import com.google.cloud.bigtable.data.v2.models.Row; | ||
| import com.google.cloud.bigtable.data.v2.models.RowCell; | ||
| import com.google.cloud.bigtable.data.v2.models.RowMutation; | ||
| import java.io.IOException; | ||
|
|
||
| public class HelloWorld { | ||
|
|
||
| private static final String COLUMN_FAMILY = "cf1"; | ||
| private static final String COLUMN_QUALIFIER = "greeting"; | ||
| private static final String ROW_KEY_PREFIX = "rowKey"; | ||
| private final String tableId; | ||
| private final BigtableDataClient dataClient; | ||
| private final BigtableTableAdminClient adminClient; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
|
|
||
| if (args.length != 2) { | ||
| System.out.println("Missing required project id or instance id"); | ||
| } | ||
| String projectId = args[0]; | ||
| String instanceId = args[1]; | ||
|
|
||
| HelloWorld helloWorld = new HelloWorld(projectId, instanceId, "test-table"); | ||
| helloWorld.run(); | ||
| } | ||
|
|
||
| public HelloWorld(String projectId, String instanceId, String tableId) throws IOException { | ||
| this.tableId = tableId; | ||
|
|
||
| // [START connecting_to_bigtable] | ||
| // Create the settings to configure a bigtable data client | ||
| BigtableDataSettings settings = | ||
| BigtableDataSettings.newBuilder() | ||
| .setInstanceName(InstanceName.of(projectId, instanceId)) | ||
| .build(); | ||
|
|
||
| // Create bigtable data client | ||
| dataClient = BigtableDataClient.create(settings); | ||
|
|
||
| // Create the settings to configure a bigtable admin client | ||
| BigtableTableAdminSettings adminSettings = | ||
| BigtableTableAdminSettings.newBuilder() | ||
| .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId)) | ||
| .build(); | ||
|
|
||
| // Create bigtable admin client | ||
| adminClient = BigtableTableAdminClient.create(adminSettings); | ||
| // [END connecting_to_bigtable] | ||
| } | ||
|
|
||
| public void run() throws Exception { | ||
| createTable(); | ||
| writeToTable(); | ||
| readSingleRow(); | ||
| readTable(); | ||
| deleteTable(); | ||
|
|
||
| dataClient.close(); | ||
| adminClient.close(); | ||
| } | ||
|
|
||
| public void createTable() { | ||
| // [START creating_a_table] | ||
| // Check if table exists, create table if does not exist | ||
| if (!adminClient.exists(tableId)) { | ||
| System.out.println("Creating table: " + tableId); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| CreateTableRequest createTableRequest = | ||
| CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY); | ||
| adminClient.createTable(createTableRequest); | ||
| System.out.printf("Table %s created successfully%n", tableId); | ||
| } | ||
igorbernstein2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [END creating_a_table] | ||
| } | ||
|
|
||
| public void writeToTable() { | ||
| // [START writing_rows] | ||
| try { | ||
| System.out.println("\nWriting some greetings to the table"); | ||
| String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"}; | ||
| for (int i = 0; i < greetings.length; i++) { | ||
| RowMutation rowMutation = | ||
| RowMutation.create(tableId, ROW_KEY_PREFIX + i) | ||
| .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER, greetings[i]); | ||
| dataClient.mutateRow(rowMutation); | ||
| System.out.println(greetings[i]); | ||
| } | ||
| } catch (NotFoundException e) { | ||
| System.err.println("Exception while writing to table: " + e.getMessage()); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| } | ||
| // [END writing_rows] | ||
| } | ||
|
|
||
| public void readSingleRow() { | ||
| // [START reading_a_row] | ||
| try { | ||
| System.out.println("\nReading a single row by row key"); | ||
| Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0); | ||
| System.out.println("Row: " + row.getKey().toStringUtf8()); | ||
| for (RowCell cell : row.getCells()) { | ||
| System.out.printf( | ||
| "Family: %s Qualifier: %s Value: %s%n", | ||
| cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); | ||
| } | ||
| } catch (NotFoundException e) { | ||
| System.err.println("Exception while reading a single row: " + e.getMessage()); | ||
| } | ||
| // [END reading_a_row] | ||
| } | ||
|
|
||
| public void readTable() { | ||
| // [START scanning_all_rows] | ||
| try { | ||
| System.out.println("\nReading the entire table"); | ||
| Query query = Query.create(tableId); | ||
| ServerStream<Row> rowStream = dataClient.readRows(query); | ||
| for (Row r : rowStream) { | ||
| System.out.println("Row Key: " + r.getKey().toStringUtf8()); | ||
| for (RowCell cell : r.getCells()) { | ||
| System.out.printf( | ||
| "Family: %s Qualifier: %s Value: %s%n", | ||
| cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); | ||
| } | ||
| } | ||
| } catch (NotFoundException e) { | ||
| System.err.println("Exception while reading table: " + e.getMessage()); | ||
| } | ||
| // [END scanning_all_rows] | ||
| } | ||
|
|
||
| public void deleteTable() { | ||
| // [START deleting_a_table] | ||
| System.out.println("\nDeleting table: " + tableId); | ||
| try { | ||
| adminClient.deleteTable(tableId); | ||
| System.out.printf("Table %s deleted successfully%n", tableId); | ||
| } catch (NotFoundException e) { | ||
| System.err.println("Exception while deleting table: " + e.getMessage()); | ||
| } | ||
| // [END deleting_a_table] | ||
| } | ||
| } | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.