-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
0131eb3
commit 8e8a523
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
samples/snippets/src/main/java/com/example/bigtable/WriteAggregate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters