-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddb_query.go
74 lines (65 loc) · 1.79 KB
/
ddb_query.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
package main
// https://aws.amazon.com/ko/getting-started/hands-on/design-a-database-for-a-mobile-app-with-dynamodb/4/
import (
"context"
"encoding/json"
"errors"
"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"
)
const tableName = "quick-photos"
func objectDump(obj interface{}) {
jsonOutput, _ := json.MarshalIndent(obj, "", "\t")
fmt.Println(string(jsonOutput))
}
func fetchUserAndPhotos(pClient *dynamodb.Client, userName string) (*dynamodb.QueryOutput, error) {
if pClient == nil {
return nil, errors.New("pClient can't be nil")
}
if len(userName) < 1 {
return nil, errors.New("Required: userName")
}
qInput := dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("PK=:pk AND SK BETWEEN :metadata AND :photos"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{
Value: fmt.Sprintf("USER#%s", userName),
},
":metadata": &types.AttributeValueMemberS{
Value: fmt.Sprintf("#METADATA#%s", userName),
},
":photos": &types.AttributeValueMemberS{
Value: "PHOTO$",
},
},
ScanIndexForward: aws.Bool(true),
}
output, err := pClient.Query(context.TODO(), &qInput)
return output, err
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
client := dynamodb.NewFromConfig(cfg)
qOut, err := fetchUserAndPhotos(client, "john42")
if err != nil {
log.Fatal(err)
}
var user interface{} = nil
var photos []map[string]types.AttributeValue = nil
if qOut.Count > 0 {
user = qOut.Items[0]
}
if qOut.Count > 1 {
photos = qOut.Items[1:]
}
objectDump(user)
objectDump(photos)
}