Hubspot to Redshift

This page provides you with instructions on how to extract data from Hubspot and load it into Redshift. (If this manual process sounds onerous, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

Pulling Data Out of Hubspot

The first step of getting all that beautiful Hubspot data into Redshift is actually pulling that data off of Hubspot’s servers. You can do this using the Hubspot REST API, which is available to all Hubspot customers. Full API documentation can be accessed here.

Data from the Hubspot API can be retrieved programmatically. Hubspot’s API offers access to calendar, companies, contacts, deals and a lot more. Using methods outlined in their API documentation, you can retrieve the data you’d like to load into Redshift.

Sample Hubspot Data

Once you successfully query the Hubspot API, it will return JSON formatted data. Take a look at an example response from the calendar API:

Example GET URL:
https://api.hubapi.com/calendar/v1/events?startDate=1414281600000&endDate=1417910400000&limit=2&hapikey=demo

[
  {
    "id": "2087009576",
    "eventType": "CONTENT",
    "eventDate": 1415975962000,
    "category": "email",
    "categoryId": 2,
    "contentId": 2087009576,
    "state": "PUBLISHED",
    "campaignGuid": "",
    "portalId": 62515,
    "name": "Z 20141114-0937",
    "description": "",
    "url": "http://62515.hs-sites.com/-temporary-slug-5966d673-e0f4-4600-9417-9b173bf0d7de",
    "ownerId": null,
    "createdBy": 215482,
    "createContent": true,
    "previewKey": "jwVDH6Z9",
    "templatePath": null,
    "socialUsername": null,
    "socialDisplayName": null,
    "avatarUrl": null,
    "isRecurring": false,
    "topicIds": null,
    "contentGroupId": null,
    "groupId": null,
    "groupOrder": null,
    "remoteId": 0
  },
  {
    "id": "9213914",
    "eventType": "SOCIAL",
    "eventDate": 1414620448396,
    "category": "twitter",
    "categoryId": 0,
    "contentId": null,
    "state": "PUBLISHED",
    "campaignGuid": null,
    "portalId": 62515,
    "name": null,
    "description": "Post With Image 20141028-180746",
    "url": "https://twitter.com/Z123QA/status/527582515264258048",
    "ownerId": null,
    "createdBy": 0,
    "createContent": true,
    "previewKey": null,
    "templatePath": null,
    "socialUsername": "Z123QA",
    "socialDisplayName": "Z123 QA",
    "avatarUrl": "https://abs.twimg.com/sticky/default_profile_images/default_profile_2_reasonably_small.png",
    "isRecurring": false,
    "topicIds": null,
    "contentGroupId": null,
    "groupId": null,
    "groupOrder": null,
    "remoteId": 0
  }
]

Preparing Hubspot Data for Redshift

With the JSON in hand, you now need to map all those data fields into a schema that can be inserted into your Redshift database. This means that, for each value in the response, you need to identify a predefined data type (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them.

Check out the Stitch Hubspot Documentation to get a good sense of what fields and data types will be provided by each endpoint. Once you have identified all of the columns you will want to insert, use the CREATE TABLE statement in Redshift to build a table that will receive all of this data.

Inserting Hubspot Data into Redshift

It may seem like the easiest way to add your data is to build tried-and-true INSERT statements that add data to your Redshift table row-by-row. If you have any experience with SQL, this will be your gut reaction.  It will work, but isn’t the most efficient way to get the job done.

Redshift actually offers some good documentation for how to best bulk insert data into new tables. The COPY command is particularly useful for this task, as it allows you to insert multiple rows without needing to build individual INSERT statements for each row.

If you cannot use COPY, it might help to use PREPARE to create a prepared INSERT statement, and then use EXECUTE as many times as required. This avoids some of the overhead of repeatedly parsing and planning INSERT.

Keeping Data Up-To-Date

So, now what’s next? You’ve built a script that pulls data from Hubspot and moves it into Redshift, but what happens on Monday when you have new and updated events?

The key is to build your script in such a way that it can also identify incremental updates to your data. Some API’s include fields like created_at that allow you to quickly identify records that are new since your last update (or since the newest record you’ve copied into Redshift). You can set your script up as a cron job or continuous loop to keep pulling down new data as it appears.

Other Data Warehouse Options

Redshift is totally awesome, but sometimes you need to start smaller or optimize for different things. In this case, many people choose to get started with Postgres, which is an open source RDBMS that uses nearly identical SQL syntax to Redshift. If you’re interested in seeing the relevant steps for loading this data into Postgres, check out Hubspot to Postgres

Easier and Faster Alternatives

If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your Hubspot data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Redshift data warehouse.