Add credentials to connect BigQuery to Cloud SQL.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Connect to Cloud SQL](https://docs.cloud.google.com/bigquery/docs/connect-to-sql)

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.cloud.bigquery.connection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlCredential.html;
    import com.google.cloud.bigquery.connection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.html;
    import com.google.cloud.bigquery.connection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html;
    import com.google.cloud.bigquery.connection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CreateConnectionRequest.html;
    import com.google.cloud.bigquery.connection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.LocationName.html;
    import com.google.cloud.bigqueryconnection.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html;
    import java.io.IOException;

    // Sample to create a connection with cloud MySql database
    public class CreateConnection {

      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        String projectId = "MY_PROJECT_ID";
        String location = "MY_LOCATION";
        String connectionId = "MY_CONNECTION_ID";
        String database = "MY_DATABASE";
        String instance = "MY_INSTANCE";
        String instanceLocation = "MY_INSTANCE_LOCATION";
        String username = "MY_USERNAME";
        String password = "MY_PASSWORD";
        String instanceId = String.format("%s:%s:%s", projectId, instanceLocation, instance);
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlCredential.html cloudSqlCredential =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlCredential.html.newBuilder().https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlCredential.Builder.html#com_google_cloud_bigquery_connection_v1_CloudSqlCredential_Builder_setUsername_java_lang_String_(username).https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlCredential.Builder.html#com_google_cloud_bigquery_connection_v1_CloudSqlCredential_Builder_setPassword_java_lang_String_(password).build();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.html cloudSqlProperties =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.html.newBuilder()
                .https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.Builder.html#com_google_cloud_bigquery_connection_v1_CloudSqlProperties_Builder_setType_com_google_cloud_bigquery_connection_v1_CloudSqlProperties_DatabaseType_(https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.html.DatabaseType.MYSQL)
                .setDatabase(database)
                .https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.Builder.html#com_google_cloud_bigquery_connection_v1_CloudSqlProperties_Builder_setInstanceId_java_lang_String_(instanceId)
                .https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CloudSqlProperties.Builder.html#com_google_cloud_bigquery_connection_v1_CloudSqlProperties_Builder_setCredential_com_google_cloud_bigquery_connection_v1_CloudSqlCredential_(cloudSqlCredential)
                .build();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html connection = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html.newBuilder().https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.Builder.html#com_google_cloud_bigquery_connection_v1_Connection_Builder_setCloudSql_com_google_cloud_bigquery_connection_v1_CloudSqlProperties_(cloudSqlProperties).build();
        createConnection(projectId, location, connectionId, connection);
      }

      static void createConnection(
          String projectId, String location, String connectionId, https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html connection)
          throws IOException {
        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html client = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient.html.create()) {
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.LocationName.html parent = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.LocationName.html.of(projectId, location);
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CreateConnectionRequest.html request =
              https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CreateConnectionRequest.html.newBuilder()
                  .setParent(parent.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.LocationName.html#com_google_cloud_bigquery_connection_v1_LocationName_toString__())
                  .setConnection(connection)
                  .https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.CreateConnectionRequest.Builder.html#com_google_cloud_bigquery_connection_v1_CreateConnectionRequest_Builder_setConnectionId_java_lang_String_(connectionId)
                  .build();
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html response = client.createConnection(request);
          System.out.println("Connection created successfully :" + response.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryconnection/latest/com.google.cloud.bigquery.connection.v1.Connection.html#com_google_cloud_bigquery_connection_v1_Connection_getName__());
        }
      }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from google.cloud import bigquery_connection_v1 as bq_connection

    """This sample shows how to create a BigQuery connection with a Cloud SQL for MySQL database"""


    def main() -> None:
        # TODO(developer): Set all variables for your Cloud SQL for MySQL connection.
        project_id = "your-project-id"  # set project_id
        location = "US"  # set location
        # See: https://cloud.google.com/bigquery/docs/locations for a list of
        # available locations.
        database = "my-database"  # set database name
        username = "my-username"  # set database username
        password = "my-password"  # set database password
        cloud_sql_conn_name = ""  # set the name of your connection
        transport = "grpc"  # Set the transport to either "grpc" or "rest"
        connection_id = "my-sample-connection"

        cloud_sql_credential = bq_connection.CloudSqlCredential(
            {
                "username": username,
                "password": password,
            }
        )
        cloud_sql_properties = bq_connection.CloudSqlProperties(
            {
                "type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL,
                "database": database,
                "instance_id": cloud_sql_conn_name,
                "credential": cloud_sql_credential,
            }
        )
        create_mysql_connection(
            connection_id, project_id, location, cloud_sql_properties, transport
        )


    def create_mysql_connection(
        connection_id: str,
        project_id: str,
        location: str,
        cloud_sql_properties: bq_connection.CloudSqlProperties,
        transport: str,
    ) -> None:
        connection = bq_connection.types.Connection({"cloud_sql": cloud_sql_properties})
        client = bq_connection.ConnectionServiceClient(transport=transport)
        parent = client.common_location_path(project_id, location)
        request = bq_connection.CreateConnectionRequest(
            {
                "parent": parent,
                "connection": connection,
                # connection_id is optional, but can be useful to identify
                # connections by name. If not supplied, one is randomly
                # generated.
                "connection_id": connection_id,
            }
        )
        response = client.create_connection(request)
        print(f"Created connection successfully: {response.name}")

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).