Skip to content

Commit d1e8ec2

Browse files
committedNov 24, 2022
Updated
1 parent 7fd42cc commit d1e8ec2

25 files changed

+1163
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/aws/aws-sdk-go/service/dynamodb"
7+
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
8+
9+
"fmt"
10+
"log"
11+
"strconv"
12+
)
13+
14+
type Item struct {
15+
Year int
16+
Title string
17+
Plot string
18+
Rating float64
19+
}
20+
21+
// snippet-end:[dynamodb.go.create_item.struct]
22+
23+
func main() {
24+
25+
sess := session.Must(session.NewSessionWithOptions(session.Options{
26+
SharedConfigState: session.SharedConfigEnable,
27+
}))
28+
29+
// Create DynamoDB client
30+
svc := dynamodb.New(sess)
31+
32+
// snippet-start:[dynamodb.go.create_item.assign_struct]
33+
item := Item{
34+
Year: 2015,
35+
Title: "The Big New Movie",
36+
Plot: "Nothing happens at all.",
37+
Rating: 0.0,
38+
}
39+
40+
av, err := dynamodbattribute.MarshalMap(item)
41+
if err != nil {
42+
log.Fatalf("Got error marshalling new movie item: %s", err)
43+
}
44+
45+
tableName := "Movies"
46+
47+
input := &dynamodb.PutItemInput{
48+
Item: av,
49+
TableName: aws.String(tableName),
50+
}
51+
52+
_, err = svc.PutItem(input)
53+
if err != nil {
54+
log.Fatalf("Got error calling PutItem: %s", err)
55+
}
56+
57+
year := strconv.Itoa(item.Year)
58+
59+
fmt.Println("Successfully added '" + item.Title + "' (" + year + ") to table " + tableName)
60+
// snippet-end:[dynamodb.go.create_item.call]
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/aws/aws-sdk-go/service/dynamodb"
7+
8+
"fmt"
9+
"log"
10+
)
11+
12+
func main() {
13+
14+
sess := session.Must(session.NewSessionWithOptions(session.Options{
15+
SharedConfigState: session.SharedConfigEnable,
16+
}))
17+
18+
// Create DynamoDB client
19+
svc := dynamodb.New(sess)
20+
21+
tableName := "Movies"
22+
23+
input := &dynamodb.CreateTableInput{
24+
AttributeDefinitions: []*dynamodb.AttributeDefinition{
25+
{
26+
AttributeName: aws.String("Year"),
27+
AttributeType: aws.String("N"),
28+
},
29+
{
30+
AttributeName: aws.String("Title"),
31+
AttributeType: aws.String("S"),
32+
},
33+
},
34+
KeySchema: []*dynamodb.KeySchemaElement{
35+
{
36+
AttributeName: aws.String("Year"),
37+
KeyType: aws.String("HASH"),
38+
},
39+
{
40+
AttributeName: aws.String("Title"),
41+
KeyType: aws.String("RANGE"),
42+
},
43+
},
44+
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
45+
ReadCapacityUnits: aws.Int64(10),
46+
WriteCapacityUnits: aws.Int64(10),
47+
},
48+
TableName: aws.String(tableName),
49+
}
50+
51+
_, err := svc.CreateTable(input)
52+
if err != nil {
53+
log.Fatalf("Got error calling CreateTable: %s", err)
54+
}
55+
56+
fmt.Println("Created the table", tableName)
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
// snippet-start:[dynamodb.go.delete_item.imports]
4+
import (
5+
"github.com/aws/aws-sdk-go/aws"
6+
"github.com/aws/aws-sdk-go/aws/session"
7+
"github.com/aws/aws-sdk-go/service/dynamodb"
8+
9+
"fmt"
10+
"log"
11+
)
12+
13+
// snippet-end:[dynamodb.go.delete_item.imports]
14+
15+
func main() {
16+
17+
// and region from the shared configuration file ~/.aws/config.
18+
sess := session.Must(session.NewSessionWithOptions(session.Options{
19+
SharedConfigState: session.SharedConfigEnable,
20+
}))
21+
22+
// Create DynamoDB client
23+
svc := dynamodb.New(sess)
24+
25+
// snippet-start:[dynamodb.go.delete_item.call]
26+
tableName := "Movies"
27+
movieName := "The Big New Movie"
28+
movieYear := "2015"
29+
30+
input := &dynamodb.DeleteItemInput{
31+
Key: map[string]*dynamodb.AttributeValue{
32+
"Year": {
33+
N: aws.String(movieYear),
34+
},
35+
"Title": {
36+
S: aws.String(movieName),
37+
},
38+
},
39+
TableName: aws.String(tableName),
40+
}
41+
42+
_, err := svc.DeleteItem(input)
43+
if err != nil {
44+
log.Fatalf("Got error calling DeleteItem: %s", err)
45+
}
46+
47+
fmt.Println("Deleted '" + movieName + "' (" + movieYear + ") from table " + tableName)
48+
// snippet-end:[dynamodb.go.delete_item.call]
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws/awserr"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/aws/aws-sdk-go/service/dynamodb"
7+
8+
"fmt"
9+
)
10+
11+
func main() {
12+
13+
sess := session.Must(session.NewSessionWithOptions(session.Options{
14+
SharedConfigState: session.SharedConfigEnable,
15+
}))
16+
17+
// Create DynamoDB client
18+
svc := dynamodb.New(sess)
19+
20+
input := &dynamodb.ListTablesInput{}
21+
22+
fmt.Printf("Tables:\n")
23+
24+
for {
25+
// Get the list of tables
26+
result, err := svc.ListTables(input)
27+
if err != nil {
28+
if aerr, ok := err.(awserr.Error); ok {
29+
switch aerr.Code() {
30+
case dynamodb.ErrCodeInternalServerError:
31+
fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
32+
default:
33+
fmt.Println(aerr.Error())
34+
}
35+
} else {
36+
// Print the error, cast err to awserr.Error to get the Code and
37+
// Message from an error.
38+
fmt.Println(err.Error())
39+
}
40+
return
41+
}
42+
43+
for _, n := range result.TableNames {
44+
fmt.Println(*n)
45+
}
46+
47+
input.ExclusiveStartTableName = result.LastEvaluatedTableName
48+
49+
if result.LastEvaluatedTableName == nil {
50+
break
51+
}
52+
}
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/aws/aws-sdk-go/service/dynamodb"
7+
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
8+
9+
"encoding/json"
10+
"fmt"
11+
"io/ioutil"
12+
"log"
13+
"strconv"
14+
)
15+
16+
// Create struct to hold info about new item
17+
type Item struct {
18+
Year int
19+
Title string
20+
Plot string
21+
Rating float64
22+
}
23+
24+
// Get table items from JSON file
25+
func getItems() []Item {
26+
raw, err := ioutil.ReadFile("./.movie_data.json")
27+
if err != nil {
28+
log.Fatalf("Got error reading file: %s", err)
29+
}
30+
31+
var items []Item
32+
json.Unmarshal(raw, &items)
33+
return items
34+
}
35+
36+
// snippet-end:[dynamodb.go.load_items.func]
37+
38+
func main() {
39+
40+
// and region from the shared configuration file ~/.aws/config.
41+
sess := session.Must(session.NewSessionWithOptions(session.Options{
42+
SharedConfigState: session.SharedConfigEnable,
43+
}))
44+
45+
// Create DynamoDB client
46+
svc := dynamodb.New(sess)
47+
48+
// Get table items from .movie_data.json
49+
items := getItems()
50+
51+
// Add each item to Movies table:
52+
tableName := "Movies"
53+
54+
for _, item := range items {
55+
av, err := dynamodbattribute.MarshalMap(item)
56+
if err != nil {
57+
log.Fatalf("Got error marshalling map: %s", err)
58+
}
59+
60+
// Create item in table Movies
61+
input := &dynamodb.PutItemInput{
62+
Item: av,
63+
TableName: aws.String(tableName),
64+
}
65+
66+
_, err = svc.PutItem(input)
67+
if err != nil {
68+
log.Fatalf("Got error calling PutItem: %s", err)
69+
}
70+
71+
year := strconv.Itoa(item.Year)
72+
73+
fmt.Println("Successfully added '" + item.Title + "' (" + year + ") to table " + tableName)
74+
// snippet-end:[dynamodb.go.load_items.call]
75+
}
76+
}
77+
78+
// snippet-end:[dynamodb.go.load_items]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
// snippet-start:[dynamodb.go.read_item.imports]
4+
import (
5+
"errors"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/dynamodb"
10+
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
11+
12+
"fmt"
13+
"log"
14+
)
15+
16+
// Create struct to hold info about new item
17+
type Item struct {
18+
Year int
19+
Title string
20+
Plot string
21+
Rating float64
22+
}
23+
24+
// snippet-end:[dynamodb.go.read_item.struct]
25+
26+
func main() {
27+
28+
// and region from the shared configuration file ~/.aws/config.
29+
sess := session.Must(session.NewSessionWithOptions(session.Options{
30+
SharedConfigState: session.SharedConfigEnable,
31+
}))
32+
33+
// Create DynamoDB client
34+
svc := dynamodb.New(sess)
35+
// snippet-end:[dynamodb.go.read_item.session]
36+
37+
// snippet-start:[dynamodb.go.read_item.call]
38+
tableName := "Movies"
39+
movieName := "The Big New Movie"
40+
movieYear := "2015"
41+
42+
result, err := svc.GetItem(&dynamodb.GetItemInput{
43+
TableName: aws.String(tableName),
44+
Key: map[string]*dynamodb.AttributeValue{
45+
"Year": {
46+
N: aws.String(movieYear),
47+
},
48+
"Title": {
49+
S: aws.String(movieName),
50+
},
51+
},
52+
})
53+
if err != nil {
54+
log.Fatalf("Got error calling GetItem: %s", err)
55+
}
56+
57+
// snippet-start:[dynamodb.go.read_item.unmarshall]
58+
if result.Item == nil {
59+
msg := "Could not find '" + *title + "'"
60+
return nil, errors.New(msg)
61+
}
62+
63+
item := Item{}
64+
65+
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
66+
if err != nil {
67+
panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
68+
}
69+
70+
fmt.Println("Found item:")
71+
fmt.Println("Year: ", item.Year)
72+
fmt.Println("Title: ", item.Title)
73+
fmt.Println("Plot: ", item.Plot)
74+
fmt.Println("Rating:", item.Rating)
75+
// snippet-end:[dynamodb.go.read_item.unmarshall]
76+
}
77+
78+
// snippet-end:[dynamodb.go.read_item]

0 commit comments

Comments
 (0)
Please sign in to comment.