创建 AppProfile
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples in C++ and PHP for creating an AppProfile in Google Cloud Bigtable.\u003c/p\u003e\n"],["\u003cp\u003eThe C++ code demonstrates creating an AppProfile with multi-cluster routing, while the PHP code showcases how to create an AppProfile with single-cluster routing, including setting its cluster ID and specifying whether transactional writes are allowed.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples require users to set up Application Default Credentials for authentication and refer to the Bigtable client libraries for installation instructions.\u003c/p\u003e\n"],["\u003cp\u003eThe PHP example also includes error handling for cases where an AppProfile with the given ID already exists.\u003c/p\u003e\n"],["\u003cp\u003eA link to the Google Cloud sample browser is provided for users to search for code samples for other products.\u003c/p\u003e\n"]]],[],null,["Create an AppProfile.\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 namespace cbt = ::google::cloud::bigtable;\n namespace cbta = ::google::cloud::bigtable_admin;\n using ::google::cloud::StatusOr;\n [](cbta::BigtableInstanceAdminClient instance_admin,\n std::string const& project_id, std::string const& instance_id,\n std::string const& profile_id) {\n std::string instance_name = cbt::InstanceName(project_id, instance_id);\n\n google::bigtable::admin::v2::AppProfile ap;\n ap.mutable_multi_cluster_routing_use_any()-\u003eClear();\n\n StatusOr\u003cgoogle::bigtable::admin::v2::AppProfile\u003e profile =\n instance_admin.CreateAppProfile(instance_name, profile_id,\n std::move(ap));\n if (!profile) throw std::move(profile).status();\n std::cout \u003c\u003c \"New profile created with name=\" \u003c\u003c profile-\u003ename() \u003c\u003c \"\\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\\AppProfile;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\AppProfile\\SingleClusterRouting;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\Client\\BigtableInstanceAdminClient;\n use Google\\Cloud\\Bigtable\\Admin\\V2\\CreateAppProfileRequest;\n\n /**\n * Create an App Profile\n *\n * @param string $projectId The Google Cloud project ID\n * @param string $instanceId The ID of the Bigtable instance\n * @param string $clusterId The ID of the cluster where the new App Profile will route it's requests(in case of single cluster routing)\n * @param string $appProfileId The ID of the App Profile to create\n */\n function create_app_profile(\n string $projectId,\n string $instanceId,\n string $clusterId,\n string $appProfileId\n ): void {\n $instanceAdminClient = new BigtableInstanceAdminClient();\n $instanceName = $instanceAdminClient-\u003einstanceName($projectId, $instanceId);\n\n $appProfile = new AppProfile([\n 'name' =\u003e $appProfileId,\n 'description' =\u003e 'Description for this newly created AppProfile'\n ]);\n\n // create a new routing policy\n // allow_transactional_writes refers to Single-Row-Transactions(https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions)\n $routingPolicy = new SingleClusterRouting([\n 'cluster_id' =\u003e $clusterId,\n 'allow_transactional_writes' =\u003e false\n ]);\n\n // set the newly created routing policy to our app profile\n $appProfile-\u003esetSingleClusterRouting($routingPolicy);\n\n // we could also create a multi cluster routing policy like so:\n // $routingPolicy = new \\Google\\Cloud\\Bigtable\\Admin\\V2\\AppProfile\\MultiClusterRoutingUseAny();\n // $appProfile-\u003esetMultiClusterRoutingUseAny($routingPolicy);\n\n printf('Creating a new AppProfile %s' . PHP_EOL, $appProfileId);\n\n try {\n $createAppProfileRequest = (new CreateAppProfileRequest())\n -\u003esetParent($instanceName)\n -\u003esetAppProfileId($appProfileId)\n -\u003esetAppProfile($appProfile);\n $newAppProfile = $instanceAdminClient-\u003ecreateAppProfile($createAppProfileRequest);\n } catch (ApiException $e) {\n if ($e-\u003egetStatus() === 'ALREADY_EXISTS') {\n printf('AppProfile %s already exists.', $appProfileId);\n return;\n }\n throw $e;\n }\n\n printf('AppProfile created: %s', $newAppProfile-\u003egetName());\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)."]]