forked from btnguyen2k/prom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_aws-dynamodb_order.go
221 lines (207 loc) · 7.8 KB
/
example_aws-dynamodb_order.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// go run example_aws-dynamodb_base.go example_aws-dynamodb_order.go
package main
import (
"fmt"
"math/rand"
"time"
awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
"github.com/btnguyen2k/prom/dynamodb"
)
const (
_orderTable1 = "test_order_1"
_orderTable2 = "test_order_2"
_orderTable3 = "test_order_3"
)
func _orderCreateTables(adc *dynamodb.AwsDynamodbConnect) {
var rcu int64 = 2
var wcu int64 = 4
var table string
table = _orderTable1
if ok, err := adc.HasTable(nil, table); err != nil {
panic(err)
} else if !ok {
var attrDefs, pkDefs []dynamodb.AwsDynamodbNameAndType
attrDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsAttrTypeString},
}
pkDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsKeyTypePartition},
}
if err := adc.CreateTable(nil, table, rcu, wcu, attrDefs, pkDefs); err != nil {
panic(err)
}
for status, err := adc.GetTableStatus(nil, table); status != "ACTIVE" && err == nil; {
fmt.Printf("Table [%s] status: %v - %s\n", table, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetTableStatus(nil, table)
}
} else {
// delete all items
pkAttrs := []string{"id"}
adc.ScanItemsWithCallback(nil, table, nil, dynamodb.AwsDynamodbNoIndex, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
keyFilter := make(map[string]interface{})
for _, v := range pkAttrs {
keyFilter[v] = item[v]
}
_, err := adc.DeleteItem(nil, table, keyFilter, nil)
fmt.Printf("Delete item from table [%s] with key %s: %s\n", table, keyFilter, err)
return true, nil
})
}
table = _orderTable2
if ok, err := adc.HasTable(nil, table); err != nil {
panic(err)
} else if !ok {
var attrDefs, pkDefs []dynamodb.AwsDynamodbNameAndType
attrDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsAttrTypeString},
{Name: "username", Type: dynamodb.AwsAttrTypeString},
}
pkDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsKeyTypePartition},
{Name: "username", Type: dynamodb.AwsKeyTypeSort},
}
if err := adc.CreateTable(nil, table, rcu, wcu, attrDefs, pkDefs); err != nil {
panic(err)
}
for status, err := adc.GetTableStatus(nil, table); status != "ACTIVE" && err == nil; {
fmt.Printf("Table [%s] status: %v - %s\n", table, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetTableStatus(nil, table)
}
} else {
// delete all items
pkAttrs := []string{"id", "username"}
adc.ScanItemsWithCallback(nil, table, nil, dynamodb.AwsDynamodbNoIndex, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
keyFilter := make(map[string]interface{})
for _, v := range pkAttrs {
keyFilter[v] = item[v]
}
_, err := adc.DeleteItem(nil, table, keyFilter, nil)
fmt.Printf("Delete item from table [%s] with key %s: %s\n", table, keyFilter, err)
return true, nil
})
}
table = _orderTable3
if ok, err := adc.HasTable(nil, table); err != nil {
panic(err)
} else if !ok {
var attrDefs, pkDefs []dynamodb.AwsDynamodbNameAndType
attrDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsAttrTypeString},
}
pkDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsKeyTypePartition},
}
if err := adc.CreateTable(nil, table, rcu, wcu, attrDefs, pkDefs); err != nil {
panic(err)
}
for status, err := adc.GetTableStatus(nil, table); status != "ACTIVE" && err == nil; {
fmt.Printf("Table [%s] status: %v - %s\n", table, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetTableStatus(nil, table)
}
} else {
// delete all items
pkAttrs := []string{"id"}
adc.ScanItemsWithCallback(nil, table, nil, dynamodb.AwsDynamodbNoIndex, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
keyFilter := make(map[string]interface{})
for _, v := range pkAttrs {
keyFilter[v] = item[v]
}
_, err := adc.DeleteItem(nil, table, keyFilter, nil)
fmt.Printf("Delete item from table [%s] with key %s: %s\n", table, keyFilter, err)
return true, nil
})
}
idxName := "idx_username"
if status, _ := adc.GetGlobalSecondaryIndexStatus(nil, table, idxName); status == "" {
var attrDefs, pkDefs []dynamodb.AwsDynamodbNameAndType
attrDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsAttrTypeString},
{Name: "username", Type: dynamodb.AwsAttrTypeString},
}
pkDefs = []dynamodb.AwsDynamodbNameAndType{
{Name: "id", Type: dynamodb.AwsKeyTypePartition},
{Name: "username", Type: dynamodb.AwsKeyTypeSort},
}
if err := adc.CreateGlobalSecondaryIndex(nil, table, idxName, 1, 1, attrDefs, pkDefs); err != nil {
fmt.Println(err)
} else {
for status, err := adc.GetGlobalSecondaryIndexStatus(nil, table, idxName); status != "ACTIVE" && err == nil; {
fmt.Printf("Index [%s/%s] status: %v - %s\n", table, idxName, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetGlobalSecondaryIndexStatus(nil, table, idxName)
}
}
}
}
func _orderInitData(adc *dynamodb.AwsDynamodbConnect, tableName string) {
for i := 0; i < 100; i++ {
id := fmt.Sprintf("%03d", rand.Int31n(1000))
if tableName == _orderTable2 {
id = "000"
}
username := fmt.Sprintf("%03d", rand.Int31n(1000))
item := map[string]interface{}{"id": id, "username": username}
fmt.Printf("Inserting item %v to table %s...\n", item, tableName)
if _, err := adc.PutItem(nil, tableName, item, nil); err != nil {
fmt.Println(" Error:", err)
}
}
}
func _orderLoadItems1(adc *dynamodb.AwsDynamodbConnect) {
tableName := _orderTable1
indexName := ""
fmt.Printf("Fetching items from table [%s] with index [%s]...\n", tableName, indexName)
filter := expression.Name("username").GreaterThanEqual(expression.Value("123"))
fmt.Println(" Filter:", filter)
counter := 0
err := adc.ScanItemsWithCallback(nil, tableName, &filter, indexName, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
fmt.Printf(" %v\n", item)
counter++
return true, nil
})
fmt.Println("Finish:", counter, err)
}
func _orderLoadItems2(adc *dynamodb.AwsDynamodbConnect) {
tableName := _orderTable2
indexName := ""
fmt.Printf("Fetching items from table [%s] with index [%s]...\n", tableName, indexName)
filter := expression.Name("id").Equal(expression.Value("000")).And(expression.Name("username").GreaterThanEqual(expression.Value("000")))
fmt.Println(" Filter:", filter)
counter := 0
err := adc.QueryItemsWithCallback(nil, tableName, &filter, nil, indexName, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
fmt.Printf(" %v\n", item)
counter++
return true, nil
})
fmt.Println("Finish:", counter, err)
}
func _orderLoadItems3(adc *dynamodb.AwsDynamodbConnect) {
tableName := _orderTable3
indexName := "idx_username"
fmt.Printf("Fetching items from table [%s] with index [%s]...\n", tableName, indexName)
filter := expression.Name("id").Equal(expression.Value("038"))
fmt.Println(" Filter:", filter)
counter := 0
err := adc.QueryItemsWithCallback(nil, tableName, &filter, nil, indexName, nil, func(item dynamodb.AwsDynamodbItem, lastEvaluatedKey map[string]*awsdynamodb.AttributeValue) (b bool, e error) {
fmt.Printf(" %v\n", item)
counter++
return true, nil
})
fmt.Println("Finish:", counter, err)
}
func main() {
rand.Seed(time.Now().UnixNano())
adc := createAwsDynamodbConnect()
defer adc.Close()
_orderCreateTables(adc)
_orderInitData(adc, _orderTable1)
_orderInitData(adc, _orderTable2)
_orderInitData(adc, _orderTable3)
_orderLoadItems1(adc)
_orderLoadItems2(adc)
_orderLoadItems3(adc)
}