Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Loading Terabytes of Data From Postgres Into BigQuery

DZone's Guide to

Loading Terabytes of Data From Postgres Into BigQuery

We take a look at how you can ingest large amounts of data from a PostgreSQL database using the popular BigQuery framework.

· Big Data Zone ·
Free Resource

How to Simplify Apache Kafka. Get eBook.

Despite the fact that an ETL task is pretty challenging when it comes to loading big data sets, there’s still the scenario in which you can load terabytes of data from Postgres into BigQuery relatively easily 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 quickly and conveniently.

Preparing Postgres Tables

BigQuery's Capacitor storage format, like 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 both for 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 theDBb instance, it can be as simple as:

$ sudo su - postgres
$ psql yourdbname

yourdbname=> create index transactions_created_at on transactions (created_at);

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.

Let’s create bigquery-upload.sh and add the following function in order to upload a single day from a specific table:

#!/bin/bash

function upload_day {
  table=$1
  sel=$2
  day=$3
  next_day=$(date -d "$day+1 days" +%Y-%m-%d)
  bq_suffix=$(date -d "$day" +%Y%m%d)
  echo "Uploading $table: $day..."
  psql <yourdbname> -c "\\copy (select $sel from $table where created_at >= '$day' and created_at < '$next_day') TO '$table-$day.csv' WITH CSV HEADER"
  gzip $table-$day.csv
  bq load --allow_quoted_newlines --project_id <your-project-id> --replace --source_format=CSV --autodetect --max_bad_records 100 <yourdbname>.$table$bq_suffix $table-$day.csv.gz
  rm $table-$day.csv.gz
};

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. The BigQuery docs say the loading of a compressed CSV is slower than uncompressed, but uploading uncompressed data almost always seems slower.

You can call this function by simply adding a line at the end of the script:

upload_day 'transactions' '*' '2018-03-01'

This will create the transactions20180301 table with one day of data. To automate the uploading process we can introduce another function:

function upload_table {
  t=$1
  s=$2
  start_date=$3
  end_date=$4
  while [ "$start_date" != "$end_date" ]; do
       upload_day "$t" "$s" "$start_date"
       start_date=$(date -d "$start_date+1 days" +%Y-%m-%d)
  done
}

This 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 the entire script will be:

#!/bin/bash

function upload_day {
  table=$1
  sel=$2
  day=$3
  next_day=$(date -d "$day+1 days" +%Y-%m-%d)
  bq_suffix=$(date -d "$day" +%Y%m%d)
  echo "Uploading $table: $day..."
  psql <yourdbname> -c "\\copy (select $sel from $table where created_at >= '$day' and created_at < '$next_day') TO '$table-$day.csv' WITH CSV HEADER"
  gzip $table-$day.csv
  bq load --allow_quoted_newlines --project_id <your-project-id> --replace --source_format=CSV --autodetect --max_bad_records 100 <yourdbname>.$table$bq_suffix $table-$day.csv.gz
  rm $table-$day.csv.gz
};

function upload_table {
  t=$1
  s=$2
  start_date=$3
  end_date=$4
  while [ "$start_date" != "$end_date" ]; do
       upload_day "$t" "$s" "$start_date"
       start_date=$(date -d "$start_date+1 days" +%Y-%m-%d)
  done
}

upload_table "$1" '*' "$2" "$3"

Don’t forget to $ chmod +x bigquery-upload.sh your script. After it, you can run it as:

$ ./bigquery-upload.sh "transactions" "2017-01-01" "2018-03-01"

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.daily or /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 leave your comments!

12 Best Practices for Modern Data Ingestion. Download White Paper.

Topics:
big data ,bigquery ,postgres ,data ingestion

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}