Compartir una conexión
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Establece la política de IAM en una conexión para compartirla con un usuario o grupo.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]