Menu
Amazon DynamoDB
Getting Started Guide (API Version 2012-08-10)

Step 2: Load Sample Data

In this step, you populate the Movies table with sample data.

We use a sample data file that contains information about a few thousand movies from the Internet Movie Database (IMDb). The movie data is in JSON format, as shown in the following example. For each movie, there is a year, a title, and a JSON map named info.

[
   {
      "year" : ... ,
      "title" : ... ,
      "info" : { ... }
   },
   {
      "year" : ...,
      "title" : ...,
      "info" : { ... }
   },

    ...

]

In the JSON data, note the following:

  • We use the year and title as the primary key attribute values for our Movies table.

  • We store the rest of the info values in a single attribute called info. This program illustrates how you can store JSON in a DynamoDB attribute.

The following is an example of movie data:


{
    "year" : 2013,
    "title" : "Turn It Down, Or Else!",
    "info" : {
        "directors" : [
            "Alice Smith",
            "Bob Jones"
        ],
        "release_date" : "2013-01-18T00:00:00Z",
        "rating" : 6.2,
        "genres" : [
            "Comedy",
            "Drama"
        ],
        "image_url" : "http://ia.media-imdb.com/images/N/O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
        "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
        "rank" : 11,
        "running_time_secs" : 5215,
        "actors" : [
            "David Matthewman",
            "Ann Thomas",
            "Jonathan G. Neff"
       ]
    }
}

Step 2.1: Download the Sample Data File

  1. Download the sample data archive by clicking this link: moviedata.zip

  2. Extract the data file (moviedata.json) from the archive.

  3. Copy the moviedata.json file to your current directory.

Step 2.2: Load the Sample Data Into the Movies Table

After you have downloaded the sample data, you can run the following program to populate the Movies table.

  1. Copy the following program into your Java development environment.

    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    // Licensed under the Apache License, Version 2.0.
    
    package com.amazonaws.codesamples.gsg;
    
    import java.io.File;
    import java.util.Iterator;
    
    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.fasterxml.jackson.core.JsonFactory;
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    
    public class MoviesLoadData {
    
        public static void main(String[] args) throws Exception {
    
            AmazonDynamoDBClient client = new AmazonDynamoDBClient()
                .withEndpoint("http://localhost:8000");
    
            DynamoDB dynamoDB = new DynamoDB(client);
    
            Table table = dynamoDB.getTable("Movies");
    
            JsonParser parser = new JsonFactory()
                .createParser(new File("moviedata.json"));
    
            JsonNode rootNode = new ObjectMapper().readTree(parser);
            Iterator<JsonNode> iter = rootNode.iterator();
    
            ObjectNode currentNode;
    
            while (iter.hasNext()) {
                currentNode = (ObjectNode) iter.next();
    
                int year = currentNode.path("year").asInt();
                String title = currentNode.path("title").asText();
    
                try {
                    table.putItem(new Item()
                    .withPrimaryKey("year", year, "title", title)
                    .withJSON("info", currentNode.path("info").toString()));
                    System.out.println("PutItem succeeded: " + year + " " + title);
    
                } catch (Exception e) {
                    System.err.println("Unable to add movie: " + year + " " + title);
                    System.err.println(e.getMessage());
                    break;
                }
            }
            parser.close();
        }
    }

    This program uses the open source Jackson library to process JSON. Jackson is included in the AWS SDK for Java. You do not need to install it separately.

  2. Compile and run the program.

Next Step

Step 3: Create, Read, Update, and Delete an Item