Menu
AWS SDK for Go
Developer Guide

Getting Amazon DynamoDB Table Items Using Expression Builder

The following example uses the DynamoDBScan operation to get items with a rating greater than 8.0 in the year 2011 in the Movies table in the us-west-2 region.

The example uses the Expression Builder package released in version 1.11.0 of the AWS SDK for Go in September 2017.

Create the file dynamodb_scan_item.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

Copy
import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "github.com/aws/aws-sdk-go/service/dynamodb/expression" )

Create the data structures we use to contain the information about the table item.

Copy
type ItemInfo struct { Plot string`json:"plot"` Rating float64`json:"rating"` } type Item struct { Year int`json:"year"` Title string`json:"title"` Info ItemInfo`json:"info"` }

Create variables for the minimum rating and year for the table items to retrieve.

Copy
min_rating := 8.0 year := 2011

Initialize the session that the SDK uses to load credentials from the shared credentials file ~/.aws/credentials, and create a new DynamoDB service client.

Copy
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) if err != nil { fmt.Println("Got error creating session:") fmt.Println(err.Error()) os.Exit(1) } // Create DynamoDB client svc := dynamodb.New(sess)

Create the expression defining the year for which we filter the table items to retrieve, and the projection so we get the title, year, and rating for each retrieved item. Then build the expression.

Copy
filt := expression.Name("year").Equal(expression.Value(year)) // Or we could get by ratings and pull out those with the right year later // filt := expression.Name("info.rating").GreaterThan(expression.Value(min_rating)) // Get back the title, year, and rating proj := expression.NamesList(expression.Name("title"), expression.Name("year"), expression.Name("info.rating")) expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()

Create the inputs for and call Scan to retrieve the items from the table (the movies made in 2011).

Copy
params := &dynamodb.ScanInput{ ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), FilterExpression: expr.Filter(), ProjectionExpression: expr.Projection(), TableName: aws.String("Movies"), } // Make the DynamoDB Query API call result, err := svc.Scan(params)

Loop through the movies from 2011 and display the title and rating for those where the rating is greater than 8.0

Copy
for _, i := range result.Items { item := Item{} err = dynamodbattribute.UnmarshalMap(i, &item) if err != nil { fmt.Println("Got error unmarshalling:") fmt.Println(err.Error()) os.Exit(1) } // Which ones had a higher rating? if item.Info.Rating > min_rating { // Or it we had filtered by rating previously: // if item.Year == year { num_items += 1 fmt.Println("Title: ", item.Title) fmt.Println("Rating:", item.Info.Rating) fmt.Println() } }

See the complete example on GitHub.