I am trying to implement a ETL process with Dataform for an ad agency. This process consists of:
-
Get all the data from Google Ads. This step happens in Data Transfer Service.
-
Based on those data create a unified table with performance data like clicks etc. This table contains perfomance data about all the clients. This step happens in Dataform.
-
Based on the unified table create a table for each client. Those client tables have the same schema as the table on step 2. This step happens in Dataform.
BUT: To perform the step 3, we want to fetch the clients ID on an external service. Based on the returned client ID list we want to perform an iteration with a JS file on Dataform and then create a table for each client.
I’ve already tried this, but it simply didn’t work, maybe beacause this is an async operation. I just wanted to ask then if Dataform accepts async operations. If it doesn’t accept, I’d like to know if there is another strategy to get the IDs in Dataform and gererate one table per ID base on the unified table on step 2. Would that be possible in Dataform?
Thanks a lot!
Hi @marciodosanjos,
Welcome to Google Developer Program Forums!
Dataform’s compilation process is designed to be hermetic and deterministic. This means that when Dataform compiles your SQLX and JavaScript files, it must be able to precisely predict the SQL code it will generate without relying on external network requests.
Compilation vs. Execution
-
Compilation: Dataform converts your SQLX and JavaScript code into executable BigQuery SQL statements. This occurs in a secure sandbox environment without internet access.
-
Execution: The compiled SQL statements are then executed within BigQuery.
Asynchronous Operations and Their Limitations
When your JavaScript attempts to fetch client IDs from an external API, it involves an asynchronous network operation. Allowing such calls during compilation would make the resulting SQL dependent on external data, causing compilation to become non-deterministic, slower, and potentially unreliable. Dataform must know exactly which tables to create and the SQL queries to run before starting the execution phase in BigQuery. Because of this, Dataform’s core compilation environment does not support making live asynchronous API calls from within JavaScript files to dynamically generate SQL actions.
While direct asynchronous calls during compilation are not supported, you can still generate a table for each client ID by decoupling the client ID retrieval from Dataform’s compilation process. One effective approach is to preload the list of client IDs into BigQuery prior to running your Dataform workflow. Here’s how you can do it:
-
Create a Client ID Table: Utilize an independent process, such as a Cloud Function, scheduled script, or Airflow workflow to retrieve client IDs from your external service and store them in a BigQuery table, for example, external.client_ids
-
Reference the Table in Dataform: Within your Dataform JavaScript file, you can synchronously access the client ID list by querying BigQuery metadata. Note that dataform.query() is available only in the open-source version of Dataform. If you’re using Dataform on Google Cloud, you’ll need to use alternatives such as importing a static list through includes or defining the client IDs via declarations.
-
Cloud Composer / Airflow for Orchestration: For complex client ID retrieval or better control, use Cloud Composer (managed Airflow).
-
Option 1: Use a PythonOperator to fetch client IDs from your external service and either load them into a BigQuery table (recommended) or pass them via XCom.
-
Option 2: Trigger your Dataform workflow using DataformCreateWorkflowInvocationOperator or a custom Airflow script.
Here are some useful resources to help you gain a deeper understanding of Dataform and its capabilities:
- Dataform Overview: This serves as the primary hub for Dataform resources, covering quickstarts, API docs, and workflow guides.
- Custom SQL Operations in Dataform: Learn how to define custom SQL operations using config { type: “operations” }, which can be useful for creating referenceable tables or running post-processing logic.
- Dataform GitHub Repository: Explore the open-source core of Dataform, including examples, CLI usage, and community contributions.
- GCP Dataform – Comprehensive guide with best practices, execution patterns, and advanced orchestration strategies.
You can also visit this post for more learning videos about Dataform.
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
Hi @dawnberdan, thanks a lot for the very detailed and comprehensive response. I am using dataform on GacP and I already have the list of clients in a BigQuery table. As I understand from your answer, the only alternative in my case is to import a static list, right? Or is there some way to retrieve the data from the table dynamically and then passing them to the other JS functions? Thanks a lot!
Hi @marciodosanjos,
Yes, there’s a way to dynamically retrieve data from a BigQuery table in Dataform on GCP to iterate through it and create a table for each client. While you can’t use asynchronous API calls, you can still reference data from an existing BigQuery table within your Dataform JavaScript files.
Hi @dawnberdan thanks again! Could you show me a small example of it? I’ve tried to query the IDs and store them in a variable like that
const ids = () = > {return `SELECT…`}
ids.forEach(id => //generate table}
But this didn’t work .