forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanItems.go
131 lines (109 loc) · 4.45 KB
/
ScanItems.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[dynamodb.go.scan_table_items]
package main
// snippet-start:[dynamodb.go.scan_table_items.imports]
import (
"flag"
"fmt"
"strconv"
"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"
)
// snippet-end:[dynamodb.go.scan_table_items.imports]
// Item holds info about the new item
// snippet-start:[dynamodb.go.scan_table_items.struct]
type Item struct {
Year int
Title string
Plot string
Rating float64
}
// snippet-end:[dynamodb.go.scan_table_items.struct]
// ScanTableItems retrieves the table items released in a specific year
// Inputs:
// svc is a LambdaDB service client
// table is the name of the table
// year is when the movie was released
// Output:
// If success, the results of the scan and nil
// Otherwise, nil and an error from the call to Scan
func ScanTableItems(sess *session.Session, year *int, table *string, minRating *float64) (*[]Item, error) {
// snippet-start:[dynamodb.go.scan_table_items.expr]
// Create the expression to fill the input struct.
// Get all movies in that year; we'll pull out those with a higher rating later
filt := expression.Name("Year").Equal(expression.Value(year))
// Or we could get the movies 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("Rating"))
expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()
// snippet-end:[dynamodb.go.scan_table_items.expr]
if err != nil {
fmt.Println("Got error building expression:")
fmt.Println(err)
return nil, err
}
// snippet-start:[dynamodb.go.scan_table_items.call]
svc := dynamodb.New(sess)
params := &dynamodb.ScanInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
TableName: table,
}
result, err := svc.Scan(params)
// snippet-end:[dynamodb.go.scan_table_items.call]
if err != nil {
return nil, err
}
var items []Item
for _, i := range result.Items {
item := Item{}
err = dynamodbattribute.UnmarshalMap(i, &item)
if err != nil {
fmt.Println("Got error unmarshalling:")
fmt.Println(err)
return nil, err
}
// Which ones had a higher rating than the minimum value?
if item.Rating > *minRating {
// Or it we had filtered by rating previously:
// if item.Year == year {
items = append(items, item)
}
}
return &items, nil
}
// Get the movies with a minimum rating of 8.0 in 2011
func main() {
// snippet-start:[dynamodb.go.scan_table_items.args]
tableName := flag.String("t", "", "The name of the table")
minRating := flag.Float64("r", -1.0, "The minimum rating of the movies to retrieve")
year := flag.Int("y", -1, "The year the movies to retrieve were released")
flag.Parse()
if *tableName == "" || *minRating < 0.0 || *year < 0 {
fmt.Println("You must supply a table name, minimum rating of 0.0, and year > 0 but < 2020")
fmt.Println("(-t TABLE -r RATING -y YEAR)")
return
}
// snippet-end:[dynamodb.go.scan_table_items.args]
// snippet-start:[dynamodb.go.scan_table_items.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[dynamodb.go.scan_table_items.session]
result, err := ScanTableItems(sess, year, tableName, minRating)
if err != nil {
fmt.Println("Got an error scanning table:")
fmt.Println(err)
return
}
// snippet-start:[dynamodb.go.scan_table_items.process]
fmt.Println("Found", strconv.Itoa(len(*result)), "movie(s) with a rating above", *minRating, "in", *year)
// snippet-end:[dynamodb.go.scan_table_items.process]
}
// snippet-end:[dynamodb.go.scan_table_items]