Ruby
on
Amazon DynamoDB #1
Hamamatsu.rb, @jacoyutorius 1
About
Hamamatsu.rb, @jacoyutorius 2
{
"name": "yuto ogi",
"twitter": "@jacoyutorius",
"skills": ["ruby", "javascript", "aws"],
"note": "AWS SAM localが気になります"
}
Hamamatsu.rb, @jacoyutorius 3
Amazon DynamoDB
4 フルマネージド型NoSQLデータベース
Hamamatsu.rb, @jacoyutorius 4
RDBとの違い
4 スケールのしかた
4 RDBは垂直方向、DynamoDBは水平方向
Hamamatsu.rb, @jacoyutorius 5
scaleup
4 マシンのメモリやディスクサイズを
拡張
Hamamatsu.rb, @jacoyutorius 6
scaleout
4 同じスペックの複製を作成して並
列化
Hamamatsu.rb, @jacoyutorius 7
用語の違い
SQL DynamoDB MongoDB
テーブル テーブル コレクション
行 項目 ドキュメント
列 属性 フィールド
PK PK ObjectId
Index セカンダリインデックス インデックス
Hamamatsu.rb, @jacoyutorius 8
Amazon DynamoDB
4 パーティションキーとソートキー
4 パーティションキー
4 いわゆるハッシュのKey
4 ソートキー
4 パーティションキーとソートキーの2つのキーの組み合
わせでレコードを一意に識別する
Hamamatsu.rb, @jacoyutorius 9
DynamoDB local
4 ローカルで実行できるDynamoDB
(Management ConsoleのGUIが使いやすいのでわざわざlocalでやる必要ないかも)
Hamamatsu.rb, @jacoyutorius 10
DynamoDB local
ダウンロードするだけ。 Java必要。
$java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
Hamamatsu.rb, @jacoyutorius 11
start DynamoDB local
$curl localhost:8000
{
"__type":"com.amazonaws.dynamodb.v20120810#MissingAuthenticationToken",
"message":"Request must contain either a valid (registered) AWS access key ID or X.509 certificate."
}
または、http://localhost:8000/shell/
Hamamatsu.rb, @jacoyutorius 12
結論
『辛い』
Hamamatsu.rb, @jacoyutorius 13
gems
4 aws/aws-sdk-ruby
4 aws/aws-sdk-ruby-record
Hamamatsu.rb, @jacoyutorius 14
Aws::Record
4 ruby製のDynamoDBクライアントgem
4 DynamoDBのテーブルとRubyのクラスをマッピング
4 ActiveRecordっぽく操作できる
(無理して使うこともないかな・・・)
Hamamatsu.rb, @jacoyutorius 15
setup
require "aws-sdk"
require "aws-record"
Aws.config.update(endpoint: "http://localhost:8000")
client = Aws::DynamoDB::Client.new
Hamamatsu.rb, @jacoyutorius 16
table
class Music
set_table_name :Music
string_attr :artist, hash_key: true
string_attr :song_title, range_key: true
string_attr :album_title
end
4 artistをハッシュキー、song_titleをソートキー。
4 2つの値でレコードを一意に識別する。
Hamamatsu.rb, @jacoyutorius 17
Artist
id name
1 AJICO
2 Bill Evans
3 Cream
class Artist
set_table_name :Artist
integer_attr :id, hash_key: true
string_attr :name
end
Hamamatsu.rb, @jacoyutorius 18
Album
artist title
AJICO AJICO SHOW
Bill Evans Portrait in JAZZ
Bill Evans Waltz for Debby
Cream BBC Sessions
class Album
set_table_name :Album
string_attr :artist, hash_key: true
string_attr :title, range_key: true
end
Hamamatsu.rb, @jacoyutorius 19
Song
artist song_title album_title
Bill Evans Waltz for Debby Waltz for Debby
Bill Evans Detour Ahead Waltz for Debby
Bill Evans Autumn Leaves Portrait in JAZZ
class Song
set_table_name :Song
string_attr :artist, hash_key: true
string_attr :song_title, range_key: true
string_attr :album_title
end
Hamamatsu.rb, @jacoyutorius 20
migration
テーブルの作成
migration = Aws::Record::TableMigration.new(Music, client: client)
migration.create!(
provisioned_throughput: {
read_capacity_units: 5,
write_capacity_units: 2
}
)
migration.wait_until_available
Hamamatsu.rb, @jacoyutorius 21
putitem
レコードのインサート
music = Music.new(
artist: "Primal Scream",
song_title: "Where The Light Gets In",
album_title: "Chaosmosis")
music.save!
=> <struct Aws::DynamoDB::Types::PutItemOutput attributes=nil,
consumed_capacity=nil,
item_collection_metrics=nil>
Hamamatsu.rb, @jacoyutorius 22
scan
テーブルの検索
music = Music.scan
music.each do |row|
puts row.name
end
#=> "Primal Scream"
Hamamatsu.rb, @jacoyutorius 23
query
テーブルの検索
params = {
table_name: "Music",
key_conditions: {
"artist" => {
attribute_value_list: ["Foo Fighters"],
comparison_operator: "EQ"
}
}
}
musics = Music.query(params)
musics.each do |row|
p row.song_title
end
Hamamatsu.rb, @jacoyutorius 24
find
テーブルのプライマリキーからレコードを抽出する。
pp Music.find(artist: "Bill Evans", song_title: "Autumn Leaves")
#<Music:0x007f84a8d2e248
@data=
#<Aws::Record::ItemData:0x007f84a8d2e180
@clean_copies=
{:artist=>"Bill Evans",
:song_title=>"Autumn Leaves",
:album_title=>"(1969)Autumn Leaves",
:favorite=>nil}, ...
Hamamatsu.rb, @jacoyutorius 25
impressions
4 RDBとは全く異なるので大変
4 今のところDynamoDBじゃなければいけないデータを扱
うことは無いのでテーブル設計のイメージがしずらい
4 わざわざRubyでやることも無いかな(LambdaのRuby対応
が来ればもしくは)
4 NodeかPython使えばいいのでは
Hamamatsu.rb, @jacoyutorius 26
reference
4 https://aws.amazon.com/jp/dynamodb/
Hamamatsu.rb, @jacoyutorius 27
to be continued
=> https://gist.github.com/jacoyutorius/
4b8f265cfb541e342cc7749fd8900610
Hamamatsu.rb, @jacoyutorius 28

Ruby with AWS DynamoDB