Last modified November 22, 2014 by Gordon Glauser

Automating User DB Imports

Tutorial objective: Automate the download and import of User DB data sets from Swrve.

If you are using the Swrve Insights hosted solution, you can skip this tutorial as Swrve already loads all of this data.

This tutorial provides examples of how to automate the download and import of User DB data sets from Swrve.

The below examples demonstrate how to load a User DB using the Swrve Export API for User DBs. The APIs are called from scripts that can be run periodically. Swrve supports both Amazon Redshift and MySQL databases.


Prerequisites

The following prerequisites are required to complete this tutorial:

  • Experience using the command line shell.
  • Experience with basic SQL commands such as SELECT, WHERE and JOIN. As the data format is straightforward, advanced SQL knowledge is not essential.
  • You have installed MySQL or have an active Amazon Redshift Cluster.

Scripted Import

You can easily create a script to download the user database files for a given app and populate a Redshift or MySQL database. This script should complete the following steps:

  1. Create the required database tables.
  2. Download the CSV data files to your local computer.
  3. Import the CSV data files into the database.

Step 1: Create the required database tables

Create a database called “app_12345” for Swrve app 12345 using the following command:

Redshift

MySQL

You can download the Schema file from the User DB Downloads screen. To automate this, download the Redshift schema file as referenced by the Export API.

This article references Swrve’s URLs for all data and content stored in both our US and EU data centers. Use the relevant example based on your app configuration. For more information, see How Do I Configure the Swrve SDK for EU Data Storage?

US

EU

More details on the UserDB Export API is available in the Export API guide.

From the JSON supplied by this API, you are given the address of your schema file. Next, you need to download this file. In pseudo-code (again for Swrve app 12345):

Redshift

MySQL

This file is a SQL document, which you can execute to create the required tables for your CSV data:

Redshift

MySQL

Step 2: Download the CSV data files to your local computer

You can download the CSV data files from the User DB Downloads screen. To automate this, download the CSV data files using the Export API.

US

EU

Redshift

Redshift requires that the files being imported exist in an Amazon S3 bucket.

Upload each of these files to an S3 bucket. If you have enabled raw event exports, you can use s3://swrveexternal-<company_name>/app-<app_id>/userdb/. If not, use a bucket of your choosing and adjust the below commands appropriately.

These files can be uploaded as compressed (that is, as is), as Redshift supports gzip compression.

MySQL

The data for your users is contained within compressed CSV files. Decompress each of these CSV files. MySQL import does not support gzip compress and therefore you must first decompress before use.

Step 3: Import the CSV data files into the database

Redshift

From the psql terminal, run this command per data set.

Avoid duplicate data. Redshift does not enforce UNIQUE constraints. If you run the above commands multiple times, data will be duplicated. We suggest dropping all relevant tables before importing new data.

MySQL

From the MySQL terminal, run this command per data set.

Because of foreign key constraints, you must import the swrve_properties table before importing the other tables.

Next Steps

That’s it! With user properties loaded, you can query the current state of every user who has used your app in the last year.


Need a hosted solution?
Most Swrve customers can self-host this pipeline; all you have to do is follow the steps in these tutorials. However, if you prefer a turn-key hosted solution, we do offer one as a professional service. There is an additional fee associated with this service. For more information, contact your Customer Success Manager at support@swrve.com.

Need help with Queries?
Swrve support can help you with basic setup and configuration of the pipeline described above. If you need help with your queries, contact our Data Services team at support@swrve.com. They will help you get the most out of your data with dedicated support, pre-built and custom reports, and dedicated data science hours.