Cómo actualizar o insertar filas
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Actualiza o inserta filas en una tabla.
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 provides code samples in C++ and PHP demonstrating how to update or insert rows in a Bigtable table.\u003c/p\u003e\n"],["\u003cp\u003eThe C++ example uses the \u003ccode\u003ecbt::SingleRowMutation\u003c/code\u003e class to set cells and update rows within a table.\u003c/p\u003e\n"],["\u003cp\u003eThe PHP example demonstrates how to create a table, a column family and then insert data in the table using the Google Cloud Bigtable client library.\u003c/p\u003e\n"],["\u003cp\u003eBoth examples guide the user on how to setup the client library and credentials needed to access the Bigtable API.\u003c/p\u003e\n"],["\u003cp\u003eThe PHP code showcases error handling for when the table already exists, skipping its creation and proceeding to the column family creation.\u003c/p\u003e\n"]]],[],null,["Update or insert rows in a table.\n\nCode sample \n\nC++\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 struct InsertOrUpdate {\n std::string column_family;\n std::string column;\n std::string value;\n };\n namespace cbt = ::google::cloud::bigtable;\n [](cbt::Table table, std::string const& key,\n std::vector\u003cInsertOrUpdate\u003e const& inserts) {\n cbt::SingleRowMutation mutation(key);\n for (auto const& mut : inserts) {\n mutation.emplace_back(\n cbt::SetCell(mut.column_family, mut.column, mut.value));\n }\n google::cloud::Status status = table.Apply(std::move(mutation));\n if (!status.ok()) throw std::runtime_error(status.message());\n std::cout \u003c\u003c \"Row successfully updated\\n\";\n }\n\nPHP\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 use Google\\ApiCore\\ApiException;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\Client\\BigtableInstanceAdminClient;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\Client\\BigtableTableAdminClient;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\ColumnFamily;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\CreateTableRequest;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\ModifyColumnFamiliesRequest;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\ModifyColumnFamiliesRequest\\Modification;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\Table as TableClass;\n use Google\\Cloud\\Bigtable\\BigtableClient;\n\n /**\n * Perform insert/update operations on a Bigtable\n *\n * @param string $projectId The Google Cloud project ID\n * @param string $instanceId The ID of the Bigtable instance\n * @param string $tableId The ID of the table on which we intend to insert/update rows\n */\n function insert_update_rows(\n string $projectId,\n string $instanceId = 'quickstart-instance-php',\n string $tableId = 'bigtable-php-table'\n ): void {\n $dataClient = new BigtableClient([\n 'projectId' =\u003e $projectId,\n ]);\n\n $instanceAdminClient = new BigtableInstanceAdminClient();\n $tableAdminClient = new BigtableTableAdminClient();\n\n $instanceName = $instanceAdminClient-\u003einstanceName($projectId, $instanceId);\n $tableName = $tableAdminClient-\u003etableName($projectId, $instanceId, $tableId);\n\n $table = new TableClass();\n\n printf('Creating table %s' . PHP_EOL, $tableId);\n\n try {\n $createTableRequest = (new CreateTableRequest())\n -\u003esetParent($instanceName)\n -\u003esetTableId($tableId)\n -\u003esetTable($table);\n $tableAdminClient-\u003ecreatetable($createTableRequest);\n } catch (ApiException $e) {\n if ($e-\u003egetStatus() === 'ALREADY_EXISTS') {\n printf('Table %s already exists.' . PHP_EOL, $tableId);\n return;\n }\n throw $e;\n }\n\n printf('Table %s created' . PHP_EOL, $tableId);\n\n $table = $dataClient-\u003etable($instanceId, $tableId);\n $columnFamilyId = 'cf1';\n\n printf('Creating column family %s' . PHP_EOL, $columnFamilyId);\n\n $columnFamily4 = new ColumnFamily();\n $columnModification = new Modification();\n $columnModification-\u003esetId($columnFamilyId);\n $columnModification-\u003esetCreate($columnFamily4);\n $modifyColumnFamiliesRequest = (new ModifyColumnFamiliesRequest())\n -\u003esetName($tableName)\n -\u003esetModifications([$columnModification]);\n $tableAdminClient-\u003emodifyColumnFamilies($modifyColumnFamiliesRequest);\n\n printf('Inserting data in the table' . PHP_EOL);\n\n $insertRows = [\n 'rk5' =\u003e [\n 'cf1' =\u003e [\n 'cq5' =\u003e [\n 'value' =\u003e 'Value5',\n 'timeStamp' =\u003e time_in_microseconds()\n ]\n ]\n ]\n ];\n $table-\u003eupsert($insertRows);\n\n printf('Data inserted successfully!' . PHP_EOL);\n }\n\n function time_in_microseconds(): float\n {\n $mt = microtime(true);\n $mt = sprintf('%.03f', $mt);\n return (float) $mt * 1000000;\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)."]]