-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_aws-dynamodb_create-table.go
62 lines (57 loc) · 2.16 KB
/
example_aws-dynamodb_create-table.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
package main
import (
"fmt"
"github.com/btnguyen2k/prom"
"math/rand"
"time"
)
func awsDynamodbCreateIndexAndWait(adc *prom.AwsDynamodbConnect, table, index string) {
err := adc.CreateGlobalSecondaryIndex(nil, table, index, 1, 1,
[]prom.AwsDynamodbNameAndType{{"email", "S"}},
[]prom.AwsDynamodbNameAndType{{"email", "HASH"}})
fmt.Printf(" Create GSI [%s] on table [%s]: %e\n", index, table, err)
time.Sleep(1 * time.Second)
for status, err := adc.GetGlobalSecondaryIndexStatus(nil, table, index); status != "ACTIVE" && err == nil; {
fmt.Printf(" GSI [%s] on table [%s] status: %v - %e\n", index, table, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetGlobalSecondaryIndexStatus(nil, table, index)
}
}
func awsDynamodbCreateTableAndWait(adc *prom.AwsDynamodbConnect, table string, schema, key []prom.AwsDynamodbNameAndType) {
err := adc.CreateTable(nil, table, 2, 2, schema, key)
fmt.Printf(" Create table [%s]: %e\n", table, err)
if err == nil {
tables, err := adc.ListTables(nil)
if err != nil {
fmt.Printf(" Error: %e\n", err)
} else {
fmt.Printf(" Tables: %v\n", tables)
}
ok, err := adc.HasTable(nil, table)
if err != nil {
fmt.Printf(" Error: %e\n", err)
} else {
fmt.Printf(" HasTable[%s]: %v\n", table, ok)
}
}
time.Sleep(1 * time.Second)
for status, err := adc.GetTableStatus(nil, table); status != "ACTIVE" && err == nil; {
fmt.Printf(" Table [%s] status: %v - %e\n", table, status, err)
time.Sleep(1 * time.Second)
status, err = adc.GetTableStatus(nil, table)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
adc := createAwsDynamodbConnect("ap-southeast-1")
defer adc.Close()
fmt.Println("-== Create Table and GSI ==-")
awsDynamodbCreateTableAndWait(adc, "test1",
[]prom.AwsDynamodbNameAndType{{"username", "S"}},
[]prom.AwsDynamodbNameAndType{{"username", "HASH"}})
awsDynamodbCreateIndexAndWait(adc, "test1", awsDynamodbIndexName)
awsDynamodbCreateTableAndWait(adc, "test2",
[]prom.AwsDynamodbNameAndType{{"username", "S"}, {"email", "S"}},
[]prom.AwsDynamodbNameAndType{{"username", "HASH"}, {"email", "RANGE"}})
awsDynamodbCreateIndexAndWait(adc, "test2", awsDynamodbIndexName)
}