Google Cloud Sql Operators¶
CloudSqlInstanceDatabaseCreateOperator¶
Creates a new database inside a Cloud SQL instance.
For parameter definition, take a look at
CloudSqlInstanceDatabaseCreateOperator
.
Arguments¶
Some arguments in the example DAG are taken from environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_db_create_task = CloudSqlInstanceDatabaseCreateOperator(
project_id=GCP_PROJECT_ID,
body=db_create_body,
instance=INSTANCE_NAME,
task_id='sql_db_create_task'
)
sql_db_create_task2 = CloudSqlInstanceDatabaseCreateOperator(
body=db_create_body,
instance=INSTANCE_NAME,
task_id='sql_db_create_task2'
)
Example request body:
db_create_body = {
"instance": INSTANCE_NAME,
"name": DB_NAME,
"project": GCP_PROJECT_ID
}
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation for to create a new database inside the instance.
CloudSqlInstanceDatabaseDeleteOperator¶
Deletes a database from a Cloud SQL instance.
For parameter definition, take a look at
CloudSqlInstanceDatabaseDeleteOperator
.
Arguments¶
Some arguments in the example DAG are taken from environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_db_delete_task = CloudSqlInstanceDatabaseDeleteOperator(
project_id=GCP_PROJECT_ID,
instance=INSTANCE_NAME,
database=DB_NAME,
task_id='sql_db_delete_task'
)
sql_db_delete_task2 = CloudSqlInstanceDatabaseDeleteOperator(
instance=INSTANCE_NAME,
database=DB_NAME,
task_id='sql_db_delete_task2'
)
Templating¶
template_fields = ('project_id', 'instance', 'database', 'gcp_conn_id',
'api_version')
More information¶
See Google Cloud SQL API documentation to delete a database.
CloudSqlInstanceDatabasePatchOperator¶
Updates a resource containing information about a database inside a Cloud SQL instance using patch semantics. See: https://cloud.google.com/sql/docs/mysql/admin-api/how-tos/performance#patch
For parameter definition, take a look at
CloudSqlInstanceDatabasePatchOperator
.
Arguments¶
Some arguments in the example DAG are taken from environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_db_patch_task = CloudSqlInstanceDatabasePatchOperator(
project_id=GCP_PROJECT_ID,
body=db_patch_body,
instance=INSTANCE_NAME,
database=DB_NAME,
task_id='sql_db_patch_task'
)
sql_db_patch_task2 = CloudSqlInstanceDatabasePatchOperator(
body=db_patch_body,
instance=INSTANCE_NAME,
database=DB_NAME,
task_id='sql_db_patch_task2'
)
Example request body:
db_patch_body = {
"charset": "utf16",
"collation": "utf16_general_ci"
}
Templating¶
template_fields = ('project_id', 'instance', 'database', 'gcp_conn_id',
'api_version')
More information¶
See Google Cloud SQL API documentation to update a database.
CloudSqlInstanceDeleteOperator¶
Deletes a Cloud SQL instance in Google Cloud Platform.
It is also used for deleting read and failover replicas.
For parameter definition, take a look at
CloudSqlInstanceDeleteOperator
.
Arguments¶
Some arguments in the example DAG are taken from OS environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_instance_delete_task = CloudSqlInstanceDeleteOperator(
project_id=GCP_PROJECT_ID,
instance=INSTANCE_NAME,
task_id='sql_instance_delete_task'
)
sql_instance_delete_task2 = CloudSqlInstanceDeleteOperator(
instance=INSTANCE_NAME2,
task_id='sql_instance_delete_task2'
)
Note: If the instance has read or failover replicas you need to delete them before you delete the primary instance. Replicas are deleted the same way as primary instances:
sql_instance_failover_replica_delete_task = CloudSqlInstanceDeleteOperator(
project_id=GCP_PROJECT_ID,
instance=FAILOVER_REPLICA_NAME,
task_id='sql_instance_failover_replica_delete_task'
)
sql_instance_read_replica_delete_task = CloudSqlInstanceDeleteOperator(
project_id=GCP_PROJECT_ID,
instance=READ_REPLICA_NAME,
task_id='sql_instance_read_replica_delete_task'
)
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation to delete a SQL instance.
CloudSqlInstanceExportOperator¶
Exports data from a Cloud SQL instance to a Cloud Storage bucket as a SQL dump or CSV file.
Note
This operator is idempotent. If executed multiple times with the same export file URI, the export file in GCS will simply be overridden.
For parameter definition take a look at
CloudSqlInstanceExportOperator
.
Arguments¶
Some arguments in the example DAG are taken from Airflow variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
EXPORT_URI = os.environ.get('GCSQL_MYSQL_EXPORT_URI', 'gs://bucketName/fileName')
IMPORT_URI = os.environ.get('GCSQL_MYSQL_IMPORT_URI', 'gs://bucketName/fileName')
Example body defining the export operation:
export_body = {
"exportContext": {
"fileType": "sql",
"uri": EXPORT_URI,
"sqlExportOptions": {
"schemaOnly": False
}
}
}
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_export_task = CloudSqlInstanceExportOperator(
project_id=GCP_PROJECT_ID,
body=export_body,
instance=INSTANCE_NAME,
task_id='sql_export_task'
)
sql_export_task2 = CloudSqlInstanceExportOperator(
body=export_body,
instance=INSTANCE_NAME,
task_id='sql_export_task2'
)
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation to export data.
Troubleshooting¶
If you receive an “Unauthorized” error in GCP, make sure that the service account of the Cloud SQL instance is authorized to write to the selected GCS bucket.
It is not the service account configured in Airflow that communicates with GCS, but rather the service account of the particular Cloud SQL instance.
To grant the service account with the appropriate WRITE permissions for the GCS bucket
you can use the GoogleCloudStorageBucketCreateAclEntryOperator
,
as shown in the example:
sql_gcp_add_bucket_permission_task = GoogleCloudStorageBucketCreateAclEntryOperator(
entity="user-{{ task_instance.xcom_pull("
"'sql_instance_create_task', key='service_account_email') "
"}}",
role="WRITER",
bucket=export_url_split[1], # netloc (bucket)
task_id='sql_gcp_add_bucket_permission_task'
)
CloudSqlInstanceImportOperator¶
Imports data into a Cloud SQL instance from a SQL dump or CSV file in Cloud Storage.
CSV import:¶
This operator is NOT idempotent for a CSV import. If the same file is imported multiple times, the imported data will be duplicated in the database. Moreover, if there are any unique constraints the duplicate import may result in an error.
SQL import:¶
This operator is idempotent for a SQL import if it was also exported by Cloud SQL. The exported SQL contains ‘DROP TABLE IF EXISTS’ statements for all tables to be imported.
If the import file was generated in a different way, idempotence is not guaranteed. It has to be ensured on the SQL file level.
For parameter definition take a look at
CloudSqlInstanceImportOperator
.
Arguments¶
Some arguments in the example DAG are taken from Airflow variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
EXPORT_URI = os.environ.get('GCSQL_MYSQL_EXPORT_URI', 'gs://bucketName/fileName')
IMPORT_URI = os.environ.get('GCSQL_MYSQL_IMPORT_URI', 'gs://bucketName/fileName')
Example body defining the import operation:
import_body = {
"importContext": {
"fileType": "sql",
"uri": IMPORT_URI
}
}
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_import_task = CloudSqlInstanceImportOperator(
project_id=GCP_PROJECT_ID,
body=import_body,
instance=INSTANCE_NAME2,
task_id='sql_import_task'
)
sql_import_task2 = CloudSqlInstanceImportOperator(
body=import_body,
instance=INSTANCE_NAME2,
task_id='sql_import_task2'
)
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation to import data.
Troubleshooting¶
If you receive an “Unauthorized” error in GCP, make sure that the service account of the Cloud SQL instance is authorized to read from the selected GCS object.
It is not the service account configured in Airflow that communicates with GCS, but rather the service account of the particular Cloud SQL instance.
To grant the service account with the appropriate READ permissions for the GCS object
you can use the GoogleCloudStorageObjectCreateAclEntryOperator
,
as shown in the example:
sql_gcp_add_object_permission_task = GoogleCloudStorageObjectCreateAclEntryOperator(
entity="user-{{ task_instance.xcom_pull("
"'sql_instance_create_task2', key='service_account_email')"
" }}",
role="READER",
bucket=import_url_split[1], # netloc (bucket)
object_name=import_url_split[2][1:], # path (strip first '/')
task_id='sql_gcp_add_object_permission_task',
)
prev_task = next_dep(sql_gcp_add_object_permission_task, prev_task)
# For import to work we also need to add the Cloud SQL instance's Service Account
# write access to the whole bucket!.
sql_gcp_add_bucket_permission_2_task = GoogleCloudStorageBucketCreateAclEntryOperator(
entity="user-{{ task_instance.xcom_pull("
"'sql_instance_create_task2', key='service_account_email') "
"}}",
role="WRITER",
bucket=import_url_split[1], # netloc
task_id='sql_gcp_add_bucket_permission_2_task',
)
CloudSqlInstanceCreateOperator¶
Creates a new Cloud SQL instance in Google Cloud Platform.
It is also used for creating read replicas.
For parameter definition, take a look at
CloudSqlInstanceCreateOperator
.
If an instance with the same name exists, no action will be taken and the operator will succeed.
Arguments¶
Some arguments in the example DAG are taken from OS environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Some other arguments are created based on the arguments above:
FAILOVER_REPLICA_NAME = INSTANCE_NAME + "-failover-replica"
READ_REPLICA_NAME = INSTANCE_NAME + "-read-replica"
Example body defining the instance with failover replica:
body = {
"name": INSTANCE_NAME,
"settings": {
"tier": "db-n1-standard-1",
"backupConfiguration": {
"binaryLogEnabled": True,
"enabled": True,
"startTime": "05:00"
},
"activationPolicy": "ALWAYS",
"dataDiskSizeGb": 30,
"dataDiskType": "PD_SSD",
"databaseFlags": [],
"ipConfiguration": {
"ipv4Enabled": True,
"requireSsl": True,
},
"locationPreference": {
"zone": "europe-west4-a"
},
"maintenanceWindow": {
"hour": 5,
"day": 7,
"updateTrack": "canary"
},
"pricingPlan": "PER_USE",
"replicationType": "ASYNCHRONOUS",
"storageAutoResize": True,
"storageAutoResizeLimit": 0,
"userLabels": {
"my-key": "my-value"
}
},
"failoverReplica": {
"name": FAILOVER_REPLICA_NAME
},
"databaseVersion": "MYSQL_5_7",
"region": "europe-west4",
}
Example body defining read replica for the instance above:
read_replica_body = {
"name": READ_REPLICA_NAME,
"settings": {
"tier": "db-n1-standard-1",
},
"databaseVersion": "MYSQL_5_7",
"region": "europe-west4",
"masterInstanceName": INSTANCE_NAME,
}
Note: Failover replicas are created together with the instance in a single task. Read replicas need to be created in separate tasks.
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_instance_create_task = CloudSqlInstanceCreateOperator(
project_id=GCP_PROJECT_ID,
body=body,
instance=INSTANCE_NAME,
task_id='sql_instance_create_task'
)
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation to create an instance.
CloudSqlInstancePatchOperator¶
Updates settings of a Cloud SQL instance in Google Cloud Platform (partial update).
For parameter definition, take a look at
CloudSqlInstancePatchOperator
.
This is a partial update, so only values for the settings specified in the body will be set / updated. The rest of the existing instance’s configuration will remain unchanged.
Arguments¶
Some arguments in the example DAG are taken from OS environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
INSTANCE_NAME = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME', 'test-mysql')
INSTANCE_NAME2 = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME2', 'test-mysql2')
DB_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'testdb')
Example body defining the instance:
patch_body = {
"name": INSTANCE_NAME,
"settings": {
"dataDiskSizeGb": 35,
"maintenanceWindow": {
"hour": 3,
"day": 6,
"updateTrack": "canary"
},
"userLabels": {
"my-key-patch": "my-value-patch"
}
}
}
Using the operator¶
You can create the operator with or without project id. If project id is missing it will be retrieved from the GCP connection used. Both variants are shown:
sql_instance_patch_task = CloudSqlInstancePatchOperator(
project_id=GCP_PROJECT_ID,
body=patch_body,
instance=INSTANCE_NAME,
task_id='sql_instance_patch_task'
)
sql_instance_patch_task2 = CloudSqlInstancePatchOperator(
body=patch_body,
instance=INSTANCE_NAME,
task_id='sql_instance_patch_task2'
)
Templating¶
template_fields = ('project_id', 'instance', 'gcp_conn_id', 'api_version')
More information¶
See Google Cloud SQL API documentation to patch an instance.
CloudSqlQueryOperator¶
Performs DDL or DML SQL queries in Google Cloud SQL instance. The DQL (retrieving data from Google Cloud SQL) is not supported. You might run the SELECT queries, but the results of those queries are discarded.
You can specify various connectivity methods to connect to running instance, starting from public IP plain connection through public IP with SSL or both TCP and socket connection via Cloud SQL Proxy. The proxy is downloaded and started/stopped dynamically as needed by the operator.
There is a gcpcloudsql:// connection type that you should use to define what
kind of connectivity you want the operator to use. The connection is a “meta”
type of connection. It is not used to make an actual connectivity on its own, but it
determines whether Cloud SQL Proxy should be started by CloudSqlDatabaseHook
and what kind of database connection (Postgres or MySQL) should be created
dynamically to connect to Cloud SQL via public IP address or via the proxy.
The ‘CloudSqlDatabaseHook` uses
CloudSqlProxyRunner
to manage Cloud SQL
Proxy lifecycle (each task has its own Cloud SQL Proxy)
When you build connection, you should use connection parameters as described in
CloudSqlDatabaseHook
. You can see
examples of connections below for all the possible types of connectivity. Such connection
can be reused between different tasks (instances of CloudSqlQueryOperator
). Each
task will get their own proxy started if needed with their own TCP or UNIX socket.
For parameter definition, take a look at
CloudSqlQueryOperator
.
Since query operator can run arbitrary query, it cannot be guaranteed to be idempotent. SQL query designer should design the queries to be idempotent. For example, both Postgres and MySQL support CREATE TABLE IF NOT EXISTS statements that can be used to create tables in an idempotent way.
Arguments¶
If you define connection via AIRFLOW_CONN_*
URL defined in an environment
variable, make sure the URL components in the URL are URL-encoded.
See examples below for details.
Note that in case of SSL connections you need to have a mechanism to make the certificate/key files available in predefined locations for all the workers on which the operator can run. This can be provided for example by mounting NFS-like volumes in the same path for all the workers.
Some arguments in the example DAG are taken from the OS environment variables:
GCP_PROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'example-project')
GCP_REGION = os.environ.get('GCP_REGION', 'europe-west-1b')
GCSQL_POSTGRES_INSTANCE_NAME_QUERY = os.environ.get(
'GCSQL_POSTGRES_INSTANCE_NAME_QUERY',
'testpostgres')
GCSQL_POSTGRES_DATABASE_NAME = os.environ.get('GCSQL_POSTGRES_DATABASE_NAME',
'postgresdb')
GCSQL_POSTGRES_USER = os.environ.get('GCSQL_POSTGRES_USER', 'postgres_user')
GCSQL_POSTGRES_PASSWORD = os.environ.get('GCSQL_POSTGRES_PASSWORD', 'password')
GCSQL_POSTGRES_PUBLIC_IP = os.environ.get('GCSQL_POSTGRES_PUBLIC_IP', '0.0.0.0')
GCSQL_POSTGRES_PUBLIC_PORT = os.environ.get('GCSQL_POSTGRES_PUBLIC_PORT', 5432)
GCSQL_POSTGRES_CLIENT_CERT_FILE = os.environ.get('GCSQL_POSTGRES_CLIENT_CERT_FILE',
".key/postgres-client-cert.pem")
GCSQL_POSTGRES_CLIENT_KEY_FILE = os.environ.get('GCSQL_POSTGRES_CLIENT_KEY_FILE',
".key/postgres-client-key.pem")
GCSQL_POSTGRES_SERVER_CA_FILE = os.environ.get('GCSQL_POSTGRES_SERVER_CA_FILE',
".key/postgres-server-ca.pem")
GCSQL_MYSQL_INSTANCE_NAME_QUERY = os.environ.get('GCSQL_MYSQL_INSTANCE_NAME_QUERY',
'testmysql')
GCSQL_MYSQL_DATABASE_NAME = os.environ.get('GCSQL_MYSQL_DATABASE_NAME', 'mysqldb')
GCSQL_MYSQL_USER = os.environ.get('GCSQL_MYSQL_USER', 'mysql_user')
GCSQL_MYSQL_PASSWORD = os.environ.get('GCSQL_MYSQL_PASSWORD', 'password')
GCSQL_MYSQL_PUBLIC_IP = os.environ.get('GCSQL_MYSQL_PUBLIC_IP', '0.0.0.0')
GCSQL_MYSQL_PUBLIC_PORT = os.environ.get('GCSQL_MYSQL_PUBLIC_PORT', 3306)
GCSQL_MYSQL_CLIENT_CERT_FILE = os.environ.get('GCSQL_MYSQL_CLIENT_CERT_FILE',
".key/mysql-client-cert.pem")
GCSQL_MYSQL_CLIENT_KEY_FILE = os.environ.get('GCSQL_MYSQL_CLIENT_KEY_FILE',
".key/mysql-client-key.pem")
GCSQL_MYSQL_SERVER_CA_FILE = os.environ.get('GCSQL_MYSQL_SERVER_CA_FILE',
".key/mysql-server-ca.pem")
SQL = [
'CREATE TABLE IF NOT EXISTS TABLE_TEST (I INTEGER)',
'CREATE TABLE IF NOT EXISTS TABLE_TEST (I INTEGER)', # shows warnings logged
'INSERT INTO TABLE_TEST VALUES (0)',
'CREATE TABLE IF NOT EXISTS TABLE_TEST2 (I INTEGER)',
'DROP TABLE TABLE_TEST',
'DROP TABLE TABLE_TEST2',
]
Example connection definitions for all connectivity cases. Note that all the components of the connection URI should be URL-encoded:
HOME_DIR = expanduser("~")
def get_absolute_path(path):
if path.startswith("/"):
return path
else:
return os.path.join(HOME_DIR, path)
postgres_kwargs = dict(
user=quote_plus(GCSQL_POSTGRES_USER),
password=quote_plus(GCSQL_POSTGRES_PASSWORD),
public_port=GCSQL_POSTGRES_PUBLIC_PORT,
public_ip=quote_plus(GCSQL_POSTGRES_PUBLIC_IP),
project_id=quote_plus(GCP_PROJECT_ID),
location=quote_plus(GCP_REGION),
instance=quote_plus(GCSQL_POSTGRES_INSTANCE_NAME_QUERY),
database=quote_plus(GCSQL_POSTGRES_DATABASE_NAME),
client_cert_file=quote_plus(get_absolute_path(GCSQL_POSTGRES_CLIENT_CERT_FILE)),
client_key_file=quote_plus(get_absolute_path(GCSQL_POSTGRES_CLIENT_KEY_FILE)),
server_ca_file=quote_plus(get_absolute_path(GCSQL_POSTGRES_SERVER_CA_FILE))
)
# The connections below are created using one of the standard approaches - via environment
# variables named AIRFLOW_CONN_* . The connections can also be created in the database
# of AIRFLOW (using command line or UI).
# Postgres: connect via proxy over TCP
os.environ['AIRFLOW_CONN_PROXY_POSTGRES_TCP'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=postgres&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=True&" \
"sql_proxy_use_tcp=True".format(**postgres_kwargs)
# Postgres: connect via proxy over UNIX socket (specific proxy version)
os.environ['AIRFLOW_CONN_PROXY_POSTGRES_SOCKET'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=postgres&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=True&" \
"sql_proxy_version=v1.13&" \
"sql_proxy_use_tcp=False".format(**postgres_kwargs)
# Postgres: connect directly via TCP (non-SSL)
os.environ['AIRFLOW_CONN_PUBLIC_POSTGRES_TCP'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=postgres&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=False&" \
"use_ssl=False".format(**postgres_kwargs)
# Postgres: connect directly via TCP (SSL)
os.environ['AIRFLOW_CONN_PUBLIC_POSTGRES_TCP_SSL'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=postgres&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=False&" \
"use_ssl=True&" \
"sslcert={client_cert_file}&" \
"sslkey={client_key_file}&" \
"sslrootcert={server_ca_file}"\
.format(**postgres_kwargs)
mysql_kwargs = dict(
user=quote_plus(GCSQL_MYSQL_USER),
password=quote_plus(GCSQL_MYSQL_PASSWORD),
public_port=GCSQL_MYSQL_PUBLIC_PORT,
public_ip=quote_plus(GCSQL_MYSQL_PUBLIC_IP),
project_id=quote_plus(GCP_PROJECT_ID),
location=quote_plus(GCP_REGION),
instance=quote_plus(GCSQL_MYSQL_INSTANCE_NAME_QUERY),
database=quote_plus(GCSQL_MYSQL_DATABASE_NAME),
client_cert_file=quote_plus(get_absolute_path(GCSQL_MYSQL_CLIENT_CERT_FILE)),
client_key_file=quote_plus(get_absolute_path(GCSQL_MYSQL_CLIENT_KEY_FILE)),
server_ca_file=quote_plus(get_absolute_path(GCSQL_MYSQL_SERVER_CA_FILE))
)
# MySQL: connect via proxy over TCP (specific proxy version)
os.environ['AIRFLOW_CONN_PROXY_MYSQL_TCP'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=mysql&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=True&" \
"sql_proxy_version=v1.13&" \
"sql_proxy_use_tcp=True".format(**mysql_kwargs)
# MySQL: connect via proxy over UNIX socket using pre-downloaded Cloud Sql Proxy binary
try:
sql_proxy_binary_path = subprocess.check_output(
['which', 'cloud_sql_proxy']).decode('utf-8').rstrip()
except subprocess.CalledProcessError:
sql_proxy_binary_path = "/tmp/anyhow_download_cloud_sql_proxy"
os.environ['AIRFLOW_CONN_PROXY_MYSQL_SOCKET'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=mysql&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=True&" \
"sql_proxy_binary_path={sql_proxy_binary_path}&" \
"sql_proxy_use_tcp=False".format(
sql_proxy_binary_path=quote_plus(sql_proxy_binary_path), **mysql_kwargs)
# MySQL: connect directly via TCP (non-SSL)
os.environ['AIRFLOW_CONN_PUBLIC_MYSQL_TCP'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=mysql&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=False&" \
"use_ssl=False".format(**mysql_kwargs)
# MySQL: connect directly via TCP (SSL) and with fixed Cloud Sql Proxy binary path
os.environ['AIRFLOW_CONN_PUBLIC_MYSQL_TCP_SSL'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=mysql&" \
"project_id={project_id}&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=False&" \
"use_ssl=True&" \
"sslcert={client_cert_file}&" \
"sslkey={client_key_file}&" \
"sslrootcert={server_ca_file}".format(**mysql_kwargs)
# Special case: MySQL: connect directly via TCP (SSL) and with fixed Cloud Sql
# Proxy binary path AND with missing project_id
os.environ['AIRFLOW_CONN_PUBLIC_MYSQL_TCP_SSL_NO_PROJECT_ID'] = \
"gcpcloudsql://{user}:{password}@{public_ip}:{public_port}/{database}?" \
"database_type=mysql&" \
"location={location}&" \
"instance={instance}&" \
"use_proxy=False&" \
"use_ssl=True&" \
"sslcert={client_cert_file}&" \
"sslkey={client_key_file}&" \
"sslrootcert={server_ca_file}".format(**mysql_kwargs)
Using the operator¶
Example operators below are using all connectivity options. Note connection id
from the operator matches the AIRFLOW_CONN_*
postfix uppercase. This is
standard AIRFLOW notation for defining connection via environment variables):
connection_names = [
"proxy_postgres_tcp",
"proxy_postgres_socket",
"public_postgres_tcp",
"public_postgres_tcp_ssl",
"proxy_mysql_tcp",
"proxy_mysql_socket",
"public_mysql_tcp",
"public_mysql_tcp_ssl",
"public_mysql_tcp_ssl_no_project_id"
]
tasks = []
with models.DAG(
dag_id='example_gcp_sql_query',
default_args=default_args,
schedule_interval=None
) as dag:
prev_task = None
for connection_name in connection_names:
task = CloudSqlQueryOperator(
gcp_cloudsql_conn_id=connection_name,
task_id="example_gcp_sql_task_" + connection_name,
sql=SQL
)
tasks.append(task)
if prev_task:
prev_task >> task
prev_task = task
Templating¶
template_fields = ('sql', 'gcp_cloudsql_conn_id', 'gcp_conn_id')
template_ext = ('.sql',)
More information¶
See Google Cloud SQL documentation for MySQL and PostgreSQL related proxies.