-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb-with-go.html
212 lines (181 loc) · 6.87 KB
/
dynamodb-with-go.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>janzenbupa.github.io</title>
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
background-color: #f8f8f8;
}
.nav-bar-header {
background-color: #1f1f2e;
color: #e6e6e6;
padding: 20px;
text-align: center;
}
.nav-bar-header h2 {
margin: 0;
}
.nav-bar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #1f1f2e;
text-align: center;
}
.nav-bar li {
display: inline-block;
margin-right: 20px;
}
.nav-bar li a {
display: block;
color: #fff;
padding: 10px 15px;
text-decoration: none;
}
.nav-bar li a:hover {
background-color: #735581;
text-decoration: underline;
}
.body-padding {
padding: 0 20px;
}
.p-center {
text-align: center;
margin-bottom: 20px;
}
code {
background: hsl(0, 3%, 92%);
}
.code-container {
display: flex;
justify-content: center;
align-items: center;
height: 300px;
}
.code-block {
width: 500px;
height: 190px;
padding: 1px;
margin: 0px;
background-color: #f5f5f5;
border: 1px solid #ddd;
overflow: auto;
white-space: pre-wrap;
text-align: left;
}
</style>
</head>
<body>
<div class="nav-bar-header">
<h2 class="h2-title">software engineering</h2>
<p></p>
</div>
<div class="nav-bar">
<ul>
<li><a class="a-look" href="index.html">home</a></li>
<li class="li-header"><a class="a-look" href="blog.html">blog</a></li>
</ul>
</div>
</div>
<div class="p-center" style="padding-bottom: 20px;">
<H2 class="h2-title" style="color:#676798">Connecting to DynamoDb with Go</H2>
<p>After creating a Go project and initializing the module name, you can begin to import the necessary
packages required for connecting to DynamoDb.
</p>
<div style="text-align: center;">
<code class="codeblock" style="display: inline-block;text-align: left;width: 400px;margin-bottom: 20px;">
go get github.com/aws/aws-sdk-go/aws</br>
go get github.com/aws/aws-sdk-go/aws/credentials</br>
go get github.com/aws/aws-sdk-go/aws/session</br>
go get github.com/aws/aws-sdk-go/service/dynamodb</br>
</code>
</div>
<p>The next step after getting the required dependencies is to create the method that can connect to DynamoDb.</p>
<div class="code-container">
<code class="code-block">
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: credentials.NewStaticCredentials(
accessKey,
secretKey,
""),
}))
svc := dynamodb.New(sess)
return *svc
</code>
</div>
<p>This creates a connection to DynamoDb using https. This session is created and can be used to read and write to your database.</p>
<p>Now we are going to try writing to our data.</p>
<div class="code-container"style="height: 300px;margin-bottom: 20px;">
<code class="code-block" style="height: 300px;">
func Write(data SomeDataStruct) {
input := &dynamodb.PutItemInput{
TableName: aws.String("TableName"),
Item: map[string]*dynamodb.AttributeValue{
"Property/Column": {
S: aws.String(data.Property),
},
"DifferentProperty": {
S: aws.String(data.DifferentProperty),
},
},
}
_, err := svc.PutItem(input)
if err != nil {
return
}
}
</code>
</div>
<p>The code above will accept some data of type struct, for example, and map that data to properties in our database.</p>
<p>The &dynamodb.PutItemInput pointer allows us to define a variable named input of the PutItemInput struct type,
and allows us to set the Item field on that struct to our actual data that we need to save.</p>
<p>Item field would be an example of our data structure in DynamoDb.</p>
<p>Next, we may want to read the data that we have written to our database. The function below allows us to do that.</p>
<div class="code-container"style="height: 300px;margin-bottom: 20px;">
<code class="code-block" style="height: 300px;width: 550px;">
func Read(limit int64) ([]map[string]*dynamodb.AttributeValue, error) {
input := &dynamodb.ScanInput{
TableName: aws.String("TableName"),
Limit: aws.Int64(limit),
}
result, err := svc.Scan(input)
if err != nil {
return nil, err
}
items := result.Items
return items, nil
}
</code>
</div>
<p>Finally, we may just want to query a single item. To do that, all we have to do is create a pointer to a QueryInput struct, and define the attribute that we want to query on.</p>
<div class="code-container"style="height: 350px;margin-bottom: 20px;">
<code class="code-block" style="height: 320px;width: 700px;">
func ReadByAttribute(attrVal string) ([]map[string]*dynamodb.AttributeValue, error) {
input := &dynamodb.QueryInput{
TableName: aws.String("TableName"),
KeyConditionExpression: aws.String("AttributeVal = :attrVal"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":attrVal": {
S: aws.String(attrVal),
},
},
}
result, err := svc.Query(input)
if err != nil {
return nil, err
}
return result.Items, nil
}
</code>
</div>
<p>Our code accepts a parameter called attrVal, and queries our database based on a field named AttributeVal.</p>
<p>It will run a query that searches for the key AttributeVal, where the value of that key is equal to the value of attrVal that we passed in. It then returns every instance of that data.</p>
</div>
</body>
</html>