More Related Content
Similar to [AWSマイスターシリーズ] Amazon DynamoDB
Similar to [AWSマイスターシリーズ] Amazon DynamoDB (20)
More from Amazon Web Services Japan
More from Amazon Web Services Japan (20)
[AWSマイスターシリーズ] Amazon DynamoDB
- 1. re:G
ene
AWS マイスターシリーズ
rate
Amazon DynamoDB
2013.10.02
アマゾン データサービス ジャパン株式会社
ソリューションアーキテクト 今井 雄太
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 2. アジェンダ
! NoSQLとRDB
! DynamoDBとは
! DynamoDBを使ってみる
! ツールとエコシステム
! When to use
! まとめ
2
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 3. NoSQLとRDB
3
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 5. NoSQL vs RDB
! NoSQL
• BASE属性
• ⼤大容量量データ
• スケールしやすい
• 書き込みが速い
• スキーマが柔軟
! RDB
• ACID属性
• Atomicity
• Consistency
• Isolation
• Durability
• トランザクション
• クエリがリッチ
• ジョインも得意
5
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 6. BASE?
6
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 7. Basically Availabl, Soft state,
Eventually consistent
! RDBの提供するACID属性と対⽐比するためによく使われ
る
! 厳密な⼀一貫性やデータの即時反映などをあきらめる代わ
りに、スケーラビリティを取る。というような意味。
! すべてのNoSQLが完全にBASEとは限らないが、⼀一貫性
やトランザクションなど、従来のACIDなRDBと⽐比べて、
諦めている要件がある。
7
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 11. DynamoDBとは
11
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 12. DynamoDBとは
! ・NoSQL as a Service
! ・超⾼高速・予測可能な⼀一貫したパフォーマンス
! ・シームレスなスケーラビリティ、そして低コスト
運⽤用管理理必要なし
低レイテンシ、SSD
プロビジョンスループット
無限に使えるストレージ
12
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 13. DynamoDBの特徴
! 管理理不不要で信頼性が⾼高い
! プロビジョンスループット
! ストレージの容量量制限がない
13
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 14. 特⻑⾧長1:管理理不不要で信頼性が⾼高い
! SPOFの存在しない構成
! データは3箇所のAZに保存されるので信頼性が⾼高い
! ストレージは必要に応じて⾃自動的にパーティショニング
される
クライアント
14
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 16. 特⻑⾧長3:ストレージの容量量制限がない
! 使った分だけの従量量課⾦金金制のストレージ
! データ容量量が増えてきたのでディスクを⾜足したり、ノー
ドを⾜足したりという作業が不不要
16
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 19. DynamoDBを使ってみる
19
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 23. 実際によく使う操作
• Get/Put/Update/Delete/BatchGet
• Scan
• テーブル総ナメする
• Query
• ハッシュ+レンジキーのみ
23
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 25. オフィシャルSDK以外にも
! Perl
• Net::Amazon::DynamoDB
! Erlang
• wagerlabs/ddb
• https://github.com/wagerlabs/ddb
! Go
• go-‐‑‒dynamodb
• https://github.com/fabiokung/go-‐‑‒dynamodb
25
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 26. テーブル設計のための要素(1)
! Table
• Primary Keyとして”Hash key”もしくは”Hash key & Range
key”を選択する
! プライマリキー:Hash key
• Hash key単体でデータを⼀一意に識識別できるケースに使う
! プライマリキー:Hash key & Range key
• Hash keyに該当する複数のデータからRange keyで絞り込み検
索索ができる
! Local Secondary Indexes
• Range key以外に絞り込み検索索のためのキーを持つことができ
る
26
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 27. テーブル設計のための要素(2)
! Attributes
• データの中⾝身。Hash key, Range keyに該当するAttributes以
外は事前に定義する必要はない。また、レコード間でAttributes
が不不揃いであっても問題ない。
! Attributesの型
•
•
•
•
•
•
String
Number
Binary
String Set
Number Set
Binary Set
27
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 28. DynamoDBのデータモデル
テーブル
Hash keyまたはHash key & Range key
アイテム
プライマリキー
アトリビュート
28
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 29. DynamoDBのプライマリキー
• ハッシュキー
• シンプルなキーバリュー
• ハッシュ値なので、ソートなし
• ハッシュキー+レンジキー
• コンポジットプライマリーキー
• レンジキーはソートあり
29
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 30. サンプル(1) 商品カタログ
! スキーマのイメージ
• テーブル名はProducts
• 商品ID(ProductId)をHash keyとするテーブル
ProductId
(Hash key)
ProductName Price
・・・
・・・
1
Pen
100
・・・
・・・
2
Pencil
50
・・・
・・・
3
Eraser
100
・・・
・・・
30
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 32. サンプル(1) 商品カタログ
! テーブルの作成
• 商品IDをHash keyとするテーブル
• Primary KeyにHashを選択して、Hash keyの名前と型を決め
る
32
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 33. サンプル(1) 商品カタログ
! node.jsのSDKで触ってみる
• データを投⼊入
var aws = require(‘aws-sdk’);
aws.config.loadFromPath(‘PATH/TO/CREDENTIAL’);
var dynamoDb = new aws.DynamoDB.Client();
33
dynamoDb.putItem(
{
TableName:"Products",
Item: {
ProductId : {N:"1"},
ProductName: {S:"Pen"},
Price: {N:"100"}
}
},
function(err,data){
if(err){ console.log(err); }else{
console.log(data);
}
}
);
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 34. サンプル(1) 商品カタログ
! node.jsのSDKで触ってみる
• データを1件取得する
dynamoDb.getItem(
{
TableName:"Products",
Key: {
ProductId : {N:"1"},
},
function(err,data){
if(err){ console.log(err); }else{
console.log(data);
}
}
);
34
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 35. サンプル(2) ⾏行行動ログ
! スキーマのイメージ
• テーブル名はAudience
• ユーザーID(AudienceId)をHash key、TimestampをRange
keyとするテーブル
AudienceId
(Hash key)
Action
Url
・・・
1
2013-10-01
00:01:01
Login
・・・
・・・
2
2013-10-01
00:02:02
Login
・・・
・・・
1
2013-10-01
00:21:00
Login
・・・
・・・
1
35
Timestamp
(Range key)
2013-10-01
00:42:00
ViewHoge
1
2013-10-01
00:56:22
PostHoge
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 37. サンプル(2) ⾏行行動ログ
! テーブルの作成
37
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 38. サンプル(2) ⾏行行動ログ
! node.jsのSDKで触ってみる
• データを投⼊入
var data = new Array(
{ AudienceId: { N:"1" }, Timestamp: { S:"2013-‐‑‒10-‐‑‒01 00:01:01" }, Action: { S:"Login" } },
{ AudienceId: { N:"2" }, Timestamp: { S:"2013-‐‑‒10-‐‑‒01 00:02:02" }, Action: { S:"Login" } },
{ AudienceId: { N:"1" }, Timestamp: { S:"2013-‐‑‒10-‐‑‒01 00:21:00" }, Action: { S:"Login" } },
{ AudienceId: { N:"1" }, Timestamp: { S:"2013-‐‑‒10-‐‑‒01 00:42:00" }, Action: { S:"ViewHoge" } },
{ AudienceId: { N:"1" }, Timestamp: { S:"2013-‐‑‒10-‐‑‒01 00:56:22" }, Action: { S:"PostHoge" } }
);
38
for (var i =0; i < data.length; i++){
dynamoDb.putItem(
{
TableName:"Audience",
Item: data[i]
},
function(err,data){
if(err){
console.log(err);
}else{
console.log(data);
}
}
);
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
}
- 39. サンプル(2) ⾏行行動ログ
! node.jsのSDKで触ってみる
• あるユーザーの10⽉月1⽇日 0時10分以降降の⾏行行動ログを取得する
39
dynamoDb.query(
{
TableName:"Audience",
KeyConditions: {
"AudienceId": {
ComparisonOperator:"EQ",
AttributeValueList:[ {N:"1"} ]
},
"Timestamp": {
ComparisonOperator:"GT",
AttributeValueList:[ {S:"2013-‐‑‒10-‐‑‒01 00:10:00"} ]
}
}
},
function(err,data){
if(err){
console.log(err);
}else{
console.log(data);
}
}
);
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 40. サンプル(3) LSI付き⾏行行動ログ
! ユーザーのアクティビティログにLocal Secondary
Indexes(LSI)をつけてみる
AudienceId
(Hash key)
Action
Url
(Action-Index)
・・・
1
2013-10-01
00:01:01
Login
・・・
・・・
2
2013-10-01
00:02:02
Login
・・・
・・・
1
2013-10-01
00:21:00
Login
・・・
・・・
1
40
Timestamp
(Range key)
2013-10-01
00:42:00
ViewHoge
1
2013-10-01
00:56:22
PostHoge
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 41. Local Secondary Indexes(LSI)
• Get/Put/Update/Delete/BatchGet
• Scan
• テーブル総ナメする
• Query
• ハッシュ+レンジキーのみ
この制限を取り除くのがLocal Secondary Index!
41
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 42. Local Secondary Indexes (LSI)
! 任意のアトリビュートに張れるQuery⽤用Index
• 各テーブルに5つまで
Forumの投稿を保持するテーブルの例:
LSIを定義すればKeyで
ないアトリビュートに関し
てもQuery可能に
例:
- Repliesが10件以上
- 最後の投稿がXX以降
LSIなしのテーブルでは
QueryはRange Keyのみ
42
RepliesIndex
LastPostIndex
LSI
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 43. なんで”Local” Secondary Indexesなの?
! ハッシュキーが⼀一致する範囲の中のSecondary Indexで
あるため
àハッシュキーが異異なるアイテムを横断的にQueryすることは出
来ない
RepliesIndex
RepliesIndexを使って1
Queryで取得可能なのは?
• ForumNameがS3で
Repliesが9以上
• 任意のForumNameで
Repliesが9以上
à 3つハッシュキーがある
ので3 Query必要
43
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 45. アトリビュートのプロジェクション
! Index⽤用テーブルに指定したアトリビュートを複製保存
する機能
• 例例:ThreadテーブルのRepliesをLastPostIndexにプロジェク
ション
LastPostIndex
45
Query時にRepliesを取得する場合は読み込みコストが低減
à性能の向上、ReadThroughputの節約
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 46. サンプル(3)LSI付き⾏行行動ログ
! ユーザーのアクティビティログにLocal Secondary
Indexes(LSI)をつけてみる
• ⼀一度度作ったテーブルのLSIは変更更できないので、新たに
Audience2というテーブルを作って設定する
• 今回はAction-‐‑‒IndexというLSIを作って、”Action”というアトリ
ビュートに設定する
46
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 47. サンプル(3)LSI付き⾏行行動ログ
! Action-‐‑‒Index(LSI)を使ってデータを取得する
47
dynamoDb.query(
{
TableName:"Audience2",
IndexName: “Action-‐‑‒Index”,
KeyConditions: {
"AudienceId": {
ComparisonOperator:"EQ",
AttributeValueList:[ {N:"1"} ]
},
“Action": {
ComparisonOperator:“EQ",
AttributeValueList:[ {S:“Login"} ]
}
}
},
function(err,data){
if(err){
console.log(err);
}else{
console.log(data);
}
}
);
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 48. テーブル設計で気をつけること: プライマリキー
! Hash keyとRange keyの概念念
Parition1 Partition2 Parition3
1 2 3 4 5 6 7 8 9
Range key
Partition内での
データの並びを
保証する
DynamoDBのスループッ
トは、各Partitionに負荷
が分散されたときに想定通
りの数字がでるように設計
されている。ひとつの
Paritionだけではプロビ
ジョンしたスループットの
数字はでないので注意。
Hash key
Partition間での
データ分散に利利
48
⽤用される
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
A,D
B,E
C,F
- 49. テーブル設計で気をつけること: プライマリキー
! Hot Key問題:特定のHash keyに対してデータ
が集中すると性能が劣劣化する(想定通りに性能
がでない)ので注意。
! 前述のAudienceテーブルでのAudienceIdのよ
うに、データが特定のキーに集中しにくいもの
を選ぶ。
! 例例えば、「年年⽉月」みたいなデータが集中しやす
いキーをHash keyにするのはアンチパターン。
49
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 50. テーブル設計で気をつけること: LSI
! Local Secondary IndexesはストレージとIOの
コストを押し上げる
• LSIはいわゆる”射影”で実装されている
• 射影されるアトリビュートのストレージコストが膨らむ
• Write時の書き込みコストも上がる
! 本当に必要なものだけに絞ってLSIを適⽤用する
! 射影(Projection)するAttributesも必要なものに絞る
50
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 51. ツールとエコシステム
51
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 53. DynamoDB Local
! 開発/テスト⽤用のツール
• 今までは開発やテストをするために実際のDynamoDBのテーブ
ルを作る必要があった。これには”費⽤用が発⽣生する”、”静的なテ
スト環境を作れない”、”オフラインで開発できない”などの問題
があった。
• このツールを利利⽤用することにより、開発やCIをより便便利利に⾏行行う
ことができる
! JARファイルで提供され、ローカルにインストールして
動かすことができる(要Java7)
! あくまでAPIの機能的を再現しているテストツールなの
で本番では利利⽤用しない
! プロビジョンスループットは再現されていないので注意
! 詳細は http://bit.ly/1d9fN5c を参照
53
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 54. Transaction Library for DynamoDB
! AWS SDK for Javaのひとつの機能としての実装
• このSDKを使うことでDynamoDB上でTransactionを利利⽤用できる
• ライブラリ側でTransaction管理理テーブルを作成することにより
Transactionを実現している
! 使い⽅方は次のページのサンプルコードを参照
! 詳細は http://bit.ly/16KbppP
54
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 55. Transaction Library for DynamoDB
55
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 56. Transaction Library for DynamoDB
56
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 57. Amazon EMRのHiveから利利⽤用する
! hiveのExternal Tableとして利利⽤用可能
• DynamoDB上のデータを集計したいときなどに利利⽤用
CREATE EXTERNAL TABLE Audience
( AudienceId Int, ActionTimestamp string, Action string )
STORED BY
'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHa
ndler'
TBLPROPERTIES (
"dynamodb.table.name" = ”Audience2",
"dynamodb.column.mapping" =
”AudienceId:AudienceId,
ActionTimestamp:Timestamp,
Aciton:Action“ );
57
• 詳細は http://amazn.to/19goT17
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 59. Amazon Redshiftにデータをロードする
! COPYコマンドでRedshiftにデータをロード
COPY audience
FROM ʻ‘dynamodb://Audience2ʼ’
CREDENTIALS
'aws_̲access_̲key_̲id=<Your-‐‑‒Access-‐‑‒Key-‐‑‒
ID>;aws_̲secret_̲access_̲key=<Your-‐‑‒Secret-‐‑‒Access-‐‑‒
Key>'
READRATIO 50;
• 詳細は http://amazn.to/19goT17
59
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 60. When to use
60
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 69. そういうところにこそDynamoDB
69
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 71. DynamoDBにマッチするユースケースを
考えてみる
! ユーザーの⾏行行動ログを格納
• Hash keyにユーザーID、Range keyにタイムスタンプ
• 広告のターゲティングのための⾏行行動ログ
• ゲーム内のユーザーの⾏行行動を記録するためのログ
! ストレージのためのインデックス
• Hash keyにObject名
• S3に格納したデータのインデックスとして
! 投票システムやフラッシュマーケティング
• 投票や応募対象のIDをHash key
• 短期間でスパイクするトラフィックを捌くために
71
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 72. ユースケース
72
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 73. 逆に、マッチしないユースケースは?
! 集計
• データを全件SCANするようなユースケースはあまり向かない
• やるのであればHiveやRedshiftと組み合わせる
! トランザクション
• ライブラリの提供はあるが、NoSQLの特性としてもともと得意
ではない(できるがコストが⾼高い)
73
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 74. まとめ
74
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 75. まとめ
!
!
!
!
DynamoDBはNoSQL as a Service
運⽤用の⼿手間がかからない。
NoSQLはRDBとは⽤用途が違う
システムを全部DynamoDBで構築す
る必要はない。適材適所でRDBと組
み合わせて使う。
75
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 76. 参考資料料
! Amazon DynamoDB のベストプラクティス、使⽤用⽅方法、
およびツールなどの情報がこちらにまとまっています。
• http://aws.amazon.com/jp/dynamodb/resources/
76
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 78. ありがとうございました!
78
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.