How to load data from Braintree to Redshift
Extract your data from Braintree
As it is common with payment gateways, Braintree exposes an API that can be used to integrate a product with payment services. Access to this API happens through several clients or SDKs that Braintree offers:
Instead of a public REST API, Braintree provides client libraries in seven languages to ease integration with our gateway. This choice is deliberate as Braintree believes that in this way they can guarantee:
- Better security
- Better platform support. And
- Backward compatibility
The languages they targeted with their SDKs cover the majority of the frameworks and needs. For example, with the Java SDK they can also support the rest of the JVM languages like Scala and Clojure.
Braintree API Rate Limiting
For a system that handles payments, rate limiting doesn’t make sense. I guess you wouldn’t like to see some of your payments failing because it happens that you have too many customers and you are dying to pay you. For this reason, Braintree has implemented some sophisticated algorithms to ensure that if one of their users goes crazy for any reason, this will not affect the others. So they are operating outside of the conventional practices of setting up rate limits. Nevertheless, you should always make sure that you respect the service you are interacting with and the code you write is not abusing it.
Endpoints and available Resources
The Braintree API exposes several resources through the available SDKs. With these you can interact with the service and perform anything that is part of the functionalities of the Braintree platform.
- Add-ons: returns a collection of all the add-ons that are available.
- Address: through this resource you can create and manage addresses for your customers. There’s a limit of 50 addresses per customer and a customer ID is always required for the operations associated with this resource.
- Client Token: This resource is available for creating tokens that will authenticate your client to the Braintree platform.
- Credit Card: Deprecated
- Credit card verification: Returns information related to the verification of credit cards.
- Customer: your customer with all the information needed in Braintree to perform payments
- Discount: Access to all the discounts that you have created in the Braintree platform.
- Merchant Account: information about merchants on the platform.
- Payment methods: Objects that represent payments
- Plan: Information about the different plans that you have created on the Braintree platform.
- Settlement Batch Summary: The settlement batch summary displays the total sales and credits for each batch for a particular date.
- Subscription: All the subscriptions that have been created on behalf of your customers inside the Braintree platform.
- Transaction: This functionality is specific to Marketplace
All the above resources are manipulated through the SDKs that Braintree maintains. In most cases, the full range of CRUD operations is supported, unless it doesn’t make sense or security concerns. In general, you can interact with everything that is available on the platform. Through the same SDKs, we can all fetch information that we can then store locally to perform our analytics. Each one can offer back all its results that we can consume, let’s assume that we want to get a list of all the Customers we have with all their associated data. In order to do that we first need to perform a search query on the Braintree API, for example in Java:
JAVASCRIPT
CustomerSearchRequest request = new CustomerSearchRequest().id().is("the_customer_id");ResourceCollection<Customer> collection = gateway.customer().search(request);for (Customer customer : collection) {System.out.println(customer.getFirstName());}
With the above query, we will be searching for all the entries that belong to a customer with the given ID. Braintree has a very rich search mechanism that allows you to perform complex queries based on your data. For example, you might search based on dates and get only the new customers back. Each customer object that will be returned will contain the following fields:
- Addresses
- AndroidPayCards
- ApplePayCards
- Company
- CreatedAt
- CreditCards
- CustomFields
- Fax
- FirstName
- Id
- LastName
- PaymentMethods
- PaypalAccounts
- Phone
- UpdatedAt
- Website
The above fields will be the Customer table columns that we will create for storing the Customer data.
Paging is transparently managed by the SDK and the Braintree API, so you won’t have to worry about iterating on many records. When you get your results, you will get an Iterator object that will iterate over all the results in a lazy way to keep the resource consumption low.
What is important to notice is that the above data are available encapsulated into the structures that each SDK is exposing, so if you need the data in JSON format, this is something that you have to take care of by converting the objects you get as results into JSON objects.
Prepare your Braintree 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, to load your data into it, you will have to follow its data model, which is a typical relational database model. The data you extract from your 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:
- SMALLINT
- INTEGER
- BIGINT
- DECIMAL
- REAL
- DOUBLE PRECISION
- BOOLEAN
- CHAR
- VARCHAR
- DATE
- TIMESTAMP
As your data are probably coming in a representation like JSON that supports a much smaller range of data types, you have to be 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 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 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:
Load data from Braintree to Redshift
The first step to load your Braintree data to Redshift, is to put them in a source that Redshift can pull it from. As mentioned earlier, there are three main data sources supported, Amazon S3, Amazon DynamoDB and Amazon Kinesis Firehose, with Firehose being the most recent addition to insert data into Redshift.
To upload your 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 REST API endpoints for S3. You can do this by using a tool like CURL or Postman or Apirise. Or use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.
After you have created your bucket you can start sending your 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 into 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 this 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 favourite 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 into it. 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.
JAVASCRIPT
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 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:
SH
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 Braintree 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.