potiuk commented on a change in pull request #4170: [AIRFLOW-3275] Implement 
Google Cloud SQL Query operator
URL: https://github.com/apache/incubator-airflow/pull/4170#discussion_r232592644
 
 

 ##########
 File path: airflow/contrib/example_dags/example_gcp_sql_query.py
 ##########
 @@ -0,0 +1,257 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Example Airflow DAG that performs query in a Cloud SQL instance.
+
+This DAG relies on the following OS environment variables
+
+* PROJECT_ID - Google Cloud Platform project for the Cloud SQL instance
+* LOCATION - Google Cloud location where the database is created
+*
+* POSTGRES_INSTANCE_NAME - Name of the postgres Cloud SQL instance
+* POSTGRES_USER - Name of the postgres database user
+* POSTGRES_PASSWORD - Password of the postgres database user
+* POSTGRES_PROXY_PORT - Local port number for proxy connections for postgres
+* POSTGRES_PUBLIC_IP - Public IP of the Postgres database
+* POSTGRES_PUBLIC_PORT - Port of the postgres database
+*
+* MYSQL_INSTANCE_NAME - Name of the postgres Cloud SQL instance
+* MYSQL_USER - Name of the mysql database user
+* MYSQL_PASSWORD - Password of the mysql database user
+* MYSQL_PROXY_PORT - Local port number for proxy connections for mysql
+* MYSQL_PUBLIC_IP - Public IP of the mysql database
+* MYSQL_PUBLIC_PORT - Port of the mysql database
+"""
+
+import os
+import subprocess
+
+from six.moves.urllib.parse import quote_plus
+
+import airflow
+from airflow import models
+from airflow.contrib.operators.gcp_sql_operator import CloudSqlQueryOperator
+
+# [START howto_operator_cloudsql_query_arguments]
+
+PROJECT_ID = os.environ.get('PROJECT_ID', 'example-project')
+LOCATION = os.environ.get('REGION', 'europe-west-1')
+
+POSTGRES_INSTANCE_NAME = os.environ.get('POSTGRES_INSTANCE_NAME', 
'testpostgres')
+POSTGRES_DATABASE_NAME = os.environ.get('POSTGRES_DATABASE_NAME', 'postgresdb')
+POSTGRES_USER = os.environ.get('POSTGRES_USER', 'postgres_user')
+POSTGRES_PASSWORD = os.environ.get('POSTGRES_PASSWORD', 'password')
+POSTGRES_PUBLIC_IP = os.environ.get('POSTGRES_PUBLIC_IP', '0.0.0.0')
+POSTGRES_PUBLIC_PORT = os.environ.get('POSTGRES_PUBLIC_PORT', 5432)
+POSTGRES_CLIENT_CERT_FILE = os.environ.get('POSTGRES_CLIENT_CERT_FILE',
+                                           "/tmp/client-cert.pem")
+POSTGRES_CLIENT_KEY_FILE = os.environ.get('POSTGRES_CLIENT_KEY_FILE',
+                                          "/tmp/client-key.pem")
+POSTGRES_SERVER_CA_FILE = os.environ.get('POSTGRES_SERVER_CA_FILE',
+                                         "/tmp/server-ca.pem")
+
+MYSQL_INSTANCE_NAME = os.environ.get('MYSQL_INSTANCE_NAME', 'testmysql')
+MYSQL_DATABASE_NAME = os.environ.get('MYSQL_DATABASE_NAME', 'mysqldb')
+MYSQL_USER = os.environ.get('MYSQL_USER', 'mysql_user')
+MYSQL_PASSWORD = os.environ.get('MYSQL_PASSWORD', 'password')
+MYSQL_PUBLIC_IP = os.environ.get('MYSQL_PUBLIC_IP', '0.0.0.0')
+MYSQL_PUBLIC_PORT = os.environ.get('MYSQL_PUBLIC_PORT', 3306)
+MYSQL_CLIENT_CERT_FILE = os.environ.get('MYSQL_CLIENT_CERT_FILE',
+                                        "/tmp/client-cert.pem")
+MYSQL_CLIENT_KEY_FILE = os.environ.get('MYSQL_CLIENT_KEY_FILE',
+                                       "/tmp/client-key.pem")
+MYSQL_SERVER_CA_FILE = os.environ.get('MYSQL_SERVER_CA_FILE',
+                                      "/tmp/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',
+]
+
+default_args = {
+    'start_date': airflow.utils.dates.days_ago(1)
+}
+
+# [END howto_operator_cloudsql_query_arguments]
+
+
+# [START howto_operator_cloudsql_query_connections]
+
+postgres_kwargs = dict(
+    user=quote_plus(POSTGRES_USER),
+    password=quote_plus(POSTGRES_PASSWORD),
+    public_port=POSTGRES_PUBLIC_PORT,
+    public_ip=quote_plus(POSTGRES_PUBLIC_IP),
+    project_id=quote_plus(PROJECT_ID),
+    location=quote_plus(LOCATION),
+    instance=quote_plus(POSTGRES_INSTANCE_NAME),
+    database=quote_plus(POSTGRES_DATABASE_NAME),
+    client_cert_file=quote_plus(POSTGRES_CLIENT_CERT_FILE),
+    client_key_file=quote_plus(POSTGRES_CLIENT_KEY_FILE),
+    server_ca_file=quote_plus(POSTGRES_SERVER_CA_FILE)
+)
+
+# Postgres: connect via proxy over TCP
+os.environ['AIRFLOW_CONN_PROXY_POSTGRES_TCP'] = \
 
 Review comment:
   Sure!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to