-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #376 from googleapis/samples-bigtable-snippets
samples: migrate samples from GoogleCloudPlatform/java-docs-samples /bigtable/snippets
- Loading branch information
Showing
12 changed files
with
2,129 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
395 changes: 395 additions & 0 deletions
395
samples/snippets/src/main/java/com/example/bigtable/Filters.java
Large diffs are not rendered by default.
Oops, something went wrong.
249 changes: 249 additions & 0 deletions
249
samples/snippets/src/main/java/com/example/bigtable/Reads.java
This file contains 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,249 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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 | ||
* | ||
* http://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.example.bigtable; | ||
|
||
// [START bigtable_reads_print] | ||
|
||
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; | ||
|
||
import com.google.api.gax.rpc.ServerStream; | ||
import com.google.cloud.bigtable.data.v2.BigtableDataClient; | ||
import com.google.cloud.bigtable.data.v2.models.Filters; | ||
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 com.google.protobuf.ByteString; | ||
import java.io.IOException; | ||
|
||
public class Reads { | ||
|
||
// Write your code here. | ||
// [START_EXCLUDE] | ||
// [START bigtable_reads_row] | ||
public static void readRow() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readRow(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readRow(String projectId, String instanceId, String tableId) { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
String rowkey = "phone#4c410523#20190501"; | ||
|
||
Row row = dataClient.readRow(tableId, rowkey); | ||
printRow(row); | ||
|
||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_row] | ||
|
||
// [START bigtable_reads_row_partial] | ||
public static void readRowPartial() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readRowPartial(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readRowPartial(String projectId, String instanceId, String tableId) { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
String rowkey = "phone#4c410523#20190501"; | ||
Filters.Filter filter = | ||
FILTERS | ||
.chain() | ||
.filter(FILTERS.family().exactMatch("stats_summary")) | ||
.filter(FILTERS.qualifier().exactMatch("os_build")); | ||
|
||
Row row = dataClient.readRow(tableId, rowkey, filter); | ||
printRow(row); | ||
|
||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_row_partial] | ||
|
||
// [START bigtable_reads_rows] | ||
public static void readRows() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readRows(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readRows(String projectId, String instanceId, String tableId) { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
Query query = | ||
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502"); | ||
ServerStream<Row> rows = dataClient.readRows(query); | ||
for (Row row : rows) { | ||
printRow(row); | ||
} | ||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_rows] | ||
|
||
// [START bigtable_reads_row_range] | ||
public static void readRowRange() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readRowRange(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readRowRange(String projectId, String instanceId, String tableId) { | ||
String start = "phone#4c410523#20190501"; | ||
String end = "phone#4c410523#201906201"; | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
Query query = Query.create(tableId).range(start, end); | ||
ServerStream<Row> rows = dataClient.readRows(query); | ||
for (Row row : rows) { | ||
printRow(row); | ||
} | ||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_row_range] | ||
|
||
// [START bigtable_reads_row_ranges] | ||
public static void readRowRanges() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readRowRanges(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readRowRanges(String projectId, String instanceId, String tableId) { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
Query query = | ||
Query.create(tableId) | ||
.range("phone#4c410523#20190501", "phone#4c410523#20190601") | ||
.range("phone#5c10102#20190501", "phone#5c10102#20190601"); | ||
ServerStream<Row> rows = dataClient.readRows(query); | ||
for (Row row : rows) { | ||
printRow(row); | ||
} | ||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_row_ranges] | ||
|
||
// [START bigtable_reads_prefix] | ||
public static void readPrefix() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readPrefix(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readPrefix(String projectId, String instanceId, String tableId) { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
Query query = Query.create(tableId).prefix("phone"); | ||
ServerStream<Row> rows = dataClient.readRows(query); | ||
for (Row row : rows) { | ||
printRow(row); | ||
} | ||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_prefix] | ||
|
||
// [START bigtable_reads_filter] | ||
public static void readFilter() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String instanceId = "my-instance-id"; | ||
String tableId = "mobile-time-series"; | ||
readFilter(projectId, instanceId, tableId); | ||
} | ||
|
||
public static void readFilter(String projectId, String instanceId, String tableId) { | ||
Filters.Filter filter = FILTERS.value().regex("PQ2A.*"); | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
Query query = Query.create(tableId).filter(filter); | ||
ServerStream<Row> rows = dataClient.readRows(query); | ||
for (Row row : rows) { | ||
printRow(row); | ||
} | ||
} catch (IOException e) { | ||
System.out.println( | ||
"Unable to initialize service client, as a network error occurred: \n" + e.toString()); | ||
} | ||
} | ||
// [END bigtable_reads_filter] | ||
// [END_EXCLUDE] | ||
|
||
private static void printRow(Row row) { | ||
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); | ||
String colFamily = ""; | ||
for (RowCell cell : row.getCells()) { | ||
if (!cell.getFamily().equals(colFamily)) { | ||
colFamily = cell.getFamily(); | ||
System.out.printf("Column Family %s%n", colFamily); | ||
} | ||
System.out.printf( | ||
"\t%s: %s @%s%n", | ||
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp()); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
// [END bigtable_reads_print] |
67 changes: 67 additions & 0 deletions
67
samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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 | ||
* | ||
* http://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.example.bigtable; | ||
|
||
// [START bigtable_writes_batch] | ||
|
||
import com.google.cloud.bigtable.data.v2.BigtableDataClient; | ||
import com.google.cloud.bigtable.data.v2.models.BulkMutation; | ||
import com.google.cloud.bigtable.data.v2.models.Mutation; | ||
import com.google.protobuf.ByteString; | ||
|
||
public class WriteBatch { | ||
private static final String COLUMN_FAMILY_NAME = "stats_summary"; | ||
|
||
public static void writeBatch(String projectId, String instanceId, String tableId) { | ||
// String projectId = "my-project-id"; | ||
// String instanceId = "my-instance-id"; | ||
// String tableId = "mobile-time-series"; | ||
|
||
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { | ||
long timestamp = System.currentTimeMillis() * 1000; | ||
|
||
BulkMutation bulkMutation = | ||
BulkMutation.create(tableId) | ||
.add( | ||
"tablet#a0b81f74#20190501", | ||
Mutation.create() | ||
.setCell( | ||
COLUMN_FAMILY_NAME, | ||
ByteString.copyFrom("connected_wifi".getBytes()), | ||
timestamp, | ||
1) | ||
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")) | ||
.add( | ||
"tablet#a0b81f74#20190502", | ||
Mutation.create() | ||
.setCell( | ||
COLUMN_FAMILY_NAME, | ||
ByteString.copyFrom("connected_wifi".getBytes()), | ||
timestamp, | ||
1) | ||
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")); | ||
|
||
dataClient.bulkMutateRows(bulkMutation); | ||
|
||
System.out.print("Successfully wrote 2 rows"); | ||
} catch (Exception e) { | ||
System.out.println("Error during WriteBatch: \n" + e.toString()); | ||
} | ||
} | ||
} | ||
|
||
// [END bigtable_writes_batch] |
Oops, something went wrong.