创建 AppProfile

创建 AppProfile。

代码示例

C++

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
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 instance_name = cbt::InstanceName(project_id, instance_id);

  google::bigtable::admin::v2::AppProfile ap;
  ap.mutable_multi_cluster_routing_use_any()->Clear();

  StatusOr<google::bigtable::admin::v2::AppProfile> profile =
      instance_admin.CreateAppProfile(instance_name, profile_id,
                                      std::move(ap));
  if (!profile) throw std::move(profile).status();
  std::cout << "New profile created with name=" << profile->name() << "\n";
}

PHP

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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\CreateAppProfileRequest;

/**
 * Create 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 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 create
 */
function create_app_profile(
    string $projectId,
    string $instanceId,
    string $clusterId,
    string $appProfileId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();
    $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);

    $appProfile = new AppProfile([
        'name' => $appProfileId,
        'description' => 'Description for this newly created AppProfile'
    ]);

    // 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' => false
    ]);

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

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

    printf('Creating a new AppProfile %s' . PHP_EOL, $appProfileId);

    try {
        $createAppProfileRequest = (new CreateAppProfileRequest())
            ->setParent($instanceName)
            ->setAppProfileId($appProfileId)
            ->setAppProfile($appProfile);
        $newAppProfile = $instanceAdminClient->createAppProfile($createAppProfileRequest);
    } catch (ApiException $e) {
        if ($e->getStatus() === 'ALREADY_EXISTS') {
            printf('AppProfile %s already exists.', $appProfileId);
            return;
        }
        throw $e;
    }

    printf('AppProfile created: %s', $newAppProfile->getName());
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器