Transform and write data to Amazon Athena
Build a real-time data stream to continuously stream data from Cloud object store to Amazon Athena. It utilizes Upsolver's built-in functions and SQL to transform streaming data.
Last updated
Build a real-time data stream to continuously stream data from Cloud object store to Amazon Athena. It utilizes Upsolver's built-in functions and SQL to transform streaming data.
Last updated
This tutorial uses pre-populated retail data and streams continuously to an Amazon Athena table. Upsolver supports many data sources and data outputs out-of-the-box.
Click on DATA OUTPUT > NEW and SELECT Amazon Athena
Provide the Output a NAME > shopping and choose DATA SOURCES > Orders Click on NEXT.
Note: The fields are automatically parsed and shown on the left hand side of the UI. Statistics and data distribution information for each field are also displayed.
From the Upsolver Transformation Screen, we can add columns to the output, create calculated fields, manage aggregations, and perform UPSERTs. Upsolver provides both a UI for building the transformations as well as a SQL interface. This functionality makes Upsolver approachable to less technical users that may not be as familiar with big data technologies.
Please find a raw sample data coming from Amazon S3 below:
The desired output will be an Amazon Athena table, flattening the nested items into individual rows. In addition, the buyerEmail should be hashed to protect PII, and an order_grand_total will be added by summing up the netTotal and salesTax.
The first transformation will be to add a column mapping for the Items. Expand the items array and click the + symbol next to itemId and price on the left-hand side. Dismiss the warning(s) if you see them.
You will see a warning regarding the mapping of NUMERIC fields to DOUBLE. This warning can be closed, as we want to keep the item price as a DOUBLE.
Click in the OUTPUT COLUMN NAME and change the column names from data_items_itemsid > item_id and data_items_price > item_price.
Click on the PREVIEW button, and verify that the output contains a row for each ordered item.
Add 3 more columns to the output and rename them orderId > order_id, netTotal > order_net_total, and salesTax > order_sales_tax. Dismiss the warnings.
Upsolver provides over 200 built in functions out-of-the-box for easy data transformations. Additionally, users can extend the capability using Python UDFs.
Click on Add Calculated Field on the upper left corner. Find the TO_DATE function and click on SELECT. Under DATETIME type in data.orderDate NAME > root.order_date Click on PREVIEW to make sure the transformation looks ok. Click on SAVE.
To protect PII, we're going to hash buyers' email addresses using the MD5 algorithm. Click on Add Calculated Field on the upper left corner. Find MD5 function and click on SELECT. Enter INPUT > data.buyerEmail and NAME > root.buyer_email_hash Click PREVIEW to make sure everything looks ok and then click on SAVE.
Note: Upsolver provides a syntax sample for usage on the right to help you get started.
Click over to the SQL tab by clicking SQL on the upper right corner.
All of the transformations from previous steps are expressed as SQL. You will see that the existing mappings and transformations are expressed as SQL query. Any changes you make in the SQL view will also be represented in the UI view and vice-versa.
Add a simple calculated field data.netTotal + data.salesTax AS order_total:DOUBLE
directly to the SQL. Notice that the added field is automatically reflected in the UI. Click on PREVIEW to make sure everything looks correct.
Checkpoint: your SQL should look like the code below. Copy/paste the SQL to your SQL tab if something is missing.
Click on RUN on the upper right corner and choose S3 STORAGE > S3 Default Output Storage
Select CONNECTION > Create your first Amazon Athena Connection and provide a NAME > athena and leave everything else as the default value click on CREATE
Go back to the RUN PARAMETER screen by clicking on the back button of your browser twice and you should see athena populated under CONNECTION. Provide the DATABASE NAME that you want to output to. This example uses upsolver. TABLE NAME > orders Click on NEXT.
Leave everything on RUN shopping as default and click on DEPLOY.
The output is continuous stream by leaving ENDING AT as Never,
In addition to transforming data to prepare it for querying, there are other behind-the-scenes optimizations that Upsolver performs to improve performance of Athena queries. Not only does Upsolver utilize best practices such as using Parquet format and partitioning data, but it also runs compaction to improve the underlying file size. You can read more about compaction here.
Using Athena you can now query the data set created by Upsolver (it may take a few minutes before the data is available). Some sample queries that can be run are below:
Click here to login to Athena in order to run below queries