<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Usman Ali on Medium]]></title>
        <description><![CDATA[Stories by Usman Ali on Medium]]></description>
        <link>https://medium.com/@usman-ali?source=rss-6dbb8d60a6b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xqtJYCrc3GIx0FS-</url>
            <title>Stories by Usman Ali on Medium</title>
            <link>https://medium.com/@usman-ali?source=rss-6dbb8d60a6b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:14:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@usman-ali/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Cloud Composer/Apache Airflow, Dataform & BigQuery]]></title>
            <link>https://medium.com/google-cloud/cloud-composer-apache-airflow-dataform-bigquery-de6e3eaabeb3?source=rss-6dbb8d60a6b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/de6e3eaabeb3</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[bigquery]]></category>
            <category><![CDATA[google-cloud-composer]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Usman Ali]]></dc:creator>
            <pubDate>Sun, 28 Mar 2021 10:14:12 GMT</pubDate>
            <atom:updated>2021-04-06T06:45:34.269Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How to use Cloud Composer and Dataform together</em></p><blockquote><em>Disclaimer: Opinions are my own and not the views of my employer</em></blockquote><p><a href="https://cloud.google.com/composer">Cloud Composer</a> is Google’s fully managed version of <a href="https://airflow.apache.org/">Apache Airflow</a> and is ideal to write, schedule and monitor workflows. Google recently acquired <a href="https://dataform.co/">Dataform</a> which is everything about Transform in ELT (Extract Load Transform). This is a great news for <a href="https://cloud.google.com/bigquery">BigQuery</a> lovers as Dataform can help manage scripts, create dependencies, add data quality checks (aka assertions), document, maintain versions and push changes into different environments with <a href="https://docs.dataform.co/dataform-web/version-control">native git integration</a>. It offers UI, CLI and APIs to do all this, in short its awesome!</p><p>So if you are using or planning to use BigQuery as your DWH, Cloud Composer as your main orchestration tool and are fond of ELT then you have come to the right place. I will be showcasing how these products can talk to each-other based on a typical use-case as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yeHnXjpgRATD5tKUPlppDg.png" /></figure><p>As shown above, Composer is the main Orchestrator of all this + simple loading using gcs_to_bq Operator into BigQuery (which can replaced by any other EL(T) solution as well). Transformation is kept in SQLX in Dataform. There are plenty of good articles already on the Dataform so I am using one of them <a href="https://medium.com/google-cloud/building-sql-pipelines-in-bigquery-with-dataform-part-1-9e96f14ec664">created by Lak</a> which is solving BigQuery ML use-case using Dataform as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CIWCW9Hu43PhGcnfwsr_0w.png" /></figure><p>Dataform has its own scheduler too but normally you would want to run your Transformation after Loading is done and, for that sort of dependency chaining across different applications, Composer is coming to the rescue again.</p><p>So lets cut the chase and get to the code:</p><p><strong>Waiting for a file in GCS</strong></p><p>Our Transformation needs input data to be loaded into BigQuery. Assume that this data will arrive into GCS by an external job and we need to wait for that. For this we can use GoogleCloudStorageObjectSensor operator as below:</p><pre>from airflow.contrib.sensors.gcs_sensor import GoogleCloudStorageObjectSensor</pre><pre>gcs_sensor_task = GoogleCloudStorageObjectSensor(<br> task_id=”gcs_object_sensor”,<br> bucket=’gcs-bucket’,<br> object=’test-files/census_adult_income.csv’,<br> dag=dag<br>)</pre><p><strong>Load from GCS to BigQuery</strong></p><p>Once data has arrived into GCS, we can use GoogleCloudStorageToBigQueryOperator operator to load into BigQuery using code below. You can alternatively specify schema in JSON as well, more details <a href="https://airflow.apache.org/docs/apache-airflow/1.10.12/_api/airflow/contrib/operators/gcs_to_bq/index.html">here</a>.</p><pre>from airflow.contrib.operators import gcs_to_bq</pre><pre>load_gcs_csv = gcs_to_bq.GoogleCloudStorageToBigQueryOperator(<br> task_id=’gcs_to_bq_load’,<br> bucket=’gcs-bucket’,<br> source_objects=[‘test-files/census_adult_income.csv’],<br> destination_project_dataset_table=’gcs-bucket.ds.census_adult_income’,<br> schema_fields=[<br> {‘name’: ‘age’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘workclass’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘functional_weight’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘education’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘education_num’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘marital_status’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘occupation’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘relationship’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘race’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘sex’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘capital_gain’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘capital_loss’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘hours_per_week’, ‘type’: ‘INTEGER’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘native_country’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> {‘name’: ‘income_bracket’, ‘type’: ‘STRING’, ‘mode’: ‘NULLABLE’},<br> ],<br> skip_leading_rows=1,<br> write_disposition=’WRITE_TRUNCATE’,<br> dag=dag)</pre><p><strong>Call Dataform for Transformation</strong></p><p>Data loaded by the code above into BigQuery table ‘census_adult_income’ is an input to transformations in Dataform, so it’s time to run Dataform job. As of now there isn’t any Dataform operator in Composer however we can use REST API call via PythonOperator to call and poll for job to finish iteratively. More details on Dataform APIs <a href="https://docs.dataform.co/dataform-web/api">here</a>.</p><p>Although you are going to use Composer for orchestration you are still going to need to create a schedule in Dataform for REST API call. You can disable that as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JtY5jZ6pDC_iHBbNH0rVeA.png" /></figure><p>Rest API also needs a project Id (not to be confused with GCP Project Id), I couldn’t find a place in Dataform UI which mentions it explicitly however its visible in the URL as highlighted below, so copy it from there (and I will update here if I find a better way!).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*CIn-0bpVtyXs04WCzaH1IA.png" /></figure><p>You can create Dataform API key as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_2kM6aRuTvc52yizUYUDrw.png" /></figure><p>And make the call:</p><pre>from airflow.operators.python_operator import PythonOperator<br>import requests<br>import json<br>import time</pre><pre>def dataform_def():<br> base_url=’<a href="https://api.dataform.co/v1/project/5685548636176384/run&#39;">https://api.dataform.co/v1/project/<strong>&lt;Project Id here&gt;</strong>/run&#39;</a><br> headers={‘Authorization’: ‘Bearer &lt;<strong>Dataform Key&gt;</strong>’}<br> run_create_request={“environmentName”: “&lt;<strong>environment name here&gt;</strong>”, “scheduleName”: “&lt;<strong>schedule name from dataform here&gt;</strong>”}</pre><pre>response = requests.post(base_url, data=json.dumps(run_create_request), headers=headers)</pre><pre>run_url = base_url + ‘/’ + response.json()[‘id’]<br> response = requests.get(run_url, headers=headers)</pre><pre>while response.json()[‘status’] == ‘RUNNING’:<br> time.sleep(10)<br> print(‘Dataform job running’)<br> response = requests.get(run_url, headers=headers)<br> print(response.json())</pre><pre>return ‘Dataform job finished’</pre><pre>run_dataform = PythonOperator(<br> task_id=’dataform_transform’,<br> python_callable=dataform_def,<br> dag=dag,<br>)</pre><p><strong>Stitching it together</strong></p><p>Last part is to chain it all together in a Composer DAG:</p><pre>gcs_sensor_task &gt;&gt; load_gcs_csv &gt;&gt; run_dataform</pre><p>Move your DAG in Composer bucket:</p><pre>gsutil cp census_elt_dag.py gs://composer-bucket</pre><p>Composer will automatically pick this new DAG:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*zGRQmmrqCCCtsqeatSsDAg.png" /></figure><p>You can schedule and monitor it directly from Composer.</p><p>Thats it! Ping me for any questions or suggestions for improvement.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de6e3eaabeb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/cloud-composer-apache-airflow-dataform-bigquery-de6e3eaabeb3">Cloud Composer/Apache Airflow, Dataform &amp; BigQuery</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>