28
Ruby on Amazon DynamoDB #1 Hamamatsu.rb, @jacoyutorius 1

Ruby with AWS DynamoDB

Embed Size (px)

Citation preview

Page 1: Ruby with AWS DynamoDB

Ruby

on

Amazon DynamoDB #1

Hamamatsu.rb, @jacoyutorius 1

Page 2: Ruby with AWS DynamoDB

About

Hamamatsu.rb, @jacoyutorius 2

Page 3: Ruby with AWS DynamoDB

{ "name": "yuto ogi", "twitter": "@jacoyutorius", "skills": ["ruby", "javascript", "aws"], "note": "AWS SAM localが気になります"

}

Hamamatsu.rb, @jacoyutorius 3

Page 4: Ruby with AWS DynamoDB

Amazon DynamoDB

4 フルマネージド型NoSQLデータベース

Hamamatsu.rb, @jacoyutorius 4

Page 5: Ruby with AWS DynamoDB

RDBとの違い4 スケールのしかた4 RDBは垂直方向、DynamoDBは水平方向

Hamamatsu.rb, @jacoyutorius 5

Page 6: Ruby with AWS DynamoDB

scaleup

4 マシンのメモリやディスクサイズを拡張

Hamamatsu.rb, @jacoyutorius 6

Page 7: Ruby with AWS DynamoDB

scaleout

4 同じスペックの複製を作成して並列化

Hamamatsu.rb, @jacoyutorius 7

Page 8: Ruby with AWS DynamoDB

用語の違い

SQL DynamoDB MongoDB

テーブル テーブル コレクション

行 項目 ドキュメント

列 属性 フィールド

PK PK ObjectId

Index セカンダリインデックス インデックス

Hamamatsu.rb, @jacoyutorius 8

Page 9: Ruby with AWS DynamoDB

Amazon DynamoDB

4 パーティションキーとソートキー4 パーティションキー4 いわゆるハッシュのKey

4 ソートキー4 パーティションキーとソートキーの2つのキーの組み合わせでレコードを一意に識別する

Hamamatsu.rb, @jacoyutorius 9

Page 10: Ruby with AWS DynamoDB

DynamoDB local

4 ローカルで実行できるDynamoDB

(Management ConsoleのGUIが使いやすいのでわざわざlocalでやる必要ないかも)

Hamamatsu.rb, @jacoyutorius 10

Page 11: Ruby with AWS DynamoDB

DynamoDB localダウンロードするだけ。 Java必要。

$java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

Hamamatsu.rb, @jacoyutorius 11

Page 12: Ruby with AWS DynamoDB

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

Page 13: Ruby with AWS DynamoDB

結論

『辛い』

Hamamatsu.rb, @jacoyutorius 13

Page 14: Ruby with AWS DynamoDB

gems

4 aws/aws-sdk-ruby

4 aws/aws-sdk-ruby-record

Hamamatsu.rb, @jacoyutorius 14

Page 15: Ruby with AWS DynamoDB

Aws::Record

4 ruby製のDynamoDBクライアントgem4 DynamoDBのテーブルとRubyのクラスをマッピング4 ActiveRecordっぽく操作できる

(無理して使うこともないかな・・・)

Hamamatsu.rb, @jacoyutorius 15

Page 16: Ruby with AWS DynamoDB

setup

require "aws-sdk"require "aws-record"

Aws.config.update(endpoint: "http://localhost:8000")client = Aws::DynamoDB::Client.new

Hamamatsu.rb, @jacoyutorius 16

Page 17: Ruby with AWS DynamoDB

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

Page 18: Ruby with AWS DynamoDB

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

Page 19: Ruby with AWS DynamoDB

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

Page 20: Ruby with AWS DynamoDB

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

Page 21: Ruby with AWS DynamoDB

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

Page 22: Ruby with AWS DynamoDB

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

Page 23: Ruby with AWS DynamoDB

scanテーブルの検索

music = Music.scanmusic.each do |row| puts row.nameend

#=> "Primal Scream"

Hamamatsu.rb, @jacoyutorius 23

Page 24: Ruby with AWS DynamoDB

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

Page 25: Ruby with AWS DynamoDB

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

Page 26: Ruby with AWS DynamoDB

impressions

4 RDBとは全く異なるので大変4 今のところDynamoDBじゃなければいけないデータを扱うことは無いのでテーブル設計のイメージがしずらい

4 わざわざRubyでやることも無いかな(LambdaのRuby対応が来ればもしくは)

4 NodeかPython使えばいいのでは

Hamamatsu.rb, @jacoyutorius 26

Page 27: Ruby with AWS DynamoDB

reference

4 https://aws.amazon.com/jp/dynamodb/

Hamamatsu.rb, @jacoyutorius 27

Page 28: Ruby with AWS DynamoDB

to be continued

=> https://gist.github.com/jacoyutorius/4b8f265cfb541e342cc7749fd8900610

Hamamatsu.rb, @jacoyutorius 28