/
main.go
164 lines (148 loc) · 3.63 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"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"
"log"
)
var (
table = flag.String("table", "your-table-name", "your stream name")
region = flag.String("region", "ap-northeast-1", "your AWS region")
)
// Item is sample table for table
type Item struct {
ID string `json:"id"`
Field1 string `json:"field1",omitempty`
IntField int `json:"intfield",omitempty`
}
func main() {
if err := realmain(); err != nil {
log.Fatal(err)
}
}
func realmain() error {
flag.Parse()
s := session.New(&aws.Config{Region: aws.String(*region)})
ddb := dynamodb.New(s)
tablename := aws.String(*table)
describeTableOutput, err := ddb.DescribeTable(&dynamodb.DescribeTableInput{
TableName: tablename,
})
if err != nil {
return err
}
fmt.Printf("%#v\n", describeTableOutput)
// if table not exists, create one.
if describeTableOutput == nil {
// create table should specify primary key.
// on DynamoDB, not primary key fields are not necessary when creating table.
createTableOutput, err := ddb.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("id"),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(10),
WriteCapacityUnits: aws.Int64(10),
},
TableName: tablename,
})
if err != nil {
return err
}
fmt.Printf("%#v\n", createTableOutput)
}
item, err := dynamodbattribute.ConvertToMap(Item{"id1", "field1", 1})
if err != nil {
return err
}
fmt.Printf("%#v\n", item)
// PutItem request overwrite existed item (same primary key) by default.
putItemOutput, err := ddb.PutItem(&dynamodb.PutItemInput{
Item: item,
TableName: tablename,
})
if err != nil {
return err
}
// empty response even if successfly put
fmt.Printf("%#v\n", putItemOutput)
getItemOutput, err := ddb.GetItem(&dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String("id1"),
},
},
TableName: tablename,
})
if err != nil {
return err
}
fmt.Printf("%#v\n", getItemOutput)
var putreq []*dynamodb.WriteRequest
for i := 0; i < 10; i++ {
it, err := dynamodbattribute.ConvertToMap(Item{
ID: fmt.Sprintf("batchwrite_id%d", i+100),
Field1: fmt.Sprintf("batchwriteItem%d", i),
IntField: i,
})
if err != nil {
return err
}
putreq = append(putreq, &dynamodb.WriteRequest{
PutRequest: &dynamodb.PutRequest{
Item: it,
},
})
}
batchWriteItemOutput, err := ddb.BatchWriteItem(&dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
*table: putreq,
},
})
if err != nil {
return err
}
fmt.Printf("%#v\n", batchWriteItemOutput)
batchGetItemOutput, err := ddb.BatchGetItem(&dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
*table: {
Keys: []map[string]*dynamodb.AttributeValue{
{
"id": {
S: aws.String("batchwrite_id100"),
},
},
{
"id": {
S: aws.String("batchwrite_id101"),
},
},
},
},
},
})
if err != nil {
return err
}
fmt.Printf("%#v\n", batchGetItemOutput)
deleteTableOutput, err := ddb.DeleteTable(&dynamodb.DeleteTableInput{
TableName: tablename,
})
if err != nil {
return err
}
fmt.Printf("%#v", deleteTableOutput)
return nil
}