Skip to content

Read and Filter Snippets #1747

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

Merged
merged 7 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bigtable/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<version>4.13-beta-3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
<dependency>
<groupId>com.google.cloud</groupId>
Expand Down
459 changes: 459 additions & 0 deletions bigtable/snippets/src/main/java/com/example/bigtable/Filters.java

Large diffs are not rendered by default.

272 changes: 272 additions & 0 deletions bigtable/snippets/src/main/java/com/example/bigtable/Reads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*
* 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_row]
// [START bigtable_reads_row_partial]
// [START bigtable_reads_rows]
// [START bigtable_reads_row_range]
// [START bigtable_reads_row_ranges]
// [START bigtable_reads_prefix]
// [START bigtable_reads_filter]

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 {
// [END bigtable_reads_row]
// [END bigtable_reads_row_partial]
// [END bigtable_reads_rows]
// [END bigtable_reads_row_range]
// [END bigtable_reads_row_ranges]
// [END bigtable_reads_prefix]
// [END bigtable_reads_filter]

// [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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \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 intailize service client, as a network error occured: \n" + e.toString());
}
}
// [END bigtable_reads_filter]

// [START bigtable_reads_row]
// [START bigtable_reads_row_partial]
// [START bigtable_reads_rows]
// [START bigtable_reads_row_range]
// [START bigtable_reads_row_ranges]
// [START bigtable_reads_prefix]
// [START bigtable_reads_filter]
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_row]
// [END bigtable_reads_row_partial]
// [END bigtable_reads_rows]
// [END bigtable_reads_row_range]
// [END bigtable_reads_row_ranges]
// [END bigtable_reads_prefix]
// [END bigtable_reads_filter]
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
long timestamp = System.currentTimeMillis() * 1000;

String rowKey = "phone#4c410523#20190501";
String rowkey = "phone#4c410523#20190501";

Mutation mutation =
Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
Expand All @@ -49,7 +49,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin
.filter(FILTERS.value().regex("PQ2A\\..*"));

ConditionalRowMutation conditionalRowMutation =
ConditionalRowMutation.create(tableId, rowKey).condition(filter).then(mutation);
ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);

boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static void writeIncrement(String projectId, String instanceId, String ta
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
// Get an existing row that has a cell with an incrementable value. A value can be incremented
// if it is encoded as a 64-bit big-endian signed integer.
String rowKey = "phone#4c410523#20190501";
String rowkey = "phone#4c410523#20190501";
ReadModifyWriteRow mutation =
ReadModifyWriteRow.create(tableId, rowKey)
ReadModifyWriteRow.create(tableId, rowkey)
.increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
Row success = dataClient.readModifyWriteRow(mutation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static void writeSimple(String projectId, String instanceId, String table
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
long timestamp = System.currentTimeMillis() * 1000;

String rowKey = "phone#4c410523#20190501";
String rowkey = "phone#4c410523#20190501";
ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});

RowMutation rowMutation =
RowMutation.create(tableId, rowKey)
RowMutation.create(tableId, rowkey)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom("connected_cell".getBytes()),
Expand All @@ -51,7 +51,7 @@ public static void writeSimple(String projectId, String instanceId, String table
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003");

dataClient.mutateRow(rowMutation);
System.out.printf("Successfully wrote row %s", rowKey);
System.out.printf("Successfully wrote row %s", rowkey);

} catch (Exception e) {
System.out.println("Error during WriteSimple: \n" + e.toString());
Expand Down
Loading