S3ToDDBTransfer.java

mail@pastecode.io avatar
unknown
java
2 years ago
3.4 kB
6
Indexable
Never
package com.example.awsdemo;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

public class S3ToDDBTransfer {
    private static final String ACCESS_KEY = ""; //TODO
    private static final String SECRET_ACCESS_KEY = ""; //TODO

    private static final String BUCKET_NAME = ""; //TODO
    private static final String OBJECT_NAME = "imdb_moviesbatman_series_1992_2022.json"; //TODO
    private static final String TABLE_NAME = ""; //TODO

    public static void execute() {
        //Create AWS credentials
        AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY);

        //Create DDB client
        AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClient.builder()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(Regions.EU_WEST_1)
                .build();

        //Create S3 client
        AmazonS3 amazonS3 = AmazonS3Client.builder()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(Regions.EU_WEST_1)
                .build();

        GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKET_NAME, OBJECT_NAME);

        //Parse S3Object to List of IMDBEntity POJOs
        List<IMDBEntity> imdbEntityList = null;
        Gson gson = new GsonBuilder().create();
        try (S3ObjectInputStream s3is = amazonS3.getObject(getObjectRequest).getObjectContent()){
            File temporaryFile = new File("temporary_file.json");
            FileUtils.copyInputStreamToFile(s3is, temporaryFile);
            String jsonAsString = FileUtils.readFileToString(temporaryFile, UTF_8);
            imdbEntityList = gson.fromJson(jsonAsString, new TypeToken<List<IMDBEntity>>(){}.getType());
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

        DynamoDB dynamoDB = new DynamoDB(amazonDynamoDB);
        Table table = dynamoDB.getTable(TABLE_NAME);

        //Put items in DDB table
        imdbEntityList.forEach((IMDBEntity -> {
            Item item = new Item()
                    .withPrimaryKey("imdbId", IMDBEntity.getImdbId())
                    .withString("title", IMDBEntity.getTitle())
                    .withString("poster", IMDBEntity.getPoster())
                    .withString("type", IMDBEntity.getType())
                    .withString("year", IMDBEntity.getYear());
            table.putItem(item);
        }));
    }

}