집계 셀에 추가
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
집계 셀에 정수를 추가합니다. 이러한 유형의 쓰기는 AddToCell API 요청입니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to add an integer to an aggregate cell in Bigtable, using the AddToCell API request.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples illustrate how to increment a view count for a specific page in both Go and Java.\u003c/p\u003e\n"],["\u003cp\u003eThe examples show how to bucket views into hourly counts, and demonstrates the use of the AddToCell method in Go and the addToCell/mergeToCell methods in Java.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize the provided code you must set up Application Default Credentials and install the Bigtable client library.\u003c/p\u003e\n"]]],[],null,["Add an integer to an aggregate cell. This type of write makes an AddToCell API request.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Write examples](/bigtable/docs/writing-data)\n\nCode sample \n\nGo\n\n\nTo learn how to install and use the client library for Bigtable, see\n[Bigtable client libraries](/bigtable/docs/reference/libraries).\n\n\nTo authenticate to Bigtable, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \t\"cloud.google.com/go/bigtable\"\n )\n\n func writeAggregate(w io.Writer, projectID, instanceID string, tableName string) error {\n \t// projectID := \"my-project-id\"\n \t// instanceID := \"my-instance-id\"\n \t// tableName := \"mobile-time-series\"\n\n \tctx := context.Background()\n \tclient, err := bigtable.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Client_NewClient(ctx, projectID, instanceID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"bigtable.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n \ttbl := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Client_Open(tableName)\n \tcolumnFamilyName := \"view_count\"\n \tviewTimestamp, err := time.Parse(time.RFC3339, \"2024-03-13T12:41:34Z\")\n \tif err != nil {\n \t\treturn err\n \t}\n \thourlyBucket := viewTimestamp.Truncate(time.Hour)\n\n \tmut := bigtable.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Mutation_NewMutation()\n \tmut.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Mutation_AddIntToCell(columnFamilyName, \"views\", bigtable.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Timestamp_Time(hourlyBucket), 1)\n\n \trowKey := \"page#index.html\"\n \tif err := tbl.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_Table_Apply(ctx, rowKey, mut); err != nil {\n \t\treturn fmt.Errorf(\"Apply: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Successfully wrote row: %s\\n\", rowKey)\n \treturn nil\n }\n\nJava\n\n\nTo learn how to install and use the client library for Bigtable, see\n[Bigtable client libraries](/bigtable/docs/reference/libraries).\n\n\nTo authenticate to Bigtable, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.bigtable.data.v2.https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html;\n import com.google.cloud.bigtable.data.v2.models.https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.models.RowMutation.html;\n import com.google.common.primitives.Longs;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html;\n import java.time.Instant;\n import java.time.temporal.ChronoUnit;\n\n public class WriteAggregate {\n private static final String COUNT_COLUMN_FAMILY_NAME = \"view_count\";\n private static final long MICROS_PER_MILLI = 1000;\n\n public static void writeAggregate(String projectId, String instanceId, String tableId) {\n // String projectId = \"my-project-id\";\n // String instanceId = \"my-instance-id\";\n // String tableId = \"page-view-counter\";\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html dataClient = https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html.create(projectId, instanceId)) {\n\n String rowKey = \"page#index.html\";\n Instant viewTimestamp = Instant.parse(\"2024-03-13T12:41:34.123Z\");\n\n // Bucket the views for an hour into a single count, giving us an hourly view count for a\n // given page.\n Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS);\n long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI;\n\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.models.RowMutation.html rowMutation =\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.models.RowMutation.html.create(tableId, rowKey)\n .addToCell(COUNT_COLUMN_FAMILY_NAME, \"views\", hourlyBucketMicros, 1);\n\n dataClient.https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html#com_google_cloud_bigtable_data_v2_BigtableDataClient_mutateRow_com_google_cloud_bigtable_data_v2_models_RowMutation_(rowMutation);\n System.out.printf(\"Successfully wrote row %s\", rowKey);\n\n } catch (Exception e) {\n System.out.println(\"Error during WriteAggregate: \\n\" + e.toString());\n }\n }\n\n public static void mergeAggregate(String projectId, String instanceId, String tableId) {\n // String projectId = \"my-project-id\";\n // String instanceId = \"my-instance-id\";\n // String tableId = \"page-view-counter\";\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html dataClient = https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html.create(projectId, instanceId)) {\n\n String rowKey = \"page#index.html\";\n Instant viewTimestamp = Instant.parse(\"2024-03-13T12:41:34.123Z\");\n\n // Bucket the views for an hour into a single count, giving us an hourly view count for a\n // given page.\n Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS);\n long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI;\n\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.models.RowMutation.html rowMutation =\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.models.RowMutation.html.create(tableId, rowKey)\n .mergeToCell(\n COUNT_COLUMN_FAMILY_NAME,\n \"views\",\n hourlyBucketMicros,\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html#com_google_protobuf_ByteString_copyFrom_byte___(Longs.toByteArray(1L)));\n\n dataClient.https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.data.v2.BigtableDataClient.html#com_google_cloud_bigtable_data_v2_BigtableDataClient_mutateRow_com_google_cloud_bigtable_data_v2_models_RowMutation_(rowMutation);\n System.out.printf(\"Successfully wrote row %s\", rowKey);\n\n } catch (Exception e) {\n System.out.println(\"Error during mergeAggregate: \\n\" + e.toString());\n }\n }\n }\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigtable)."]]