Skip to main content

Singer.io tap for extracting data from redshift

Project description

Singer tap that extracts data from a Redshift database and produces JSON-formatted data following the Singer spec.

Usage

tap-redshift assumes you have a connection to Redshift and requires Python 3.6+.

Step 1: Create a configuration file

When you install tap-redshift, you need to create a config.json file for the database connection.

The json file requires the following attributes;

  • host

  • port

  • dbname

  • user

  • password

  • start_date (Notation: yyyy-mm-ddThh:mm:ssZ)

And an optional attribute;

  • schema

Example:

{
    "host": "REDSHIFT_HOST",
    "port": "REDSHIFT_PORT",
    "dbname": "REDSHIFT_DBNAME",
    "user": "REDSHIFT_USER",
    "password": "REDSHIFT_PASSWORD",
    "start_date": "REDSHIFT_START_DATE",
    "schema": "REDSHIFT_SCHEMA"
}

Step 2: Discover what can be extracted from Redshift

The tap can be invoked in discovery mode to get the available tables and columns in the database. It points to the config file created to connect to redshift:

$ tap-redshift --config config.json -d

A full catalog tap is written to stdout, with a JSON-schema description of each table. A source table directly corresponds to a Singer stream.

Redirect output from the tap’s discovery mode to a file so that it can be modified when the tap is to be invoked in sync mode.

$ tap-redshift -c config.json -d > catalog.json

This runs the tap in discovery mode and copies the output into a catalog.json file.

A catalog contains a list of stream objects, one for each table available in your Redshift schema.

Example:

{
    "streams": [
        {
            "tap_stream_id": "sample-dbname.public.sample-name",
            "stream": "sample-stream",
            "database_name": "sample-dbname",
            "table_name": "public.sample-name"
            "schema": {
                "properties": {
                    "id": {
                        "minimum": -2147483648,
                        "inclusion": "automatic",
                        "maximum": 2147483647,
                        "type": [
                            "null",
                            "integer"
                        ]
                    },
                    "name": {
                        "maxLength": 255,
                        "inclusion": "available",
                        "type": [
                            "null",
                            "string"
                        ]
                    },
                    "updated_at": {
                        "inclusion": "available",
                        "type": [
                            "string"
                        ],
                        "format": "date-time"
                    },
                },
                "type": "object"
            },
            "metadata": [
                {
                    "metadata": {
                        "selected-by-default": false,
                        "selected": true,
                        "is-view": false,
                        "table-key-properties": ["id"],
                        "schema-name": "sample-stream",
                        "valid-replication-keys": [
                            "updated_at"
                        ]
                    },
                    "breadcrumb": [],
                },
                {
                    "metadata": {
                        "selected-by-default": true,
                        "sql-datatype": "int2",
                        "inclusion": "automatic"
                    },
                    "breadcrumb": [
                        "properties",
                        "id"
                    ]
                },
                {
                    "metadata": {
                        "selected-by-default": true,
                        "sql-datatype": "varchar",
                        "inclusion": "available"
                    },
                    "breadcrumb": [
                        "properties",
                        "name"
                    ]
                },
                {
                    "metadata": {
                        "selected-by-default": true,
                        "sql-datatype": "datetime",
                        "inclusion": "available",
                    },
                    "breadcrumb": [
                        "properties",
                        "updated_at"
                    ]
                }
            ]
        }
    ]
}

Step 3: Select the tables you want to sync

In sync mode, tap-redshift requires a catalog file to be supplied, where the user must have selected which streams (tables) should be transferred. Streams are not selected by default.

For each stream in the catalog, find the metadata section. That is the section you will modify to select the stream and, optionally, individual properties too.

The stream itself is represented by an empty breadcrumb.

Example:

"metadata": [
    {
        "breadcrumb": [],
        "metadata": {
            "selected-by-default": false,
            ...
        }
    }
]

You can select it by adding "selected": true to its metadata.

Example:

"metadata": [
    {
        "breadcrumb": [],
        "metadata": {
            "selected": true,
            "selected-by-default": false,
            ...
        }
    }
]

The tap can then be invoked in sync mode with the properties catalog argument:

Example (paired with target-datadotworld)

tap-redshift -c config.json --catalog catalog.json | target-datadotworld -c config-dw.json

Step 4: Sync your data

There are two ways to replicate a given table. FULL_TABLE and INCREMENTAL. FULL_TABLE replication is used by default.

Full Table

Full-table replication extracts all data from the source table each time the tap is invoked without a state file.

Incremental

Incremental replication works in conjunction with a state file to only extract new records each time the tap is invoked i.e continue from the last synced data.

To use incremental replication, we need to add the replication_method and replication_key to the streams (tables) metadata in the catalog.json file.

Example:

"metadata": [
    {
        "breadcrumb": [],
        "metadata": {
            "selected": true,
            "selected-by-default": false,
            "replication-method": "INCREMENTAL",
            "replication-key": "updated_at",
            ...
        }
    }
]

We can then invoke the tap again in sync mode. This time the output will have STATE messages that contains a replication_key_value and bookmark for data that were extracted.

Redirect the output to a state.json file. Normally, the target will echo the last STATE after it has finished processing data.

Run the code below to pass the state into a state.json file.

Example:

tap-redshift -c config.json --catalog catalog.json | \
    target-datadotworld -c config-dw.json > state.json

The state.json file should look like;

{
    "currently_syncing": null,
    "bookmarks": {
        "sample-dbname.public.sample-name": {
            "replication_key": "updated_at",
            "version": 1516304171710,
            "replication_key_value": "2013-10-29T09:38:41.341Z"
        }
    }
}

For subsequent runs, you can then invoke the incremental replication passing the latest state in order to limit data only to what has been modified since the last execution.

tail -1 state.json > latest-state.json; \
tap-redshift \
    -c config-redshift.json \
    --catalog catalog.json \
        -s latest-state.json | \
            target-datadotworld -c config-dw.json > state.json

All steps in one Makefile

For your convenience, all the steps mentioned above are captured in the Makefile below. This example uses target-datadotworld but can be modified to use any other Singer target.

# Requires python 3.6
install:
    pip3 install tap-redshift; \
    pip3 install target-datadotworld

# Catalog discovery
discover:
    tap-redshift \
        -c config-redshift.json -d > catalog.json

# Full sync
fullsync:
    tap-redshift \
        -c config-redshift.json \
        --catalog catalog.json | \
            target-datadotworld -c config-dw.json > state.json

# Incremental sync
sync:
    tail -1 state.json > latest-state.json; \
    tap-redshift \
      -c config-redshift.json \
      --catalog catalog.json \
      -s latest-state.json | \
        target-datadotworld -c config-dw.json > state.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tap-redshift-1.0.0b9.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

tap_redshift-1.0.0b9-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tap-redshift-1.0.0b9.tar.gz.

File metadata

  • Download URL: tap-redshift-1.0.0b9.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for tap-redshift-1.0.0b9.tar.gz
Algorithm Hash digest
SHA256 9b3975271ac6600c1ea4e266773697b8cc83cac2a5efc190f21fa21b73af3e07
MD5 96167e7903397620b54272fe4b24d2cd
BLAKE2b-256 a9173417583b6b62f69d3458d0a4697cca8342d7db6e646d7b2f4049270fb648

See more details on using hashes here.

File details

Details for the file tap_redshift-1.0.0b9-py2.py3-none-any.whl.

File metadata

  • Download URL: tap_redshift-1.0.0b9-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for tap_redshift-1.0.0b9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 771cc8176d841ef18bfe467da605fc6b6c2db1b9a1788678672ffd41e56dd693
MD5 cb68a40b75b5ef5087778ed52f1d0dea
BLAKE2b-256 98ea31c57b85cc1c104f9ecbc46abb43346c86a117e0116db88a6dbc8dbc69b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page