AppProfile aktualisieren

AppProfile aktualisieren

Codebeispiel

C++

Informationen zum Installieren und Verwenden der Clientbibliothek für Bigtable finden Sie unter Bigtable-Clientbibliotheken

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Bigtable zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id, std::string const& instance_id,
   std::string const& profile_id, std::string const& description) {
  std::string profile_name =
      cbt::AppProfileName(project_id, instance_id, profile_id);

  google::bigtable::admin::v2::AppProfile profile;
  profile.set_name(profile_name);
  profile.set_description(description);

  google::protobuf::FieldMask mask;
  mask.add_paths("description");

  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(std::move(profile), std::move(mask));
  auto modified_profile = profile_future.get();
  if (!modified_profile) throw std::move(modified_profile).status();
  std::cout << "Updated AppProfile: " << modified_profile->DebugString()
            << "\n";
}
namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id, std::string const& instance_id,
   std::string const& profile_id) {
  std::string profile_name =
      cbt::AppProfileName(project_id, instance_id, profile_id);

  google::bigtable::admin::v2::UpdateAppProfileRequest req;
  req.mutable_app_profile()->set_name(profile_name);
  req.mutable_app_profile()->mutable_multi_cluster_routing_use_any()->Clear();
  req.mutable_update_mask()->add_paths("multi_cluster_routing_use_any");
  req.set_ignore_warnings(true);

  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(std::move(req));
  auto modified_profile = profile_future.get();
  if (!modified_profile) throw std::move(modified_profile).status();
  std::cout << "Updated AppProfile: " << modified_profile->DebugString()
            << "\n";
}

PHP

Informationen zum Installieren und Verwenden der Clientbibliothek für Bigtable finden Sie unter Bigtable-Clientbibliotheken.

Richten Sie zur Authentifizierung bei Bigtable die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

use Google\ApiCore\ApiException;
use Google\Cloud\Bigtable\Admin\V2\AppProfile;
use Google\Cloud\Bigtable\Admin\V2\AppProfile\SingleClusterRouting;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\UpdateAppProfileRequest;
use Google\Protobuf\FieldMask;

/**
 * Update an App Profile
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 * @param string $clusterId The ID of the new cluster where the new App Profile will route it's requests(in case of single cluster routing)
 * @param string $appProfileId The ID of the App Profile to update
 */
function update_app_profile(
    string $projectId,
    string $instanceId,
    string $clusterId,
    string $appProfileId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();
    $appProfileName = $instanceAdminClient->appProfileName($projectId, $instanceId, $appProfileId);

    $appProfile = new AppProfile([
        'name' => $appProfileName,
        'description' => 'The updated description',
    ]);

    // create a new routing policy
    // allow_transactional_writes refers to Single-Row-Transactions(https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions)
    $routingPolicy = new SingleClusterRouting([
        'cluster_id' => $clusterId,
        'allow_transactional_writes' => true
    ]);

    // set the newly created routing policy to our app profile
    $appProfile->setSingleClusterRouting($routingPolicy);

    // or we could also create a multi cluster routing policy like so:
    // $routingPolicy = new \Google\Cloud\Bigtable\Admin\V2\AppProfile\MultiClusterRoutingUseAny();
    // $appProfile->setMultiClusterRoutingUseAny($routingPolicy);

    // returns a string identifier depending on SingleClusterRouting or MultiClusterRoutingUseAny
    $routingPolicyStr = $appProfile->getRoutingPolicy();

    $updateMask = new FieldMask([
        'paths' => ['description', $routingPolicyStr]
    ]);

    printf('Updating the AppProfile %s' . PHP_EOL, $appProfileId);

    try {
        // Bigtable warns you while updating the routing policy, or when toggling the allow_transactional_writes
        // to force it to update, we set ignoreWarnings to true.
        // If you just want to update something simple like description, you can remove it.
        $updateAppProfileRequest = (new UpdateAppProfileRequest())
            ->setAppProfile($appProfile)
            ->setUpdateMask($updateMask)
            ->setIgnoreWarnings(true);
        $operationResponse = $instanceAdminClient->updateAppProfile($updateAppProfileRequest);

        $operationResponse->pollUntilComplete();
        if ($operationResponse->operationSucceeded()) {
            $updatedAppProfile = $operationResponse->getResult();
            printf('App profile updated: %s' . PHP_EOL, $updatedAppProfile->getName());
            // doSomethingWith($updatedAppProfile)
        } else {
            $error = $operationResponse->getError();
            // handleError($error)
        }
    } catch (ApiException $e) {
        if ($e->getStatus() === 'NOT_FOUND') {
            printf('App Profile %s does not exist.' . PHP_EOL, $appProfileId);
            return;
        }
        throw $e;
    }
}

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.