Loading Terabytes of Data from Postgres into BigQuery
Despite the fact that an ETL task is pretty challenging when it comes to loading Big Data, there’s still the scenario in which you can load terabytes of data from Postgres into BigQuery relatively easy and very efficiently.
Despite the fact that an ETL task is pretty challenging when it comes to loading Big Data, there’s still the scenario in which you can load terabytes of data from Postgres into BigQuery relatively easy and very efficiently. This is the case when you have a lot of immutable data distributed in tables by some timestamp. For example, a
transactions table with a
created_at timestamp column. BigQuery and Postgres have great tools in order to do this pretty fast and conveniently.
Preparing Postgres Tables
BigQuery Capacitor storage format, as many other Big Data formats, is optimized for a one-time write of an entire table. To distribute data between tables, BigQuery heavily relies on the wild card tables pattern. Typical usage is to create tables with names suffixed by some field value. Usually, date suffixes are used for this purpose. For example, the table
transactions20180301 can contain transactions created on March 1, 2018. This approach allows for both the creation of very efficient storage on the BigQuery side, and for ease of loading the data source on the storage side.
In order to implement this loading approach we need to ensure we have the right indexes in the Postgres database before we start loading processes from Postgres into BigQuery. In order to create this, you can use psql to connect to your database. Let’s create an index for a
created_at timestamp in the
transactions table. Assuming we’re dealing with an ubuntu machine that hosts the db instance, it can be as simple as:
Index creation can take a while if you have a lot of data. You can track process execution using
top utility. You should repeat index creation for each table you’re going to load into BigQuery.
Loading Data into BigQuery
To load data into BigQuery we’re going to use BigQuery CLI, which is a very versatile tool. You can install it using these instructions. As we’re on Linux, we’ll be using bash script in order to perform all the work. I assume BigQuery CLI is installed and authorized.
bigquery-upload.sh and add the following function in order to upload a single day from a specific table:
This function has three arguments: table, columns for selection, and date to upload. As you can see, it uses the
\copy operation to download a CSV from Postgres and then compresses it. BigQuery docs says loading of a compressed CSV is slower than uncompressed, but uploading uncompressed data almost always seems slower.
You can call this function as simply as adding a line at the end of the script:
This will create the
transactions20180301 table with one day of data. To automate the uploading process we can introduce another function:
upload_table function has 4 arguments: table name, columns for select, start dates, and end dates.
You can make this script parametrized by adding
upload_table "$1" '*' "$2" "$3" as the last line so that entire script will be:
Don’t forget to
$ chmod +x bigquery-upload.sh your script. After it you can run it as
The great thing about this script of loading data from Postgres into BigQuery is you can use it to upload just a single day and it can be put in the
/etc/cron.hourly directory so that you can get your data in nearly real time. The
--replace option script can be run more than once for the same day to replace data as it is refreshed.
If all is done right, it should load your terabyte of data in a day or so. Please do not hesitate to contact us and we would love to advice you.
Original. Reposted with permission.
- Calculating Customer Lifetime Value: SQL Example
- Which Database is best for an Analyst?
- A Beginner’s Guide to Data Engineering – Part I
Top Stories Past 30 Days