共享连接
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
针对连接设置 IAM 政策以与用户或群组共享连接。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 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 details how to set the IAM policy on a connection to share it with specific users or groups.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Java code sample demonstrates how to share a connection by setting its IAM policy using the \u003ccode\u003eConnectionServiceClient\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe code requires specifying the project ID, location, and connection ID to identify the connection being shared.\u003c/p\u003e\n"],["\u003cp\u003eSharing a connection involves creating a \u003ccode\u003eBinding\u003c/code\u003e to define the members (users or groups) and the role they should have (e.g., \u003ccode\u003eroles/bigquery.connectionUser\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eThe sample shows that you can set the IAM policy on a connection and after the code is executed, it outputs "Connection shared successfully".\u003c/p\u003e\n"]]],[],null,["# Share a connection\n\nSet the IAM policy on a connection to share the connection with a user or group.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Connect to Amazon S3](/bigquery/docs/omni-aws-create-connection)\n- [Connect to Apache Spark](/bigquery/docs/connect-to-spark)\n- [Connect to Blob Storage](/bigquery/docs/omni-azure-create-connection)\n- [Connect to Cloud SQL](/bigquery/docs/connect-to-sql)\n- [Connect to SAP Datasphere](/bigquery/docs/connect-to-sap-datasphere)\n- [Connect to Spanner](/bigquery/docs/connect-to-spanner)\n- [Create and set up a Cloud resource connection](/bigquery/docs/create-cloud-resource-connection)\n- [Manage connections](/bigquery/docs/working-with-connections)\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.api.resourcenames.https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.resourcenames.ResourceName.html;\n import com.google.cloud.bigquery.connection.v1.https://cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.ConnectionName.html;\n import com.google.cloud.bigqueryconnection.v1.https://cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html;\n import com.google.iam.v1.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html;\n import com.google.iam.v1.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html;\n import com.google.iam.v1.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html;\n import java.io.IOException;\n\n // Sample to share connections\n public class ShareConnection {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"MY_PROJECT_ID\";\n String location = \"MY_LOCATION\";\n String connectionId = \"MY_CONNECTION_ID\";\n shareConnection(projectId, location, connectionId);\n }\n\n static void shareConnection(String projectId, String location, String connectionId)\n throws IOException {\n try (https://cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html.create()) {\n https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.resourcenames.ResourceName.html resource = https://cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.ConnectionName.html.of(projectId, location, connectionId);\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html binding =\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html.newBuilder()\n .https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.Builder.html#com_google_iam_v1_Binding_Builder_addMembers_java_lang_String_(\"group:example-analyst-group@google.com\")\n .setRole(\"roles/bigquery.connectionUser\")\n .build();\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html policy = https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html.newBuilder().https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.Builder.html#com_google_iam_v1_Policy_Builder_addBindings_com_google_iam_v1_Binding_(binding).build();\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html request =\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.html.newBuilder()\n .setResource(resource.toString())\n .https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.SetIamPolicyRequest.Builder.html#com_google_iam_v1_SetIamPolicyRequest_Builder_setPolicy_com_google_iam_v1_Policy_(policy)\n .build();\n client.setIamPolicy(request);\n System.out.println(\"Connection shared successfully\");\n }\n }\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigqueryconnection)."]]