AWS DynamoDB 정리 1부 테이블 만들기, 테이블 정보보기, 데이터 읽기

참조

배경

드디어 나와 사부가 만들던 어플리케이션에서 서버 부분까지 내가 담당해 보기로 했다.

사부는 nodejs와 aws dynamodb로 서버를 개발하고 있었는데 나도 처음으로 그 부분에 대해서 공부하게 되었다.

그런데 이제까지는 mysql정도 밖에 다루어 보지 않았어서 aws dynamodb는 정말 생소하였다.

그래서 이 포스트를 남겨 앞으로 dynamodb를 쓸 때 참고를 하고자 한다.

핵심

  • primaryKey에는 partitionKey와 sortKey로 나누어져있다.(paritionKey만 있는 경우도 있음.)
  • partitionKey는 겹치더라도, sortKey는 겹치지 않는다.

  • C : put, batchWrite

  • R : query
  • U : update
  • D : delete

기본 내용

Create a Table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// setting dynamo db table
var params = {
TableName : "Music",
KeySchema: [
{ AttributeName: "Artist", KeyType: "HASH" }, //Partition key
{ AttributeName: "SongTitle", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "Artist", AttributeType: "S" },
{ AttributeName: "SongTitle", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
  • The params object holds the parameters for the corresponding DynamoDB API operation.
  • The dynamodb. line invokes the operation, with the correct parameters. In the example above, the operation is createTable.

Get Information About Tables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Retrieve a Table Description
var params = {
TableName: "Music"
};
dynamodb.describeTable(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
// Retrieve a List of Tables
var params = {};
dynamodb.listTables(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});

Write Items to the Table

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
// Write a Item
var params = {
TableName: "Music",
Item: {
"Artist":"No One You Know",
"SongTitle":"Call Me Today",
"AlbumTitle":"Somewhat Famous",
"Year": 2015,
"Price": 2.14,
"Genre": "Country",
"Tags": {
"Composers": [
"Smith",
"Jones",
"Davis"
],
"LengthInSeconds": 214
}
}
docClient.put(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
// Writing succeed only when it satisfy the ConditionExpression's content. if not, it simply overwrite.
// "ConditionExpression": "attribute_not_exists(Artist) and attribute_not_exists(SongTitle)"
};
  • Artist and SongTitle are primary key attributes (partition key and sort key, respectively). Both are of string type. Every item that you add to the table must have values for these attributes.
  • Other attributes are AlbumTitle (string), Year (number), Price (number), Genre (string), and Tags (map).
  • DynamoDB allows you to nest attributes within other attributes. The Tags map contains two nested attributes—Composers (list) and LengthInSeconds (number).
  • Artist, SongTitle, AlbumTitle, Year, Price, Genre, and Tags are top-level attributes because they are not nested within any other attributes.
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
// Write Multiple Items
var params = {
RequestItems: {
"Music": [
{
PutRequest: {
Item: {
"Artist": "No One You Know",
"SongTitle": "My Dog Spot",
"AlbumTitle":"Hey Now",
"Price": 1.98,
"Genre": "Country",
"CriticRating": 8.4
}
}
},
{
PutRequest: {
Item: {
"Artist": "No One You Know",
"SongTitle": "Somewhere Down The Road",
"AlbumTitle":"Somewhat Famous",
"Genre": "Country",
"CriticRating": 8.4,
"Year": 1984
}
}
},
{
PutRequest: {
Item: {
"Artist": "The Acme Band",
"SongTitle": "Still In Love",
"AlbumTitle":"The Buck Starts Here",
"Price": 2.47,
"Genre": "Rock",
"PromotionInfo": {
"RadioStationsPlaying":[
"KHCR", "KBQX", "WTNR", "WJJH"
],
"TourDates": {
"Seattle": "20150625",
"Cleveland": "20150630"
},
"Rotation": "Heavy"
}
}
}
},
{
PutRequest: {
Item: {
"Artist": "The Acme Band",
"SongTitle": "Look Out, World",
"AlbumTitle":"The Buck Starts Here",
"Price": 0.99,
"Genre": "Rock"
}
}
}
]
}
};
docClient.batchWrite(params, function (err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});

Read an Item Using Its Primary Key

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
// Read an Item Using GetItem
var params = {
TableName: "Music",
Key: {
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
}
};
docClient.get(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
//// Result : It is very different from get multiple datas using batchGet api.
{
"Item": {
"Artist": "No One You Know",
"Year": 2015,
"Price": 2.14,
"SongTitle": "Call Me Today",
"AlbumTitle": "Somewhat Famous",
"Genre": "Country",
"Tags": {
"Composers": [
"Smith",
"Jones",
"Davis"
],
"LengthInSeconds": 214
}
}
}
// only Read an item's subset of attributes
var params = {
TableName: "Music",
Key: {
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
},
ProjectionExpression: "AlbumTitle"
};
// when it comes to Reserved word (Year)
var params = {
TableName: "Music",
Key: {
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
},
ProjectionExpression: "AlbumTitle, #y", // #y is placeholder token
ExpressionAttributeNames: {"#y": "Year"}
};
// Read maps / list data
var params = {
TableName: "Music",
Key: {
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
},
ProjectionExpression: "AlbumTitle, #y, Tags.Composers[0], Tags.LengthInSeconds",
ExpressionAttributeNames: {"#y": "Year"}
};
// Read multiple items
var params = {
RequestItems: {
"Music": {
Keys: [
{
"Artist": "No One You Know",
"SongTitle": "My Dog Spot"
},
{
"Artist": "No One You Know",
"SongTitle": "Somewhere Down The Road"
},
{
"Artist": "The Acme Band",
"SongTitle": "Still In Love"
},
{
"Artist": "The Acme Band",
"SongTitle": "Look Out, World"
}
],
}
}
};
docClient.batchGet(params, function (err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
// Result
{
"Responses": {
"Music": [
{
"Artist": "The Acme Band",
"PromotionInfo": {
"TourDates": {
"Seattle": "20150625",
"Cleveland": "20150630"
},
"Rotation": "Heavy",
"RadioStationsPlaying": [
"KHCR",
"KBQX",
"WTNR",
"WJJH"
]
},
"AlbumTitle": "The Buck Starts Here",
"Genre": "Rock",
"Price": 2.47,
"SongTitle": "Still In Love"
},
{
"Artist": "No One You Know",
"AlbumTitle": "Somewhat Famous",
"Genre": "Country",
"Year": 1984,
"SongTitle": "Somewhere Down The Road",
"CriticRating": 8.4
},
{
"Artist": "The Acme Band",
"AlbumTitle": "The Buck Starts Here",
"Genre": "Rock",
"Price": 0.99,
"SongTitle": "Look Out, World"
},
{
"Artist": "No One You Know",
"AlbumTitle": "Hey Now",
"Genre": "Country",
"Price": 1.98,
"SongTitle": "My Dog Spot",
"CriticRating": 8.4
}
]
},
"UnprocessedKeys": {}
}

정리

To be continued…

Share