Mettre à jour ou insérer des lignes

Mettre à jour ou insérer des lignes dans une table.

Exemple de code

C++

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

struct InsertOrUpdate {
  std::string column_family;
  std::string column;
  std::string value;
};
namespace cbt = ::google::cloud::bigtable;
[](cbt::Table table, std::string const& key,
   std::vector<InsertOrUpdate> const& inserts) {
  cbt::SingleRowMutation mutation(key);
  for (auto const& mut : inserts) {
    mutation.emplace_back(
        cbt::SetCell(mut.column_family, mut.column, mut.value));
  }
  google::cloud::Status status = table.Apply(std::move(mutation));
  if (!status.ok()) throw std::runtime_error(status.message());
  std::cout << "Row successfully updated\n";
}

PHP

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
use Google\Cloud\Bigtable\Admin\V2\Table as TableClass;
use Google\ApiCore\ApiException;

/**
 * Perform insert/update operations on a Bigtable
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 * @param string $tableId The ID of the table on which we intend to insert/update rows
 */
function insert_update_rows(
    string $projectId,
    string $instanceId = 'quickstart-instance-php',
    string $tableId = 'bigtable-php-table'
): void {
    $dataClient = new BigtableClient([
        'projectId' => $projectId,
    ]);

    $instanceAdminClient = new BigtableInstanceAdminClient();
    $tableAdminClient = new BigtableTableAdminClient();

    $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
    $tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);

    $table = new TableClass();

    printf('Creating table %s' . PHP_EOL, $tableId);

    try {
        $tableAdminClient->createtable(
            $instanceName,
            $tableId,
            $table
        );
    } catch (ApiException $e) {
        if ($e->getStatus() === 'ALREADY_EXISTS') {
            printf('Table %s already exists.' . PHP_EOL, $tableId);
            return;
        }
        throw $e;
    }

    printf('Table %s created' . PHP_EOL, $tableId);

    $table = $dataClient->table($instanceId, $tableId);
    $columnFamilyId = 'cf1';

    printf('Creating column family %s' . PHP_EOL, $columnFamilyId);

    $columnFamily4 = new ColumnFamily();
    $columnModification = new Modification();
    $columnModification->setId($columnFamilyId);
    $columnModification->setCreate($columnFamily4);
    $tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);

    printf('Inserting data in the table' . PHP_EOL);

    $insertRows = [
        'rk5' => [
            'cf1' => [
                'cq5' => [
                    'value' => 'Value5',
                    'timeStamp' => time_in_microseconds()
                ]
            ]
        ]
    ];
    $table->upsert($insertRows);

    printf('Data inserted successfully!' . PHP_EOL);
}

function time_in_microseconds()
{
    $mt = microtime(true);
    $mt = sprintf('%.03f', $mt);
    return (float) $mt * 1000000;
}

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.