Ruby with AWS DynamoDB
Transcript of Ruby with AWS DynamoDB
{ "name": "yuto ogi", "twitter": "@jacoyutorius", "skills": ["ruby", "javascript", "aws"], "note": "AWS SAM localが気になります"
}
Hamamatsu.rb, @jacoyutorius 3
用語の違い
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
gems
4 aws/aws-sdk-ruby
4 aws/aws-sdk-ruby-record
Hamamatsu.rb, @jacoyutorius 14
Aws::Record
4 ruby製のDynamoDBクライアントgem4 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_titleend
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 :nameend
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: trueend
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_titleend
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.scanmusic.each do |row| puts row.nameend
#=> "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_titleend
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
to be continued
=> https://gist.github.com/jacoyutorius/4b8f265cfb541e342cc7749fd8900610
Hamamatsu.rb, @jacoyutorius 28