NoSQL を Ruby で実践するための n 個の方法
NoSQL を Ruby で実践するための 1 個の方法
n = 1 である異論は認めにゃいにゃ
西村 友裕 (@Sixeight)にしむら ともひろ
もくじ
• NoSQL とは
• MongoDB について
• MongoMapper を使う
• まとめ
もくじ
• NoSQL とは
• MongoDB について
• MongoMapper を使う
• まとめ
NoSQL
「SQL 止めようぜ」
「SQL 止めようぜ」×
Not only SQL
リレーショナルでないデータベースの
発展促進運動のこと
リレーショナルでない?
スキーマに縛られない
CREATE TABLE cats ( id int NOT NULL, name text, PRIMARY KEY (id));INSERT INTO cats VALUES (1, “tama”);
CREATE TABLE cats ( id int NOT NULL, name text, PRIMARY KEY (id));INSERT INTO cats VALUES (1, “tama”);
斑の色も入れたいにゃー
$ mongo> db.cats.insert({name: “tama”})
ここまでは一緒にゃ
$ mongo> db.cats.insert({name: “tama”})> db.cats.insert({ name: “goro”, dapple_color: “gray” })> db.cats.find(){ "_id" : ObjectId("4c49118783d1600c126d2147"), "name" : "tama" }{ "_id" : ObjectId("4c49119983d1600c126d2148"), "name" : "goro", "dapple_color" : "gray" }
なんでも入るにゃー
スケールするように設計されている
• Google BigTable
• amazon Dynamo
• HBase
• Apache Casandra
• CounchDB
• MongoDB
• and more
• Google BigTable
• amazon Dynamo
• HBase
• Apache Casandra
• CounchDB
• MongoDB
• and more
オープンソース
• Google BigTable
• amazon Dynamo
• HBase
• Apache Casandra
• CounchDB
• MongoDB
• and more
今日はこれを見るにゃ
もくじ
• NoSQL とは
• MongoDB について
• MongoMapper を使う
• まとめ
MongoDBby 10gen
MongoDB は、C++ で書かれた、オープンソースのドキュメント指向データベースである。 (wikipedia)
MongoDB は、C++ で書かれた、オープンソースのドキュメント指向データベースである。 (wikipedia)
スケーラブル
ハイパフォーマンス
スキーマフリー
• ドキュメント指向ストレージ (the simplicity and power of JSON-like data schemas)
• 動的な クエリー
• 組み込みのオブジェクトと配列をサポートした完全な Index のサポート。
• クエリー プロファイリング
• 速い in-place アップデート
• バイナリデータの効率的な保存 large objects (例:写真や動画)
• レプリケーション とフェイルオーバーのサポート。
• クラウドレベルのスケーラビリティな 自動的なsharding
• 複雑な集約のための MapReduce
• 商用サポート、ホスティング、コンサルティング
公式サイトより
ドキュメント指向
各ドキュメントがスキーマ情報を持つ
?
まずドキュメントってなに?
コレクション ≒ テーブルドキュメント ≒ レコード
リレーショナルドキュメント指向
各ドキュメントがスキーマ情報を持つ
{ "_id" : ###, "name" : "tama" }
たま
ドキュメント
{ "_id" : ###, "name" : "tama" }{ "_id" : ###, "name" : "goro", "dapple_color" : "gray" }
たま
コレクション
ごろう
{ "_id" : ###, "name" : "tama" }{ "_id" : ###, "name" : "goro", "dapple_color" : "gray" }
たま
ごろう
!
導入事例
over 200 projects
もくじ
• NoSQL とは
• MongoDB について
• MongoMapper を使う
• まとめ
MongoMapperhttp://github.com/jnunemaker/mongomapper
ActiveRecord に似たAPI
+ 周辺ライブラリの充実
gem install mongo_mapper
簡単にゃ
使う
class Cats include MongoMapper::Document
key :name, Stringend
irb で使う
class Cats include MongoMapper::Document
key :name, Stringend
class Cats include MongoMapper::Document
key :name, Stringend
class Cats include MongoMapper::Document
key :name, Stringend
irb:001:0> Cats.create :name => 'tama'
irb:002:0> Cats.first
=> #<Cats name: "tama", _id: BSON::ObjectID('###')>
irb:003:0> Cats.create :name => 'goro', :dapple_color => ‘gray’
irb:004:0> Cats.count
=> 2
irb:005:0> Cats.first(:name => ‘goro’)
=> #<Cats name: "goro", dapple_color:”gray”,_id: ###>
Array, Binary, Boolean, Date, Float, Hash
Integer, Nil, ObjectId, Set, String, Time
Custom Type, and TypeLess
Array, Binary, Boolean, Date, Float, Hash
Integer, Nil, ObjectId, Set, String, Time
Custom Type, and TypeLess
TypeLessってにゃに?
class Storage include MongoMapper::Document
key :valueend
class Storage include MongoMapper::Document
key :valueend
irb:001:0> Storage.create :values => 25
irb:002:0> Storage.create :value => 'String'
irb:003:0> Cats.all
=> [#<Storage value: 25, _id: BSON::ObjectID('###')>,
#<Storage value: “String”, _id: BSON::ObjectID('###')>]
Array, Binary, Boolean, Date, Float, Hash
Integer, Nil, ObjectId, Set, String, Time
Custom Type, and TypeLess
にゃるほど!
アソシエーション
class Family include MongoMapper::Document key :name, String many :catsend
class Cats include MongoMapper::Document key :name, String key :family_id, ObjectId belongs_to :familyend
class Person include MongoMapper::Document key :name, String one :addressend
class Address include MongoMapper::EmbeddedDocument key :zipcode, String key :street, Stringend
バリデーション
• validates_presence_of :title
• validates_presence_of
• validates_length_of
• validates_format_of
• validated_numericality_of
• validates_acceptance_of
• validated_configmati
class Cats include MongoMapper::Document key :name, String
validates_presence_of :nameend
class Cats include MongoMapper::Document key :name, String, :required => trueend
class Cats include MongoMapper::Document key :name, String, :required => trueend
実は一緒にゃんです
コールバック
before / after
validate, create, save
class Cats include MongoMapper::Document key :name, String
before_validation :add_nyan
private def add_nyan self.name += ‘ nyan’ endend
irb:001:0> Cats.create :name => 'tama'
irb:002:0> Cats.first
=> #<Cats name: "tama nyan", _id: BSON::ObjectID('###')>
拡張する
基本機能もプラグイン
module Charisma def fly puts “I’m flying” endend
MongoMapper::Document. append_inclusions(Charisma)
Cats.first(:name => ‘tama’).fly
module Charisma def fly puts “I’m flying” endend
MongoMapper::Document. append_inclusions(Charisma)
Cats.first(:name => ‘tama’).fly
実は飛べるにゃ
もくじ
• NoSQL とは
• MongoDB について
• MongoMapper を使う
• まとめ
MongoDB
ドキュメント指向DB
と KVS と RDBMS
の良いとこどり
MongoMapper
ActiveRecord のように簡単に MongoDB
を利用できる
使ってみるにゃ
参考 URL
• http://www.mongodb.org/
• http://www.mongodb.org/display/DOCSJP/Home
• http://wiki.github.com/jnunemaker/mongomapper/
• http://gihyo.jp/dev/serial/01/ruby/0033