diff --git a/README.md b/README.md index c48901f989..90aa37ffe7 100644 --- a/README.md +++ b/README.md @@ -430,6 +430,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigtable/tree | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | | 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) | diff --git a/samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java b/samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java new file mode 100644 index 0000000000..646e302cb5 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java @@ -0,0 +1,58 @@ +/* + * Copyright 2024 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_aggregate] + +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +public class WriteAggregate { + private static final String COUNT_COLUMN_FAMILY_NAME = "view_count"; + private static final long MICROS_PER_MILLI = 1000; + + public static void writeAggregate(String projectId, String instanceId, String tableId) { + // String projectId = "my-project-id"; + // String instanceId = "my-instance-id"; + // String tableId = "page-view-counter"; + + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { + + String rowKey = "page#index.html"; + Instant viewTimestamp = Instant.parse("2024-03-13T12:41:34.123Z"); + + // Bucket the views for an hour into a single count, giving us an hourly view count for a + // given page. + Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS); + long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI; + + RowMutation rowMutation = + RowMutation.create(tableId, rowKey) + .addToCell(COUNT_COLUMN_FAMILY_NAME, "views", hourlyBucketMicros, 1); + + dataClient.mutateRow(rowMutation); + System.out.printf("Successfully wrote row %s", rowKey); + + } catch (Exception e) { + System.out.println("Error during WriteAggregate: \n" + e.toString()); + } + } +} + +// [END bigtable_writes_aggregate] diff --git a/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java b/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java index 98182187a3..f1a9ae5c01 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/MobileTimeSeriesBaseTest.java @@ -18,6 +18,7 @@ import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.Type; 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; @@ -32,6 +33,7 @@ public class MobileTimeSeriesBaseTest extends BigtableBaseTest { public static final String TABLE_ID = generateResourceId("mobile-time-series"); public static final String COLUMN_FAMILY_NAME_STATS = "stats_summary"; public static final String COLUMN_FAMILY_NAME_PLAN = "cell_plan"; + public static final String COLUMN_FAMILY_NAME_VIEW_COUNT = "view_count"; public static final Instant CURRENT_TIME = Instant.now(); public static final long TIMESTAMP = CURRENT_TIME.toEpochMilli() * 1000; public static final long TIMESTAMP_MINUS_HR = @@ -43,7 +45,8 @@ public static void createTable() throws IOException { CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID) .addFamily(COLUMN_FAMILY_NAME_STATS) - .addFamily(COLUMN_FAMILY_NAME_PLAN); + .addFamily(COLUMN_FAMILY_NAME_PLAN) + .addFamily(COLUMN_FAMILY_NAME_VIEW_COUNT, Type.int64Sum()); adminClient.createTable(createTableRequest); } catch (IOException e) { System.out.println("Error during createTable: \n" + e.toString()); diff --git a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java index 2bbdea0b15..ac6a09b4ef 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -72,4 +72,12 @@ public void test4_WriteIncrement() { assertThat( output, CoreMatchers.containsString("Successfully updated row phone#4c410523#20190501")); } + + @Test + public void test5_WriteAggregate() { + WriteAggregate.writeAggregate(projectId, instanceId, TABLE_ID); + + String output = bout.toString(); + assertThat(output, CoreMatchers.containsString("Successfully wrote row page#index.html")); + } }