-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathdynamodb_examples.clj
141 lines (117 loc) · 6.28 KB
/
dynamodb_examples.clj
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
;; Copyright (c) Cognitect, Inc.
;; All rights reserved.
;; This example is taken from the AWS DynamoDB developer guide, with
;; additions to explore the capabilities of cognitect.aws.
;; See: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.html
(ns dynamodb-examples
(:require [clojure.core.async :as a]
[clojure.java.io :as io]
[clojure.data.json :as json]
[cognitect.aws.client.api :as aws]))
(comment
;; 0 Create a client to talk to DynamoDB
(def ddb (aws/client {:api :dynamodb}))
;; ask what it can do
(aws/ops ddb)
;; doc!
(aws/doc ddb :ListTables)
(aws/doc ddb :CreateTable)
(aws/doc ddb :Scan) ;; this one has a Given section with shape/AttributeValue
;; 1. Create Example Tables
(aws/invoke ddb {:op :ListTables})
(aws/invoke ddb
{:op :CreateTable
:request {:TableName "Forum"
:AttributeDefinitions [{:AttributeName "Name"
:AttributeType "S"}]
:KeySchema [{:AttributeName "Name"
:KeyType "HASH"}]
:ProvisionedThroughput {:ReadCapacityUnits 1
:WriteCapacityUnits 1}}})
(aws/invoke ddb {:op :ListTables})
(aws/invoke ddb {:op :DescribeTable
:request {:TableName "Forum"}})
(aws/invoke ddb
{:op :CreateTable
:request {:TableName "Thread"
:AttributeDefinitions [{:AttributeName "ForumName"
:AttributeType "S"}
{:AttributeName "Subject"
:AttributeType "S"}]
:KeySchema [{:AttributeName "ForumName"
:KeyType "HASH"}
{:AttributeName "Subject"
:KeyType "RANGE"}]
:ProvisionedThroughput {:ReadCapacityUnits 1
:WriteCapacityUnits 1}}})
(aws/invoke ddb
{:op :CreateTable
:request {:TableName "Reply"
:AttributeDefinitions [{:AttributeName "Id"
:AttributeType "S"}
{:AttributeName "ReplyDateTime"
:AttributeType "S"}
{:AttributeName "PostedBy"
:AttributeType "S"}
{:AttributeName "Message"
:AttributeType "S"}]
:KeySchema [{:AttributeName "Id"
:KeyType "HASH"}
{:AttributeName "ReplyDateTime"
:KeyType "RANGE"}]
:GlobalSecondaryIndexes [{:IndexName "PostedBy-Message-Index"
:KeySchema [{:AttributeName "PostedBy"
:KeyType "HASH"}
{:AttributeName "Message"
:KeyType "RANGE"}]
:Projection {:ProjectionType "ALL"}
:ProvisionedThroughput {:ReadCapacityUnits 1
:WriteCapacityUnits 1}}]
:ProvisionedThroughput {:ReadCapacityUnits 1
:WriteCapacityUnits 1}}})
(->> ["Forum" "Reply" "Thread"]
(map #(aws/invoke ddb {:op :DescribeTable
:request {:TableName %}
:ch (a/promise-chan (comp
(map :Table)
(map :TableStatus)))}))
(into #{}))
;; when ^^ returns #{"ACTIVE"}, the tables are all ready
;; 2. Load the Data
(aws/doc ddb :BatchWriteItem)
(let [ ;; The aws-supplied example data are all json. We can use them
;; as/is, but we need keys defined the input specs to be
;; keywords if we want to validate the structure first!
xform-specified-keys
(fn [k]
(get (reduce (fn [a v] (assoc a v (keyword v)))
{}
["B" "BOOL" "BS" "Item" "L" "M" "N" "NS" "NULL" "PutRequest" "S" "SS"])
k
k))]
(->> ["Forum.json"
"Reply.json"
"Thread.json"]
(map #(-> (io/file "examples" "resources" "dynamodb" %)
slurp
(json/read-str :key-fn xform-specified-keys)))
(map #(aws/invoke ddb {:op :BatchWriteItem
:request {:RequestItems %}}))))
;; 3. Query the Data
(aws/invoke ddb
{:op :Query
:request {:TableName "Reply"
:KeyConditionExpression "Id = :id"
:ExpressionAttributeValues {":id" {:S "Amazon DynamoDB#DynamoDB Thread 1"}}}})
(aws/invoke ddb
{:op :Query
:request {:TableName "Reply"
:IndexName "PostedBy-Message-Index"
:KeyConditionExpression "PostedBy = :user"
:ExpressionAttributeValues {":user" {:S "User A"}}}})
;; 4. Delete the tables
(->> ["Forum" "Reply" "Thread"]
(map #(aws/invoke ddb {:op :DeleteTable
:request {:TableName %}}))
(into []))
)