Technology

Writing data into dynamodb from csv file using java ?

Please watch this video so that it will be easy to understand how to read csv file data from s3 bucket and write those data into dynamodb using java .

 

 

Explanation:

Step-1  From the beginning we need to create a bucket, login to aws.amazon.com -> s3-> create bucket.  

Step-2 We have to write a lambda  function which will be responsible to read data from csv file and insert those data into dynamodb.

Lambda function I have created i.e readfilefromcsvfile inside aws. Code I have uploaded in github which we can get it from https://github.com/BinayKumarr/readcsvfilefroms3 .

After downloading(maven project) from github we can import to eclipse.

Step-3 Now we can convert this eclipse project to jar file as we have to upload into lambda function in aws.

How to do this ?

Command to convert eclipse project to jar file

mvn assembly:assembly -DdescriptorId=jar-with-dependencies package

Where to run this command ?

Goto to your workspace location like assume D:/readcsvfilefroms3 inside this open command prompt then run the above command. After running this the jar file will be created inside target folder of this project.

It looks like this

Step-4

Now we can come to aws side, create one table in dynamodb in which we are going to insert our csv file data.

Goto aws.amazon.com->dynamodb->create table-> give table name as well as primary key name.

Step-5 Another important point is we need to create a role for our lambda function i.e readfilefromcsvfile

How to do this ?

Creating a role for lambda function Login to aws.amazon.com-> IAM -> Create Role .

While creating role we have to give permissions as AmazonS3FullAccess and  AmazonDynamoDBFullAccess so that we will be able to accesss all the functionalities of S3 and DynamoDB otherwise we will get an exception.

Step-6  In the third step we have created our eclipse maven project into jar file,

now login to aws.amazon.com -> lambda ->open lambda function which we have created i.e readfilefromcsvfile

we needto upload that jar file into aws so that the map will create between lambda function and aws.

It looks like

 

For uploading the jar file click on Upload-> then select the jar file form your local system.

In the handler we need to give the Package Name.ClassName::functionname

For my project

learnandgrow.projects.LambdaFunctionHandler::handleRequest

Step-7 Now its time to test, click on Test, we can see in the above image as well. Then the output will be saved it will display.

Step-8 Now we can with some data, goto s3-> upload (select some csv file).

Now we can go to dynamodb->table -> it will display all the data whatever we have inside csv file.