How to load data from Salesforce to Redshift
Extract your data from Salesforce
You can’t use a Data Warehouse without data, so the first and most important step is to extract any data you want from Salesforce. Salesforce has many products and it’s also a pioneer in cloud computing and the API economy. This means that it offers a plethora of APIs to access the services and the underlying data. In this post, we’ll focus only on Salesforce CRM, which again exposes a large number of APIs to the world. More specifically and as it can be found in this excellent post from their Helpdesk about which API to use, we have the following options:
- REST API
- SOAP API
- Chatter API
- Bulk API
- Metadata API
- Streaming API
- Apex API
- Apex SOAP API
- Tooling API
Pull data from the Salesforce API
From the above list, the complexity and feature richness of the Salesforce API is more than evident. The REST API and the SOAP API are exposing the same functionalities but using different protocols. Interacting with the first can be done by using tools like CURL or by using HTTP clients for your favorite language or framework. A few suggestions:
The Salesforce REST API supports OAuth 2.0 authentication, more information can be found in the Understanding Authentication article. After you successfully authenticate with the API, you have to start interacting with its resources and start fetching data from it in order to load them on a data warehouse. It’s easy to get a list of all the resource we have access to, for example using curl we can execute the following:
curl https://XXX.salesforce.com/services/data/v26.0/ -H "Authorization: Bearer token"
A typical response from the server will be a list of available resources in JSON or XML. The Salesforce API is very expressive, it also supports a language called Salesforce Object Query Language (SOQL) for executing arbitrarily complex queries. Again, the result can be either in JSON or XML serialization. We would recommend using JSON as it will make the whole data moving process easier because the most popular data warehousing solutions natively support it. With XML you might have to transform it first into JSON before loading data to the repository.
More information about SOQL can be found on the Salesforce Object Query Language specification page. Although the protocol changes the architecture of the API remains the same so again you will be able to access the same resources etc. After you have your client ready and you are able to communicate with Salesforce you need to perform the following steps:
- Decide which resources to extract
- Map these resources to a schema of the data warehouse repository that you will use
- Transform your data into it and
- Load the transformed data on the repository based on the instructions below
As you can see, accessing their API alone is not enough for ensuring the operation of a pipeline that will safely and on time deliver your data on a data warehousing solution for analysis.
Pull Data using the Salesforce Streaming API
Another interesting way of interacting with SalesForce is through the Streaming API. With it you define queries and every time something changes to the data that register to this query you get notifications. So for example, every time you get a new account created Salesforce’s API will push notification about the event to your desired service. This is an extremely powerful mechanism that can guarantee almost real-time updates on a Data Warehouse repository. In order to implement something like that though, you need to take into consideration the limitations of both ends, while ensuring that delivery semantics that your use case requires for the data management infrastructure that you will build.
For more information, you can read the documentation of the Streaming API.
Salesforce is the innovative company behind the world’s #1 CRM platform that employees can access entirely over the Internet — there’s no infrastructure to buy, set up, or manage — you just log in and get to work.
But Salesforce has become something much bigger than its CRM solution, currently, it offers products for analytics, marketing, data, and even cloud infrastructure for IoT. It’s easy to understand that there’s an abundance of data that your company is generating on Salesforce if you are using its products.
Prepare your Salesforce data for Amazon Redshift
Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high-performance analysis. So, in order to load data into it, you will have to follow its data model which is a typical relational database model. The data you extract from a data source should be mapped into tables and columns. Where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the data types that are supported by Redshift, currently, the data types that are supported are the following:
- DOUBLE PRECISION
As data comes in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the data types that are supported by Redshift. Designing a Schema for Redshift and mapping data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions that you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:
About Amazon Redshift
Amazon Redshift is one of the most popular data warehousing solutions which is part of the Amazon Web Services (AWS) ecosystem. It is a petabyte-scale, fully managed data warehouse as a service solution that runs in the cloud.
It is SQL-based and you can communicate with it as you would do with PostgreSQL, actually, you can use the same driver although it would be better to use the drivers recommended by Amazon. You can connect either through JDBC or ODBC connections.
Load data from Salesforce to Redshift
The first step to load your data from Salesforce to Redshift is to put them in a source that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported, Amazon S3, DynamoDB, and Kinesis, with Firehose being the most recent addition as a way to insert data into Redshift.
To upload data to Amazon S3 you will have to use the AWS REST API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse.
The first task that you have to perform is to create a bucket, you do that by executing an HTTP PUT on the Amazon AWS API endpoints for S3. After you have created your bucket you can start sending data to Amazon S3, using again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case you can either access the HTTP endpoints directly or use the library of your preference. DynamoDB imports data again from S3, it adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons you can avoid it. Amazon Kinesis Firehose is the latest addition as a way to insert data into Redshift and offers a real-time streaming approach to data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:
- Create a delivery stream
- Add data to the stream
whenever you add new data to the stream, Kinesis takes care of adding these data to S3 or Redshift, again going through S3, in this case, is redundant if your goal is to move your data to Redshift.
The execution of the previous two steps can be performed either through the REST API or through your favorite library just as in the previous two cases. The difference here is that for pushing your data into the stream you’ll be using a Kinesis Agent.
Amazon Redshift supports two methods for loading data. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client, using either a JDBC or ODBC connection and then you perform an INSERT command for your data.
insert into category_stage values(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');
The way you invoke the INSERT command is the same as you would do with any other SQL database, for more information you can check the INSERT examples page on the Amazon Redshift documentation. Redshift is not designed for INSERT like operations, on the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table.
When you perform COPY commands, Redshift is able to read multiple files in simultaneously and it automatically distributes the workload to the cluster nodes and performs the load in parallel. As a command COPY is quite flexible and allows for many different ways of using it, depending on your use case. Performing a COPY on Amazon S3 is as simple as the following command:
copy listingfrom 's3://mybucket/data/listing/'credentials 'aws\_access\_key\_id=;aws\_secret\_access\_key=';
For more examples on how to invoke a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.
The best way to load data from Salesforce to Amazon Redshift and possible alternatives
So far we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from, and the requirements of your use case.
Things can get even more complicated if you want to integrate data coming from different sources. A possible alternative, instead of writing, hosting, and maintaining a flexible data infrastructure, is to use a product like RudderStack that can handle this kind of problem automatically for you.
RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more.
Don't want to go through the pain of direct integration? RudderStack's Salesforce integration makes it easy to send data from Salesforce to Amazon Redshift.