Skip to content

Commit 8e8a523

Browse files
samples: Add aggregate write samples (#2170)
* feat: Add aggregate write samples Change-Id: Ie3834bd66a054c35ab95d4a4bbef232c2a37b72c * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 0131eb3 commit 8e8a523

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigtable/tree
430430
| Quickstart | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/Quickstart.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/Quickstart.java) |
431431
| Reads | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/Reads.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/Reads.java) |
432432
| Table Admin Example | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/TableAdminExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/TableAdminExample.java) |
433+
| Write Aggregate | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java) |
433434
| Write Batch | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java) |
434435
| Write Conditionally | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteConditionally.java) |
435436
| Write Increment | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/WriteIncrement.java) |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 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_aggregate]
20+
21+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
22+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import java.time.Instant;
24+
import java.time.temporal.ChronoUnit;
25+
26+
public class WriteAggregate {
27+
private static final String COUNT_COLUMN_FAMILY_NAME = "view_count";
28+
private static final long MICROS_PER_MILLI = 1000;
29+
30+
public static void writeAggregate(String projectId, String instanceId, String tableId) {
31+
// String projectId = "my-project-id";
32+
// String instanceId = "my-instance-id";
33+
// String tableId = "page-view-counter";
34+
35+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
36+
37+
String rowKey = "page#index.html";
38+
Instant viewTimestamp = Instant.parse("2024-03-13T12:41:34.123Z");
39+
40+
// Bucket the views for an hour into a single count, giving us an hourly view count for a
41+
// given page.
42+
Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS);
43+
long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI;
44+
45+
RowMutation rowMutation =
46+
RowMutation.create(tableId, rowKey)
47+
.addToCell(COUNT_COLUMN_FAMILY_NAME, "views", hourlyBucketMicros, 1);
48+
49+
dataClient.mutateRow(rowMutation);
50+
System.out.printf("Successfully wrote row %s", rowKey);
51+
52+
} catch (Exception e) {
53+
System.out.println("Error during WriteAggregate: \n" + e.toString());
54+
}
55+
}
56+
}
57+
58+
// [END bigtable_writes_aggregate]

samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
2020
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
21+
import com.google.cloud.bigtable.admin.v2.models.Type;
2122
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2223
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
2324
import com.google.cloud.bigtable.data.v2.models.Mutation;
@@ -32,6 +33,7 @@ public class MobileTimeSeriesBaseTest extends BigtableBaseTest {
3233
public static final String TABLE_ID = generateResourceId("mobile-time-series");
3334
public static final String COLUMN_FAMILY_NAME_STATS = "stats_summary";
3435
public static final String COLUMN_FAMILY_NAME_PLAN = "cell_plan";
36+
public static final String COLUMN_FAMILY_NAME_VIEW_COUNT = "view_count";
3537
public static final Instant CURRENT_TIME = Instant.now();
3638
public static final long TIMESTAMP = CURRENT_TIME.toEpochMilli() * 1000;
3739
public static final long TIMESTAMP_MINUS_HR =
@@ -43,7 +45,8 @@ public static void createTable() throws IOException {
4345
CreateTableRequest createTableRequest =
4446
CreateTableRequest.of(TABLE_ID)
4547
.addFamily(COLUMN_FAMILY_NAME_STATS)
46-
.addFamily(COLUMN_FAMILY_NAME_PLAN);
48+
.addFamily(COLUMN_FAMILY_NAME_PLAN)
49+
.addFamily(COLUMN_FAMILY_NAME_VIEW_COUNT, Type.int64Sum());
4750
adminClient.createTable(createTableRequest);
4851
} catch (IOException e) {
4952
System.out.println("Error during createTable: \n" + e.toString());

samples/snippets/src/test/java/com/example/bigtable/WritesTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ public void test4_WriteIncrement() {
7272
assertThat(
7373
output, CoreMatchers.containsString("Successfully updated row phone#4c410523#20190501"));
7474
}
75+
76+
@Test
77+
public void test5_WriteAggregate() {
78+
WriteAggregate.writeAggregate(projectId, instanceId, TABLE_ID);
79+
80+
String output = bout.toString();
81+
assertThat(output, CoreMatchers.containsString("Successfully wrote row page#index.html"));
82+
}
7583
}

0 commit comments

Comments
 (0)