Getting Started with DynamoDB in Go (Part 2 — Load sample data)

Miguel Cabrerizo
3 min readJan 22, 2018

--

In the second part of this tutorial for DynamoDB and Go where I’m rewriting the Amazon AWS Node.js tutorial for the Go programming language, I’m adding some data to our Movies table.

We’ll start downloading the sample file provided by Amazon AWS to the directory where our main.go file lives. In the following line I download the zip file using wget, unzip it and remove the file in my Linux machine:

wget -O moviedata.zip https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip; unzip moviedata.zip; rm moviedata.zip

We’ll now have a moviedata.json file where Amazon AWS is giving us some movies that can be added to our table. This is the JSON structure for one of the movies where year is the partition key and title is used as a sort key, the rest of the information is stored in the info attribute.

{
“year”: 2013,
“title”: “Rush”,
“info”: {
“directors”: [“Ron Howard”],
“release_date”: “2013–09–02T00:00:00Z”,
“rating”: 8.3,
“genres”: [
“Action”,
“Biography”,
“Drama”,
“Sport”
],
“image_url”: “http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg",
“plot”: “A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.”,
“rank”: 2,
“running_time_secs”: 7380,
“actors”: [
“Daniel Bruhl”,
“Chris Hemsworth”,
“Olivia Wilde”
]
}
}

In our main.go file I assume that you’ve created a table in your local DynamoDB as explained in the first part of the tutorial. Now we’ll use Go to read the json file and add the sample data to our Movies table. This is the flow:

  • Define a structure that will be used to parse the JSON objects inside the moviedata.json file. The JSON objects have a year which is an integer, a title which is a string and info which is an object. As the info object can have arbitrary attributes as the type for info as explained in the JSON and Go blog post.
  • Open the JSON file.
  • Use the JSON decoder to unmarshal the contents of the moviedata.json file.
  • Create a session and a DynamoDB service connection as explained in the first part of this tutorial.
  • Loop throught the movies objects parsed from the JSON file.
  • For each movie we can use the MarshalMap function from the dynamodbattribute library which helps us to create the map of AttributeValues that Amazon AWS expect as the item to be inserted in the table using the PutItem operation.
  • Create a PutItem input with the map of AttributeValues which represents the movie and its attributes like year, title and info. Also we specify the name of the table.
  • Finally put the item using the input we’ve just created. This operation will create the item in the table. In DynamoDB’s terminology is a group of attributes that is uniquely identifiable among all of the other items.

This would be the source code for the main.go file that would populate our table with the sample data.

Now before running our main.go, please run the go get command to download all the required packages.

go get

And finally go run your main.go

go run main.go

If the import operation is successful we’ll see a message with the number of records imported:

We have processed 4609 records

In the next part of this tutorial we’ll see how we can perform CRUD operations now that our table has some sample data.

--

--

Miguel Cabrerizo

Freelance DevOps practicioner who loves Docker, K8s, Typescript and Golang