forked from awsdocs/aws-doc-sdk-examples
/
LoadTableItems.go
138 lines (118 loc) · 3.9 KB
/
LoadTableItems.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
132
133
134
135
136
137
138
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[dynamodb.go.add_items]
package main
// snippet-start:[dynamodb.go.add_items.imports]
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"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/dynamodbiface"
)
// snippet-end:[dynamodb.go.add_items.imports]
// Item holds info about new item
// snippet-start:[dynamodb.go.add_items.struct]
type Item struct {
Year int
Title string
Plot string
Rating float64
}
// snippet-end:[dynamodb.go.add_items.struct]
// GetItems creates a list of table items from a JSON file
func GetItems(filename *string) ([]Item, error) {
raw, err := ioutil.ReadFile(*filename)
if err != nil {
return nil, err
}
var items []Item
err = json.Unmarshal(raw, &items)
if err != nil {
return nil, err
}
return items, nil
}
// GetMovieName gets the name of the movie from an attribute value
func GetMovieName(av map[string]*dynamodb.AttributeValue) (string, error) {
var item Item
err := dynamodbattribute.UnmarshalMap(av, &item)
if err != nil {
return "", err
}
return item.Title, nil
}
// AddTableItem adds an item to an Amazon DynamoDB table
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// year is the year when the movie was released
// table is the name of the table
// title is the movie title
// plot is a summary of the plot of the movie
// rating is the movie rating, from 0.0 to 10.0
// Output:
// If success, nil
// Otherwise, an error from the call to PutItem
func AddTableItem(svc dynamodbiface.DynamoDBAPI, av map[string]*dynamodb.AttributeValue, table *string) error {
// snippet-start:[dynamodb.go.add_items.call]
_, err := svc.PutItem(&dynamodb.PutItemInput{
Item: av,
TableName: table,
})
// snippet-end:[dynamodb.go.add_items.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[dynamodb.go.add_items.args]
jsonFile := flag.String("j", "", "The JSON file containing the items to add to the table")
table := flag.String("d", "", "The name of the database table")
flag.Parse()
if *jsonFile == "" || *table == "" {
fmt.Println("You must supply a JSON file name and database table name")
fmt.Println("-j JSON-FILE -d TABLE")
return
}
// snippet-end:[dynamodb.go.add_items.args]
// snippet-start:[dynamodb.go.add_items.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
// snippet-end:[dynamodb.go.add_items.session]
// snippet-start:[dynamodb.go.add_items.get_items]
items, err := GetItems(jsonFile)
if err != nil {
fmt.Println("Got an error retrieving items:")
fmt.Println(err)
return
}
for _, item := range items {
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Got error marshalling map:")
fmt.Println(err)
return
}
err = AddTableItem(svc, av, table)
if err != nil {
fmt.Println("Got an error adding item to table:")
fmt.Println(err)
return
}
title, err := GetMovieName(av)
if err != nil {
fmt.Println("Got an error retrieving movie name:")
fmt.Println(err)
return
}
fmt.Println("Successfully added '" + title + " to table " + *table)
}
// snippet-end:[dynamodb.go.add_items.get_items]
}
// snippet-end:[dynamodb.go.add_items]