By Rudderstack Team

How to load Data from Salesforce to SQL Data Warehouse

The purpose of this post is to help you load data from Salesforce to SQL Data Warehouse for further analysis. Information will be given on how to access and extract your data from Salesforce through its API and how to load it into SQL Data Warehouse.
Alternatively, you may check out RudderStack to load your data from Salesforce to SQL Data Warehouse in minutes for you.

Extract your data from Salesforce

You can’t use a Data Warehouse without data, so the first and most important step is to extract the 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 at this excellent post from their Helpdesk about which API to use, we have the following options:

  • REST API
  • SOAP API
  • Chatter REST API
  • Bulk API
  • Metadata API
  • Streaming API
  • Apex REST API
  • Apex SOAP API
  • Tooling API

Pull Data from the Salesforce Rest 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 REST API can be done by using tools like CURL or Postman by using HTTP clients for your favorite language or framework.

A few suggestions:

The SalesforceREST API supports OAuth 2.0 authentication, more information can be found in the Understanding Authentication article. After you successfully authenticate with the REST API, you have to start interacting with its resources and fetching data to load them into your 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://na1.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, depending on what you have asked as part of your request.

{ "sobjects" : "/services/data/v26.0/sobjects", "licensing" : "/services/data/v26.0/licensing", "connect" : "/services/data/v26.0/connect", "search" : "/services/data/v26.0/search", "query" : "/services/data/v26.0/query", "tooling" : "/services/data/v26.0/tooling", "chatter" : "/services/data/v26.0/chatter", "recent" : "/services/data/v26.0/recent" }

The Salesforce REST API is very expressive, it also supports a language called Salesforce Object Query Language (SOQL) for executing arbitrarily complex queries. For example, the following curl command will return the name fields of accounts:

curl https://na1.salesforce.com/services/data/v20.0/query/?q=SELECT+name+from+Account -H "Authorization: Bearer token"

and the result will look like the following:

{ "done" : true, "totalSize" : 14, "records" : [ { "attributes" : {

Again, the result can be either in JSON or XML serialization. We would recommend using JSON to 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 it to the repository. More information about SOQL can be found on the Salesforce Object Query Language specification page.

If for any reason you would prefer to use SOAP, then you should create a SOAP client first, you can, for example, use the force.com Web Service Connector (WSC) client. Or create your own using the WSDL using the information provided by this guide. Although the protocol changes the API’s architecture remains the same, you will be able to access the same resources, etc.

After you have your client ready and you can communicate with Salesforce, you need to perform the following steps:

  • decide which resources to extract from the API
  • Map these resources to the schema of the data warehouse repository that you will use
  • transform the data into it and
  • load the transformed data on the repository based on the instructions below

As you can see, accessing the 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 the API will push a notification about the event to your desired service. This is an extremely powerful mechanism that can guarantee almost real-time updates on your Data Warehouse repository. To implement something like that though, you need to consider the limitations of both ends while ensuring the 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 Salesforce Streaming API.

Prepare your Salesforce Data for SQL Data Warehouse

SQL Data Warehouse shares many common characteristics with SQL Server. It’s also a relational database that requires a Schema but it doesn’t support the following SQL Server features:

  • Primary keys
  • Foreign keys
  • Check constraints
  • Unique constraints
  • Unique indexes
  • Computed columns
  • Sparse columns
  • User defined types
  • Indexed views
  • Identities
  • Sequences
  • Triggers
  • Synonyms

and it supports the following data types:

  • bigint
  • binary
  • bit
  • char
  • date
  • datetime
  • datetime2
  • datetimeoffset
  • decimal
  • float
  • int
  • money
  • nchar
  • nvarchar
  • real
  • smalldatetime
  • smallint
  • smallmoney
  • time
  • tinyint
  • varbinary
  • Varchar

The data is probably coming in a representation similar to JSON - that supports a much smaller range of data types- you have to be careful about what data you feed into SQL Data Warehouse and make sure that you have mapped your types into one of the datatypes that are supported by it.

Designing a Schema for SQL Data Warehouse and mapping the 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 you can answer. It’s always a good idea to have in your mind the documentation that Microsoft Azure has published regarding the design of an SQL Data Warehouse database.

Load your Data from Salesforce to SQL Data Warehouse

Prior to seeing how to load our data from Salesforce to SQL Data warehouse, we need to see our options. SQL Data Warehouse support numerous options for loading data, such as:

  • PolyBase
  • Azure Data Factory
  • BCP command-line utility
  • SQL Server integration services

As we are interested in loading data from online services by using their exposed HTTP APIs, we will not consider the usage of BCP command-line utility or SQL server integration in this guide. We’ll consider the case of loading our data as Azure storage Blobs and then use PolyBase to load the data into SQL Data Warehouse.

Accessing these services happens through HTTP APIs. APIs play an important role in both the extraction and the loading of data into our data warehouse. You can access these APIs by using a tool like CURL, Postman.

Or use the libraries provided by Microsoft for your favorite language. Before you actually upload any data you have to create a container which is something similar to a concept to the Amazon AWS Bucket, creating a container is a straightforward operation and you can do it by following the instructions found on the Blog storage documentation from Microsoft.

As an example, the following code can create a container in Node.js.

blobSvc.createContainerIfNotExists('mycontainer', function(error, result, response){ if(!error){ // Container exists and allows // anonymous read access to blob // content and metadata within this container } });

After the creation of the container you can start uploading data to it by using again the given SDK of your choice in a similar fashion:

blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob', 'test.txt', function(error, result, response){ if(!error){ // file uploaded } });

When you are done putting your data into Azure Blobs you are ready to load it into SQL Data Warehouse using PolyBase. To do that you should follow the directions in the Load with PolyBase documentation.

In summary, the required steps to do it are the following:

  • create a database master key
  • create a database scoped credentials
  • create an external file format
  • create an external data source

PolyBase’s ability to transparently parallelize loads from Azure Blob Storage will make it the fastest tool for loading data. After configuring PolyBase, you can load data directly into your SQL Data Warehouse by simply creating an external table that points to your data in storage and then mapping that data to a new table within SQL Data Warehouse.

Of course, you will need to establish a recurrent process that will extract any newly created data from your service, load them in the form of Azure Blobs and initiate the PolyBase process for importing the data again into SQL Data Warehouse.

One way of doing this is by using the Azure Data Factory service. In case you would like to follow this path you can read some good documentation on how to move data to and from Azure SQL Warehouse using Azure Data Factory.

The best way to load data from Salesforce to SQL Data Warehouse and possible alternatives

So far, we just scraped the surface of what can be done and how to load data from Salesforce to SQL data warehouse. 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.

Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use an ETL as a service 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.

About Microsoft Azure SQL Data Warehouse

SQL Data Warehouse is the data warehousing solution you can use if you are a user of Microsoft Azure. It’s an elastic data warehousing as a service solution, emphasizing it’s enterprise focus. It also speaks SQL like the previous two solutions and it supports querying both relational and non-relational data. It offers a number of enterprise-class features like support for hybrid cloud installations and strong security. It’s probably the less mature solution than the two others though, it’s still in “Preview” mode although accessible to existing Azure subscribers.

About Salesforce

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. Salesforce offers products for analytics, marketing, data, and even cloud infrastructure for IoT. It’s easy to understand that your company’s abundance of data is generating on Salesforce if you are using its products.

Quickly and safely move all your data from Salesforce into SQL Data Warehouse and start generating insights from your data.

RudderStack is the easiest way to automate powerful data integrations.

Try RudderStack Free Today