Cómo agregar a una celda agregada
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Agrega un número entero a una celda de agregado. Con este tipo de escritura se realiza una solicitud a la API de AddToCell.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]