-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddb_create_gsi.go
78 lines (69 loc) · 2.05 KB
/
ddb_create_gsi.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
package main
// https://aws.amazon.com/ko/getting-started/hands-on/design-a-database-for-a-mobile-app-with-dynamodb/5/
// add_inverted_index.py
// https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/dynamodb@v1.2.1#Client.UpdateTable
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func objectDump(obj interface{}) {
jsonOutput, _ := json.MarshalIndent(obj, "", "\t")
fmt.Println(string(jsonOutput))
}
func main() {
const tableName = "quick-photos"
const indexName = "InvertedIndex"
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
client := dynamodb.NewFromConfig(cfg)
updateTableInput := &dynamodb.UpdateTableInput{
TableName: aws.String(tableName),
AttributeDefinitions: []types.AttributeDefinition{
types.AttributeDefinition{
AttributeName: aws.String("PK"),
AttributeType: types.ScalarAttributeTypeS,
},
types.AttributeDefinition{
AttributeName: aws.String("SK"),
AttributeType: types.ScalarAttributeTypeS,
},
},
GlobalSecondaryIndexUpdates: []types.GlobalSecondaryIndexUpdate{
types.GlobalSecondaryIndexUpdate{
Create: &types.CreateGlobalSecondaryIndexAction{
IndexName: aws.String(indexName),
KeySchema: []types.KeySchemaElement{
types.KeySchemaElement{
AttributeName: aws.String("SK"),
KeyType: types.KeyTypeHash,
},
types.KeySchemaElement{
AttributeName: aws.String("PK"),
KeyType: types.KeyTypeRange,
},
},
Projection: &types.Projection{
ProjectionType: types.ProjectionTypeAll,
},
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(5),
WriteCapacityUnits: aws.Int64(5),
},
},
},
},
} // end of updateTableInput
output, err := client.UpdateTable(context.TODO(), updateTableInput)
if err != nil {
log.Fatal(err)
}
objectDump(output)
}