Mettre à jour ou insérer des lignes
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Mettre à jour ou insérer des lignes dans une table.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]