Skip to content
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

samples: Add aggregate write samples #2170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.34.0')
implementation platform('com.google.cloud:libraries-bom:26.40.0')

implementation 'com.google.cloud:google-cloud-bigtable'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigtable:2.35.1'
implementation 'com.google.cloud:google-cloud-bigtable:2.39.4'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigtable" % "2.35.1"
libraryDependencies += "com.google.cloud" % "google-cloud-bigtable" % "2.39.4"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -498,6 +498,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 Expand Up @@ -609,7 +610,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigtable/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigtable.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigtable/2.35.1
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigtable/2.39.4
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
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 @@ -31,6 +32,7 @@ public class MobileTimeSeriesBaseTest extends BigtableBaseTest {
public static final String TABLE_ID = generateTableId("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 @@ -42,7 +44,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"));
}
}