Skip to content

Commit

Permalink
samples: Add aggregate write samples (#2170)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
steveniemitz and gcf-owl-bot[bot] committed Jun 10, 2024
1 parent 0131eb3 commit 8e8a523
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 8e8a523

Please sign in to comment.