Share a connection
Stay organized with collections
Save and categorize content based on your preferences.
Set the IAM policy on a connection to share the connection with a user or group.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","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)."]]