How to load data from Magento to Redshift

Extract your data from Magento

Magento exposes its platform through both a REST and a SOAP interface. Both can be used to pull data from it, which is also the scope of this article and interact with the platform. By using these interfaces, developers create rich applications and plugins for Magento. In this post, we will use the REST version of the Magento platform.

As a Web API following the RESTful architecture principles, it can be accessed through HTTP. As a RESTful API, interacting with it can be achieved by using tools like CURL or Postman or by using HTTP clients for your favorite language or framework. A few suggestions:

Magento does not publish official SDKs, but by using the SOAP interface, it is possible to automatically generate clients that can act as SDKs for your favorite language or platform. For example, in Java, you can create a client in Eclipse by providing the WSDL file that Magento exposes after you set up the platform.

Magento API Authentication

Magento is a self-hosted platform, unless you are using the Enterprise cloud edition, so you have much more control over its access than other solutions. Still, if you want to access its data through the REST API that it has, then you will have to use OAuth for authentication which Magento supports.

Magento rate limiting

As a platform hosted on your premises, it doesn’t impose any rate-limiting. In any case, as you would like to avoid stressing your e-commerce platform that is facing your customers, you should make sure that your pipeline process does not overstress your Magento installation. But this is completely at your discretion.

Endpoints and available resources

Magento exposes the following resources:

  • Products: Retrieve the list of products, create, update, delete a product.
  • Product categories: Retrieve the list of categories assigned to a product, assign and unassign the category from a product.
  • Product images: Retrieve the list of websites assigned to a product, assign, unassign a website to/from a product.
  • Customers: Retrieve the list of customers, create, delete a customer, and update the customer information.
  • Customer Addresses: Retrieve the list of customer addresses, create, update, and delete the customer address.
  • Inventory: Retrieve the list of stock items, update required stock items.
  • Sales Orders: Retrieve the list of sales orders with detailed information on order addresses, items, and comments.
  • Order Addresses: Retrieve information on the specified order comments.
  • Order Item: Retrieve information on specified order items.

The API is possible to return either JSON or XML responses. This is something that you can control by providing the appropriate Accept header content type.

For all the above resources we can request from the Magento platform to pull out a list of results with all the associated data. Ideally, we would like to pull all the data and make sure that we keep them up to date on our analytics platform of choice for further analysis. For this post, we will just see how we can pull data for one resource, the Sales Orders, the process is the same for all other resources.

To pull data for the Sales Orders, we need to execute a get request to the following endpoint:

http://magentohost/api/rest/orders

As a platform hosted by you, you need to replace the “magentohost” part of the URL with the actual URL of the host that has Magento running. The rest of the URL is the same as the above. The default response is in XML and looks like the following:

MARKDOWN
<?xml version="1.0"?>
<magento_api>
<data_item_1>
<customer_id>3</customer_id>
<base_discount_amount>0.0000</base_discount_amount>
<base_shipping_amount>455.0000</base_shipping_amount>
<base_shipping_tax_amount>0.0000</base_shipping_tax_amount>
<base_subtotal>13650.0000</base_subtotal>
<base_tax_amount>0.0000</base_tax_amount>
<base_total_paid></base_total_paid>
<base_total_refunded></base_total_refunded>
<tax_amount>0.0000</tax_amount>
<total_paid></total_paid>
<total_refunded></total_refunded>
<base_shipping_discount_amount>0.0000</base_shipping_discount_amount>
<base_subtotal_incl_tax>13650.0000</base_subtotal_incl_tax>
<base_total_due>14105.0000</base_total_due>
<total_due>14105.0000</total_due>
<base_currency_code>USD</base_currency_code>
<tax_name></tax_name>
<tax_rate></tax_rate>
<addresses>
<data_item>
<region>Palau</region>
<postcode>19103</postcode>
<lastname>Doe</lastname>
<street>2356 Jody Road Philadelphia, PA 19103</street>
<city>PA</city>
<telephone>610-634-1181</telephone>
<country_id>US</country_id>
<firstname>John</firstname>
<address_type>billing</address_type>
<prefix></prefix>
<middlename></middlename>
<suffix></suffix>
<company></company>
</data_item>
<data_item>
<region>Palau</region>
<postcode>19103</postcode>
<lastname>Doe</lastname>
<street>2356 Jody Road Philadelphia, PA 19103</street>
<city>PA</city>
<telephone>610-634-1181</telephone>
<country_id>US</country_id>
<firstname>John</firstname>
<address_type>shipping</address_type>
<prefix></prefix>
<middlename></middlename>
<suffix></suffix>
<company></company>
</data_item>
</addresses>
<order_items>
<data_item>
<sku>Sunglasses_1</sku>
<price>150.0000</price>
<base_price>150.0000</base_price>
<base_original_price>150.0000</base_original_price>
<tax_percent>0.0000</tax_percent>
<tax_amount>0.0000</tax_amount>
<base_tax_amount>0.0000</base_tax_amount>
<base_discount_amount>0.0000</base_discount_amount>
<base_row_total>13650.0000</base_row_total>
<base_price_incl_tax>150.0000</base_price_incl_tax>
<base_row_total_incl_tax>13650.0000</base_row_total_incl_tax>
</data_item>
</order_items>
</data_item_1>
<data_item_2>
<customer_id>3</customer_id>
<base_discount_amount>0.0000</base_discount_amount>
<base_shipping_amount>95.0000</base_shipping_amount>
<base_shipping_tax_amount>0.0000</base_shipping_tax_amount>
<base_subtotal>3350.0000</base_subtotal>
<base_tax_amount>0.0000</base_tax_amount>
<base_total_paid>2445.0000</base_total_paid>
<base_total_refunded>1845.0000</base_total_refunded>
<tax_amount>0.0000</tax_amount>
<total_paid>2445.0000</total_paid>
<total_refunded>1845.0000</total_refunded>
<base_shipping_discount_amount>0.0000</base_shipping_discount_amount>
<base_subtotal_incl_tax>3350.0000</base_subtotal_incl_tax>
<base_total_due>1000.0000</base_total_due>
<total_due>1000.0000</total_due>
<base_currency_code>USD</base_currency_code>
<tax_name></tax_name>
<tax_rate></tax_rate>
<addresses>
<data_item>
<region>Palau</region>
<postcode>19103</postcode>
<lastname>Doe</lastname>
<street>2356 Jody Road Philadelphia, PA 19103</street>
<city>PA</city>
<telephone>610-634-1181</telephone>
<country_id>US</country_id>
<firstname>John</firstname>
<address_type>billing</address_type>
<prefix></prefix>
<middlename></middlename>
<suffix></suffix>
<company></company>
</data_item>
<data_item>
<region>Palau</region>
<postcode>19103</postcode>
<lastname>Doe</lastname>
<street>2356 Jody Road Philadelphia, PA 19103</street>
<city>PA</city>
<telephone>610-634-1181</telephone>
<country_id>US</country_id>
<firstname>John</firstname>
<address_type>shipping</address_type>
<prefix></prefix>
<middlename></middlename>
<suffix></suffix>
<company></company>
</data_item>
</addresses>
<order_items>
<data_item>
<sku>Sunglasses_1</sku>
<price>150.0000</price>
<base_price>150.0000</base_price>
<base_original_price>150.0000</base_original_price>
<tax_percent>0.0000</tax_percent>
<tax_amount>0.0000</tax_amount>
<base_tax_amount>0.0000</base_tax_amount>
<base_discount_amount>0.0000</base_discount_amount>
<base_row_total>1350.0000</base_row_total>
<base_price_incl_tax>150.0000</base_price_incl_tax>
<base_row_total_incl_tax>1350.0000</base_row_total_incl_tax>
</data_item>
<data_item>
<sku>Sun_glasses</sku>
<price>200.0000</price>
<base_price>200.0000</base_price>
<base_original_price>200.0000</base_original_price>
<tax_percent>0.0000</tax_percent>
<tax_amount>0.0000</tax_amount>
<base_tax_amount>0.0000</base_tax_amount>
<base_discount_amount>0.0000</base_discount_amount>
<base_row_total>2000.0000</base_row_total>
<base_price_incl_tax>200.0000</base_price_incl_tax>
<base_row_total_incl_tax>2000.0000</base_row_total_incl_tax>
</data_item>
</order_items>
</data_item_2>
</magento_api>

As we can see, we get back a list of items with each one representing an order, that contains all the information that we would like to use for further analysis. information like the discount that we might have applied, the taxes paid, the base price of the order etc. As we might have many order objects to retrieve, we should paginate through the results. To do that, we need to provide the “page” and “limit” parameters to our GET request.

Now that we have the results from our Magento shop, we can further process it before loading it into the BI platform of our choice.

Prepare your Magento 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 datatypes 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 datatypes 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 Magento to Redshift

The first step to load your data from Magento to Redshift is to put them in a source that Redshift can pull from. As it was mentioned earlier, there are three main data sources supported, Amazon S3, Amazon DynamoDB, and Amazon Kinesis Firehose, with Firehose being the most recent addition as a way 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 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:

  1. create a delivery stream
  2. 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 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.

SH
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:

SH
copy listing
from '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 Magento 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.

Sign Up For Free And Start Sending Data
Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.
Don't want to go through the pain of direct integration? RudderStack's Magento integration makes it easy to send data from Magento to Amazon Redshift.