-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddbbatchup.go
107 lines (93 loc) · 2.63 KB
/
ddbbatchup.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
package main
// https://aws.amazon.com/ko/getting-started/hands-on/design-a-database-for-a-mobile-app-with-dynamodb/4/
import (
"bufio"
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func makeWriteRequests(itemFilePath string) ([]types.WriteRequest, error) {
file, err := os.Open(itemFilePath)
if err != nil {
return nil, err
}
defer file.Close()
//
arWriteRequests := make([]types.WriteRequest, 0, 1000)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
strLine := scanner.Text()
itemObj := make(map[string]interface{})
err := json.Unmarshal([]byte(strLine), &itemObj)
if err != nil {
return nil, err
}
itemForPut, _ := attributevalue.MarshalMap(itemObj)
writeReq := types.WriteRequest{
PutRequest: &types.PutRequest{
Item: itemForPut,
},
}
arWriteRequests = append(arWriteRequests, writeReq)
// /*for debug*/
//jsonOutput, _ := json.MarshalIndent(itemForPut, "", "\t")
//fmt.Println(string(jsonOutput["PK"]))
//break
}
if err := scanner.Err(); err != nil {
return nil, err
}
return arWriteRequests, nil
}
func BatchWrite(ddbClient *dynamodb.Client, strTableName string, arRequests []types.WriteRequest) (*dynamodb.BatchWriteItemOutput, error) {
params := &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]types.WriteRequest{
strTableName: arRequests,
},
}
output, err := ddbClient.BatchWriteItem(context.TODO(), params)
return output, err
}
func main() {
const strTableName = "quick-photos"
const strItemFileName = "items.json"
const maxItemsInBatch = 25
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
path = path + string(os.PathSeparator) + strItemFileName
fmt.Println(path)
arWriteRequests, err := makeWriteRequests(path)
lenOfRequests := len(arWriteRequests)
fmt.Printf("%d items\n", lenOfRequests)
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
client := dynamodb.NewFromConfig(cfg)
for startIndex := 0; startIndex < lenOfRequests; startIndex += maxItemsInBatch {
endIndex := startIndex + maxItemsInBatch
if endIndex > lenOfRequests {
endIndex = lenOfRequests
}
output, err := BatchWrite(client, strTableName, arWriteRequests[startIndex:endIndex])
// /*for debug*/
if output != nil {
jsonOutput, _ := json.MarshalIndent(output, "", "\t")
fmt.Println(string(jsonOutput))
}
if err != nil {
fmt.Println("Error!")
log.Fatal(err)
}
time.Sleep(220 * time.Millisecond)
}
}