Importing DynamoDB Data Using Apache Hive on Amazon EMR | by Yi Ai | Towards Data Science

This article describes one of the many ways to import data into AWS DynamoDB database. The option explained here uses Amazon EMR and Hive. Using Amazon EMR and Hive you can quickly and efficiently process large amounts of data, such as importing data from Amazon S3 into a DynamoDB table.
Every day an external datasource sends a csv file with about 1000 records to S3 bucket.A lambda function that will get triggered when an csv object is placed into an S3 bucket.Lambda function will start a EMR job with steps includes:
Every day an external datasource sends a csv file with about 1000 records to S3 bucket.
A lambda function that will get triggered when an csv object is placed into an S3 bucket.
Lambda function will start a EMR job with steps includes:
Create a Hive table that references data stored in DynamoDB.
Creating a hive table that references a location in Amazon S3.
Load data form S3 table to DynamoDB table.
The following diagram shows the architecture of the process.
Basic understanding of CloudFormation.
Basic understanding of EMR.
Setup an AWS account.
Install Serverless Framework.
Before getting started, Install the Serverless Framework. Open up a terminal and type npm install -g serverless.
There is a yml file (serverless.yml) in the project directory. Let’s start to define a set of objects in template file as below:
There are 2 S3 buckets, LogBucket is for EMR logs, S3BucketCsvimport is to store csv files.
A DynamoDB table to load csv data from S3.
Adding lambda function configuration to serverless.yml. it will be triggered by S3 new objected created event, the lambda function will then start a EMR job flow to process data importing.
We also need to create IAM role for the lambda function, so our lambda function has permission to start EMR job flow.
Let’s add a lambda function to create an AWS EMR cluster and adding the step details such as the location of the hive scripts, arguments etc. We can use the boto3 lib for EMR, in order to create a cluster and submit the job dynamically from lambda function.
Ad finally...let’s add Hive script.
We will create an external Hive table that maps to the csv data file.
Establish a mapping between Hive and the Features table in DynamoDB.
Add current date as partition and load table with csv data in S3.
Using following Hive script to write data from Amazon S3 to DynamoDB.
Note that Amazon EMR operations on a DynamoDB table count as read/write operations, and are subject to the table’s provisioned throughput settings. for more details please visit EMR document.
$sls deploy --stage dev
After script deployed, copy a csv file to S3 bucket with created_date={CURRENT_DATE}prefix, eg.
$aws s3 cp csv/contacts.csv s3://myemr.csv.import.dev/uploads/created_date=2020-02-03/contacts.csv
Then we can go to AWS EMR console and check the progress of the EMR steps.
It will take several minutes to complete all the steps and cluster will terminate automatically after running above steps.
Next We can go to AWS DynamoDB console to verify that the data has been loaded into DynamoDB:
Eventually Importing process took about 6 mins to load 1000 records total 76kb into DynamoDB table with write capacity units 10 and no auto scaling enabled.
That’s about it, Thanks for reading!
I hope you have found this article useful, You can find the complete project in my GitHub repo.