Skip to content

Commit d5befb3

Browse files
authored
Merge pull request #376 from googleapis/samples-bigtable-snippets
samples: migrate samples from GoogleCloudPlatform/java-docs-samples /bigtable/snippets
2 parents f0765d1 + 012fbc3 commit d5befb3

File tree

12 files changed

+2129
-0
lines changed

12 files changed

+2129
-0
lines changed

.kokoro/nightly/samples.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ env_vars: {
3636
key: "ENABLE_BUILD_COP"
3737
value: "true"
3838
}
39+
40+
env_vars: {
41+
key: "BIGTABLE_TESTING_INSTANCE"
42+
value: "instance"
43+
}

.kokoro/presubmit/samples.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ env_vars: {
3030
env_vars: {
3131
key: "SECRET_MANAGER_KEYS"
3232
value: "java-docs-samples-service-account"
33+
}
34+
35+
env_vars: {
36+
key: "BIGTABLE_TESTING_INSTANCE"
37+
value: "instance"
3338
}

samples/snippets/src/main/java/com/example/bigtable/Filters.java

Lines changed: 395 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.example.bigtable;
18+
19+
// [START bigtable_reads_print]
20+
21+
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
22+
23+
import com.google.api.gax.rpc.ServerStream;
24+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
25+
import com.google.cloud.bigtable.data.v2.models.Filters;
26+
import com.google.cloud.bigtable.data.v2.models.Query;
27+
import com.google.cloud.bigtable.data.v2.models.Row;
28+
import com.google.cloud.bigtable.data.v2.models.RowCell;
29+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
30+
import com.google.protobuf.ByteString;
31+
import java.io.IOException;
32+
33+
public class Reads {
34+
35+
// Write your code here.
36+
// [START_EXCLUDE]
37+
// [START bigtable_reads_row]
38+
public static void readRow() {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String projectId = "my-project-id";
41+
String instanceId = "my-instance-id";
42+
String tableId = "mobile-time-series";
43+
readRow(projectId, instanceId, tableId);
44+
}
45+
46+
public static void readRow(String projectId, String instanceId, String tableId) {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
51+
String rowkey = "phone#4c410523#20190501";
52+
53+
Row row = dataClient.readRow(tableId, rowkey);
54+
printRow(row);
55+
56+
} catch (IOException e) {
57+
System.out.println(
58+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
59+
}
60+
}
61+
// [END bigtable_reads_row]
62+
63+
// [START bigtable_reads_row_partial]
64+
public static void readRowPartial() {
65+
// TODO(developer): Replace these variables before running the sample.
66+
String projectId = "my-project-id";
67+
String instanceId = "my-instance-id";
68+
String tableId = "mobile-time-series";
69+
readRowPartial(projectId, instanceId, tableId);
70+
}
71+
72+
public static void readRowPartial(String projectId, String instanceId, String tableId) {
73+
// Initialize client that will be used to send requests. This client only needs to be created
74+
// once, and can be reused for multiple requests. After completing all of your requests, call
75+
// the "close" method on the client to safely clean up any remaining background resources.
76+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
77+
String rowkey = "phone#4c410523#20190501";
78+
Filters.Filter filter =
79+
FILTERS
80+
.chain()
81+
.filter(FILTERS.family().exactMatch("stats_summary"))
82+
.filter(FILTERS.qualifier().exactMatch("os_build"));
83+
84+
Row row = dataClient.readRow(tableId, rowkey, filter);
85+
printRow(row);
86+
87+
} catch (IOException e) {
88+
System.out.println(
89+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
90+
}
91+
}
92+
// [END bigtable_reads_row_partial]
93+
94+
// [START bigtable_reads_rows]
95+
public static void readRows() {
96+
// TODO(developer): Replace these variables before running the sample.
97+
String projectId = "my-project-id";
98+
String instanceId = "my-instance-id";
99+
String tableId = "mobile-time-series";
100+
readRows(projectId, instanceId, tableId);
101+
}
102+
103+
public static void readRows(String projectId, String instanceId, String tableId) {
104+
// Initialize client that will be used to send requests. This client only needs to be created
105+
// once, and can be reused for multiple requests. After completing all of your requests, call
106+
// the "close" method on the client to safely clean up any remaining background resources.
107+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
108+
Query query =
109+
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
110+
ServerStream<Row> rows = dataClient.readRows(query);
111+
for (Row row : rows) {
112+
printRow(row);
113+
}
114+
} catch (IOException e) {
115+
System.out.println(
116+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
117+
}
118+
}
119+
// [END bigtable_reads_rows]
120+
121+
// [START bigtable_reads_row_range]
122+
public static void readRowRange() {
123+
// TODO(developer): Replace these variables before running the sample.
124+
String projectId = "my-project-id";
125+
String instanceId = "my-instance-id";
126+
String tableId = "mobile-time-series";
127+
readRowRange(projectId, instanceId, tableId);
128+
}
129+
130+
public static void readRowRange(String projectId, String instanceId, String tableId) {
131+
String start = "phone#4c410523#20190501";
132+
String end = "phone#4c410523#201906201";
133+
134+
// Initialize client that will be used to send requests. This client only needs to be created
135+
// once, and can be reused for multiple requests. After completing all of your requests, call
136+
// the "close" method on the client to safely clean up any remaining background resources.
137+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
138+
Query query = Query.create(tableId).range(start, end);
139+
ServerStream<Row> rows = dataClient.readRows(query);
140+
for (Row row : rows) {
141+
printRow(row);
142+
}
143+
} catch (IOException e) {
144+
System.out.println(
145+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
146+
}
147+
}
148+
// [END bigtable_reads_row_range]
149+
150+
// [START bigtable_reads_row_ranges]
151+
public static void readRowRanges() {
152+
// TODO(developer): Replace these variables before running the sample.
153+
String projectId = "my-project-id";
154+
String instanceId = "my-instance-id";
155+
String tableId = "mobile-time-series";
156+
readRowRanges(projectId, instanceId, tableId);
157+
}
158+
159+
public static void readRowRanges(String projectId, String instanceId, String tableId) {
160+
// Initialize client that will be used to send requests. This client only needs to be created
161+
// once, and can be reused for multiple requests. After completing all of your requests, call
162+
// the "close" method on the client to safely clean up any remaining background resources.
163+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
164+
Query query =
165+
Query.create(tableId)
166+
.range("phone#4c410523#20190501", "phone#4c410523#20190601")
167+
.range("phone#5c10102#20190501", "phone#5c10102#20190601");
168+
ServerStream<Row> rows = dataClient.readRows(query);
169+
for (Row row : rows) {
170+
printRow(row);
171+
}
172+
} catch (IOException e) {
173+
System.out.println(
174+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
175+
}
176+
}
177+
// [END bigtable_reads_row_ranges]
178+
179+
// [START bigtable_reads_prefix]
180+
public static void readPrefix() {
181+
// TODO(developer): Replace these variables before running the sample.
182+
String projectId = "my-project-id";
183+
String instanceId = "my-instance-id";
184+
String tableId = "mobile-time-series";
185+
readPrefix(projectId, instanceId, tableId);
186+
}
187+
188+
public static void readPrefix(String projectId, String instanceId, String tableId) {
189+
// Initialize client that will be used to send requests. This client only needs to be created
190+
// once, and can be reused for multiple requests. After completing all of your requests, call
191+
// the "close" method on the client to safely clean up any remaining background resources.
192+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
193+
Query query = Query.create(tableId).prefix("phone");
194+
ServerStream<Row> rows = dataClient.readRows(query);
195+
for (Row row : rows) {
196+
printRow(row);
197+
}
198+
} catch (IOException e) {
199+
System.out.println(
200+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
201+
}
202+
}
203+
// [END bigtable_reads_prefix]
204+
205+
// [START bigtable_reads_filter]
206+
public static void readFilter() {
207+
// TODO(developer): Replace these variables before running the sample.
208+
String projectId = "my-project-id";
209+
String instanceId = "my-instance-id";
210+
String tableId = "mobile-time-series";
211+
readFilter(projectId, instanceId, tableId);
212+
}
213+
214+
public static void readFilter(String projectId, String instanceId, String tableId) {
215+
Filters.Filter filter = FILTERS.value().regex("PQ2A.*");
216+
217+
// Initialize client that will be used to send requests. This client only needs to be created
218+
// once, and can be reused for multiple requests. After completing all of your requests, call
219+
// the "close" method on the client to safely clean up any remaining background resources.
220+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
221+
Query query = Query.create(tableId).filter(filter);
222+
ServerStream<Row> rows = dataClient.readRows(query);
223+
for (Row row : rows) {
224+
printRow(row);
225+
}
226+
} catch (IOException e) {
227+
System.out.println(
228+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
229+
}
230+
}
231+
// [END bigtable_reads_filter]
232+
// [END_EXCLUDE]
233+
234+
private static void printRow(Row row) {
235+
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
236+
String colFamily = "";
237+
for (RowCell cell : row.getCells()) {
238+
if (!cell.getFamily().equals(colFamily)) {
239+
colFamily = cell.getFamily();
240+
System.out.printf("Column Family %s%n", colFamily);
241+
}
242+
System.out.printf(
243+
"\t%s: %s @%s%n",
244+
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
245+
}
246+
System.out.println();
247+
}
248+
}
249+
// [END bigtable_reads_print]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.example.bigtable;
18+
19+
// [START bigtable_writes_batch]
20+
21+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
22+
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
23+
import com.google.cloud.bigtable.data.v2.models.Mutation;
24+
import com.google.protobuf.ByteString;
25+
26+
public class WriteBatch {
27+
private static final String COLUMN_FAMILY_NAME = "stats_summary";
28+
29+
public static void writeBatch(String projectId, String instanceId, String tableId) {
30+
// String projectId = "my-project-id";
31+
// String instanceId = "my-instance-id";
32+
// String tableId = "mobile-time-series";
33+
34+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
35+
long timestamp = System.currentTimeMillis() * 1000;
36+
37+
BulkMutation bulkMutation =
38+
BulkMutation.create(tableId)
39+
.add(
40+
"tablet#a0b81f74#20190501",
41+
Mutation.create()
42+
.setCell(
43+
COLUMN_FAMILY_NAME,
44+
ByteString.copyFrom("connected_wifi".getBytes()),
45+
timestamp,
46+
1)
47+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1"))
48+
.add(
49+
"tablet#a0b81f74#20190502",
50+
Mutation.create()
51+
.setCell(
52+
COLUMN_FAMILY_NAME,
53+
ByteString.copyFrom("connected_wifi".getBytes()),
54+
timestamp,
55+
1)
56+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));
57+
58+
dataClient.bulkMutateRows(bulkMutation);
59+
60+
System.out.print("Successfully wrote 2 rows");
61+
} catch (Exception e) {
62+
System.out.println("Error during WriteBatch: \n" + e.toString());
63+
}
64+
}
65+
}
66+
67+
// [END bigtable_writes_batch]

0 commit comments

Comments
 (0)