Skip to main content

Writing to tables

Here are basic examples of writing data to tables:

Difference between upsert() and write()

Difference between dbconn.upsert() and dbconn.write(): (*) The record is not updated if there are no actual changes. This means that any columns such as timestamp_lastupdate are not updated unnecessarily. This is important in incremental pipelines and data syncs that use timestamp fields to trigger updates in a target (avoid constant triggering of updates/syncs).

dbconn.write(): writing to tables using ELT data pipeline logic

The function dbconn.write() uses ELT data pipeline logic (Singer) to write rows to a target table in a data warehouse. This function will create the table if it does not exist, and it will add or alter columns when needed.
Examples:

Overview of how data is written to the target data warehouse

How dbconn.write writes data to the target data warehouse

Column types

dbconn.write() will first determine the column types of the target table. On the first write, the target table does not exist yet. If an object_schema is set, this will be used to create the columns. If no object_schema is set, introspection will be performed on the rows (input data) to determine the column types to create. From the second write into an existing table, the column types are fixed and will not change, unless an object_schema is set. If there is a conflict between the object_schema and an existing column, the existing column will be split, see detail below. New columns will be added as needed. Once the object_schema is determined (because it is set or because the existing columns are used or because of introspection) each row if the input data is transformed, see betails below.

Setting an object_schema

Example:
Especially when writing in batches, make sure to always send an object_schema, otherwise introspection is done per batch (instead of on the entire data set at once) and this might cause the creation of split columns because in one batch all ages are numeric, and in a second batch there’s a text for the age column.
This example is useful if you want to keep the target table “fixed” after the initial creation with dbconn.write().This script will read the object_schema from the table and apply it for all subsequent writes. The first write is assumed to have happened before so the table is already created.

Transformer

The input rows will be transformed to the object_schema, using the parameter transformer_mode. The default value is “lossless”. Note that if the object_schema is not set, dbconn.write() will look at the existing columns in the target table for the transformations. Options for transformer_mode:
  • lossless (default): the transformer will perform data conversion without losing data, for example 5.21 will not be written into an integer column but it will be written into a string column as “5.21”.
  • lossy: the transformer will perform data conversion on all data while silently ignoring data loss, e.g. 5.21 will be written as 5 if the existing column (or object_schema) is integer.
  • strict: the transformer will strictly validate data against the object_schema, without performing any transformations on the original data, e.g. int will not become string.
  • None: transformation step is skipped.
Example of setting a transformer_mode:

Splitting of columns

If an object_schema is set and this conflicts with existing columns in the target table, the existing column will be split into 2 columns. For example an existing numeric (integer) column age will be split into age__s and age__i when you set an object_schema in which age is a text (string). The __s column will contain string values, the __i column will contain the integer values. In a similar manner an __s (string) and __t (timestamp) column will be created when writing a string (text) to an existing timestamp column. Example:
Here’s a script you can use to fix split columns, and turn them into one column again.Note that one column will be deleted, the data in that column will be lost. This script will also fix the Singer pipeline metadata in the target table.