In my latest webinar I talked about the shift that has taken place from ETL to ELT for data pipelines. More and more pipelines today are built as ELT. Simply put, this is because ELT pipelines are much less resource-intensive in both the short and the long term.
The aim of this article is to give you as a reader an understanding of how an ELT pipeline works in practice.
I will go through the prerequisites and the code for a made-up example, to make it easier for you to understand. This is the guide I wish I had when I started exploring the area. For me, the penny only dropped once I had seen an example of an implementation.
Before I start on that, I want to go through the differences between ETL & ELT at a higher level, and share some thoughts on what has driven the change.
The difference between ETL and ELT
In the previous paradigm, data was moved between sources (APIs, databases, file systems) and analytics platforms using what are called ETL pipelines. There, data is processed in this order:
- Extract. The data is extracted from its source
- Transform. The data is cleaned and transformed.
- Load. The data is loaded into an analytics platform so that it can create value
In general, steps 1 (Extract) and 3 (Load) are trivial. They are steps that every company with its data in the same source will need to build and maintain.
What sets company A apart from company B is usually just what they do with the data in step 2 (Transform).
This is where what is unique to the company’s business, and how it wants to create value with the data, happens.
But since all three steps used to sit in the same service/program, it wasn’t entirely easy to separate them. Standalone libraries/SDKs for APIs for the Extract and Load steps of course already existed, but since every company wanted its own Transform, they also had to connect the parts into a program themselves. We also need to remember that this was before massively parallel databases in the cloud (Redshift, BigQuery & Snowflake) existed. Which meant there was also a performance reason to keep the transformation in the same flow as the extracting and loading. The data that landed in the analytics platform had to be as ready for analysis as possible.
The shift from ETL to ELT began when AWS released Redshift to the world.
AWS was first to make a managed service out of this kind of technology. Suddenly a lot of power became available in the cloud for little money. What had previously been an argument for placing the transformation outside the data warehouse – the limited power of the data warehouse – now became an argument against. If we can just get the data into the DW, we can do whatever we want with it there.
But a tool was still needed to orchestrate the transformation of the data in the DW. A tool where we can keep track of where the relatively raw input data lands and what we then do with it. A tool that was popular among data engineers for building ETL pipelines was Airflow. Airflow is a tool for orchestration and scheduling that really excels when complex workflows need orchestrating across several parts of the IT architecture. The downside is that Airflow is a relatively resource-intensive tool to get started with.
Some people who saw the limitations of using tools like Airflow to transform data in the data warehouse were Fishtown Analytics (now dbt Labs). Instead, they saw the opportunity to build a tool focused on making it as easy and robust as possible to transform data within a cloud data warehouse.
I’ll spend the rest of the article explaining how the T in ELT can be built in dbt. I hope it becomes clear how much it changes the requirements on the EL steps.
A detailed look at the T in ELT
So what needs to be done in the Transform step? The most basic things are the following:
- Reading fields from unstructured source data
- Casting these fields to the correct types and naming them consistently
- Removing duplicates
- Tests / error handling
To show and explain how we can do this in dbt, we need some example data to start from. In the table below, I’ve put in data that we can imagine is generated by a service that handles EL. Every hour, data from the last three days is loaded from a source and saved in the table completely untouched. The data comes from a service that handles incoming and outgoing calls for customer support.
| _loaded_at | _loaded_data |
|---|---|
| 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:08:12”, ”number”: ”0703857940”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”4.2″}} |
| 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:07:58”, ”number”: ”0765831292”, ”direction”: ”Outgoing”, ”Outcome”: Unanswered”, ”minutes”:”0″} |
| 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:08:12”, ”number”: ”0703857940”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”4.2″} |
where _loaded_at contains a timestamp of when the data was loaded, and _loaded_data contains the data in JSON format.
Step 1: Reading fields from source data in JSON format
The first thing we need to do is unpack the JSON data in _loaded_data into columns. Treating a string as JSON and getting the value for a specific key (or a key in a list) is supported by all common DWs. The example below is written for BigQuery, but I think you can quite easily translate it into your DW’s syntax
which generates the following table:
| call_time | caller_number | call_type | call_outcome | call_duration | _loaded_at | _loaded_data |
|---|---|---|---|---|---|---|
| 2021-12-06T09:10:12 | 0708887950 | Incoming | Answered | 0.7 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06T09:08:12 | 0703857940 | Incoming | Answered | 4.2 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06T09:07:58 | 0765831292 | Outgoing | Unanswered | 0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:07:58”, ”number”: ”0765831292”, ”direction”: ”Outgoing”, ”Outcome”: Unanswered”, ”minutes”:”0″} |
| 2021-12-06T09:10:12 | 0708887950 | Incoming | Answered | 0.7 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06T09:08:12 | 0703857940 | Incoming | Answered | 4.2 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
There we go! Now it’s starting to look like something. Note that I’ve also given the columns new names, to be more consistent with other data in our (made-up) organisation.
Step 2: Casting fields to the correct types
But we can definitely do something about the data quality. For example, the date is still read as a string, the values in type and outcome can be standardised and made easier to query, and the duration metric becomes easier to work with if it’s defined in seconds. This is what the code to fix that looks like:
which generates the following table:
| call_time | caller_number | call_type | call_outcome | call_duration | _loaded_at | _loaded_data |
|---|---|---|---|---|---|---|
| 2021-12-06 08:10:12 UTC | 0708887950 | incoming | answered | 42.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06 08:08:12 UTC | 0703857940 | incoming | answered | 252.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06 08:07:58 UTC | 0765831292 | outgoing | unanswered | 0.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:07:58”, ”number”: ”0765831292”, ”direction”: ”Outgoing”, ”Outcome”: Unanswered”, ”minutes”:”0″} |
| 2021-12-06 08:10:12 UTC | 0708887950 | incoming | answered | 42.0 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 2021-12-06 08:08:12 UTC | 0703857940 | incoming | answered | 252.0 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
Step 3: Removing duplicates
But one problem we’ve put off until later is that we have duplicates in the data. Time to deal with that now.
We have duplicates because our EL pipeline is relatively dumb: “Every hour, the last three days of data are synced from the source.” So the EL pipeline doesn’t bother checking whether a row has already been synced from the source to the DW. That makes the EL part of the pipeline less complex, but we’ve moved the problem to the Transform step.
To solve this, we can use a method that relies on surrogate keys. A surrogate key is a key we can use to identify unique rows. To create one, we first need to work out how we can identify that a row in the input data is unique. For this example, we can say that each unique row should have a unique combination of values in column_a, b, c, d and so on. This is how we calculate each row’s surrogate_key
| call_surr_key | call_surr_key_rn | call_time | caller_number | call_type | call_outcome | call_duration | _loaded_at | _loaded_data |
|---|---|---|---|---|---|---|---|---|
| 0cc175b9… | 2 | 2021-12-06 08:10:12 UTC | 0708887950 | incoming | answered | 42.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 92eb5ff… | 2 | 2021-12-06 08:07:58 UTC | 0703857940 | incoming | answered | 252.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 4a8a08f09… | 1 | 2021-12-06 08:07:58 UTC | 0765831292 | outgoing | unanswered | 0.0 | 2021-12-06 13:04:14 UTC | {”call_time”: ”2021-12-06T09:07:58”, ”number”: ”0765831292”, ”direction”: ”Outgoing”, ”Outcome”: Unanswered”, ”minutes”:”0″} |
| 0cc175b9… | 1 | 2021-12-06 08:10:12 UTC | 0708887950 | incoming | answered | 42.0 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”Incoming.”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
| 92eb5ff… | 1 | 2021-12-06 08:08:12 UTC | 0703857940 | incoming | answered | 252.0 | 2021-12-05 13:02:10 UTC | {”call_time”: ”2021-12-06T09:10:12”, ”number”: ”0708887950”, ”direction”: ”Incoming”, ”Outcome”: Answered”, ”minutes”:”0.7″} |
As we can see, we have two rows where the surrogate_key is “0cc175b9…” and “92eb5ff…” respectively. One of these rows is a duplicate and can be thrown away. To identify them, I’ve used the window function row_number, which gives each row with the same surrogate key a number from one upwards. Which number each row gets depends on how we sort the window.
Which row we throw away is irrelevant in this case, since both rows have exactly the same values. But there are situations where we have a preference for which one we want to keep. Imagine, for example, that we had been working with data from Facebook Ads. We would have had two or more rows for a campaign’s results on a given day, with different numbers of conversions depending on when the data was fetched from Facebook. That’s because Facebook uses an attribution window that lets conversions trickle in on an activity a certain number of days into the future. In that case, we would have wanted to throw away all rows except the most recently loaded one for a unique surrogate key.
But in our case, we’re now nearly done making the data ready for analysis. All that remains is to actually filter out the duplicates and drop fields that belong to the source.
| call_surr_key | call_time | caller_number | call_type | call_outcome | call_duration |
|---|---|---|---|---|---|
| 0cc175b9… | 2021-12-06 08:10:12 UTC | 0708887950 | incoming | answered | 42.0 |
| 92eb5ff… | 2021-12-06 08:07:58 UTC | 0703857940 | incoming | answered | 252.0 |
| 4a8a08f09… | 2021-12-06T09:07:58 | 0765831292 | outgoing | unanswered | 0.0 |
Neat, right? 🙂
Step 4: Tests / error handling in dbt
Now we’re almost home. The table we’ve created is cleaned and ready to be used for analysis. At this stage, it’s also a good idea to capture the expectations we have of the data in tests. Tests mean we can trust that the data keeps flowing in and keeps the same format as before.
Tests I would have wanted to set up in this case are:
- Freshness. How old is the latest data point we have loaded right now? We want a warning if it looks like new input data isn’t being loaded.
- Expected values. Which values do we expect in different columns? Imagine if the input data started containing values other than incoming or outgoing for call_type.
- Uniqueness of the surrogate_key. We want to be completely sure that we don’t accidentally load duplicates for the same row in the future. This test catches any errors if someone were to change the code and forget that requirement.
dbt makes this very easy. Using a .yml file, we can easily map out which fields are in our table and what assumptions we have about them. dbt will then test our table every time it’s rebuilt on new input data.
All together
It’s no harder than that. With a very simple integration that just pushes data into a table, we can make the data correct and ready for analysis with relatively little SQL and dbt.
Want all the code for reference? Here you’ll find the dbt model (the SQL code), and here its accompanying documentation (the yml file).
What does this mean?
As you can see, it’s both robust and simple to set up the T in ELT with dbt. Which means we can significantly lower the complexity of/requirements on the EL steps. It has gone so far that you can get raw data delivered to your DW with the help of SaaS players like Fivetran or Stitch.
There are also two open source projects that want to democratise access to integrations completely. One of their arguments is that it’s the only way to handle “the long tail of integrations”, by which they mean that Fivetran/Stitch will always find it hard to justify maintaining less popular integrations from an economic point of view. By making the integration code fully available, Airbyte and Meltano make it possible for every company to go in and fix/maintain any integrations that are unique to them, while getting the rest “for free”.
It’s an interesting space where a lot is happening. A growing share of what used to be expensive and complex data engineering can be solved with sharp tools created by communities. We can spend an increasing share of our brainpower on creating value from the data.
I hope you learned something new from this article, whatever your role.
Do you have a question, or are you interested in talking more? Add me on LinkedIn or write in the dbt Slack 🙂