Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

20120831 mongoid

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
Osc2008 Opensuse Moonlinx
Osc2008 Opensuse Moonlinx
Chargement dans…3
×

Consultez-les par la suite

1 sur 52 Publicité

Plus De Contenu Connexe

Similaire à 20120831 mongoid (20)

Publicité

Plus récents (20)

Publicité

20120831 mongoid

  1. 1. Mongoidを語ってみよう 2012/08/31 @akimatter 13年1月24日木曜日
  2. 2. 自己紹介 秋間武志 RBCスタッフ 仕事でmongoidを使って約2年 株式会社グルーヴノーツ @akimatter https://github.com/akm http://d.hatena.ne.jp/akm/ 13年1月24日木曜日
  3. 3. アジェンダ NoSQLのおさらい MongoDBの概要 Mongoidの概要 Mongoid3の変更点 MongoDB 2.2の新機能 Mongoidでのモデリング 13年1月24日木曜日
  4. 4. NoSQLのおさらい 13年1月24日木曜日
  5. 5. NoSQLとは NoSQLとは、リレーショナルデータベース管理シ ステム(RDBMS)以外のデータベース管理システム を指し、リレーショナルデータベースの長い歴史 を打破するものとして、広い意味での関係モデル 以外に属するデータベースの発展を促進させよう とする運動である。 http://ja.wikipedia.org/wiki/NoSQL より 13年1月24日木曜日
  6. 6. 実装 産業界での有名な実装 GoogleのBigTable アマゾンのAmazon DynamoDB オープンソースの実装 MongoDB Apache HBase Apache Cassandra 13年1月24日木曜日
  7. 7. 分散データベース いくつかのNoSQLシステムは分散アーキテクチャ を採用している 13年1月24日木曜日
  8. 8. CAP定理 ブリュワーの定理とも呼ばれ、分散コンピュータ システムのマシン間の情報複製に関する定理。 ウェブサービスを想定して作られた定理。同時に 以下の3つを満たすことはできないというもの。 一貫性 (Consistency) 可用性 (Availability) 分断耐性 (Partition-tolerance) 13年1月24日木曜日
  9. 9. 13年1月24日木曜日
  10. 10. 13年1月24日木曜日
  11. 11. Eventually Consistency システム内に、一時的にConsistentでない状態が生 まれても、ある期間の後には、Consistentな状態 になるような性質を、Eventually Consistencyとい う。 13年1月24日木曜日
  12. 12. Eventually Consistency この言葉が定義されたことで「Scalableで Availableで、かつ、Eventually Consistentなシステ ムは可能である。」と言うことが可能になった。 実際、mongoidにも、consistencyの選択肢 に:eventual と :strongがある 13年1月24日木曜日
  13. 13. BASE CAPの全部を満たさなくてもシステムとして十分 Basically Available Soft-state Eventually consistent http://qcontokyo.com/tokyo-2009/pdf/ GeneralSession-Day2-Maruyama.pdf 13年1月24日木曜日
  14. 14. MongoDBの概要 13年1月24日木曜日
  15. 15. 特徴 「MongoDB はドキュメント志向のDBMSです。 MySQLですがJSON型オブ ジェクトを伴い、RDBMS表ではなく、データモデルで構成されます。大切 なことは、MongoDBは、結合やトランザクションをサポートしないという ことです。しかし二次的インデックスで、クエリ言語表示、アトミックがド キュメントレベルで書き込みを行い一貫した読み込みをするのを特徴としま す。 操作上、MongoDBは、自動範囲ベース自動フェイルオーバーと水平スケー リングを内蔵する、マスタースレーブ・レプリケーションを特徴としま す。」 http://jp.docs.mongodb.org/manual/faq/fundamentals/#what-kind-of-database-is- mongodb 13年1月24日木曜日
  16. 16. 例えばこんな構成も 13年1月24日木曜日
  17. 17. 分散アーキテクチャ レプリケーション マスター/スレイブ レプリカセット シャーディング 13年1月24日木曜日
  18. 18. JOINなし 「Mongoでは、リレーショナルデータベースのデ ザインでするような"正規化"はそれほど必要ありま せん。サーバサイドでの"join"がないからです。」 http://www.mongodb.org/pages/viewpage.action? pageId=7209156 13年1月24日木曜日
  19. 19. 埋め込みドキュメント ドキュメントに複数のドキュメントを埋め込むこ とができる db.employees.insert({_id:   ObjectId("4d85c7039ab0fd70a117d734"),   name:  'Ghanima',  family:  {mother:   'Chani',  father:  'Paul',  brother:   ObjectId("4d85c7039ab0fd70a117d730")}}) http://www.mongodb.org/pages/viewpage.action?pageId=18907286 13年1月24日木曜日
  20. 20. 組み込み vs 参照 http://www.mongodb.org/pages/viewpage.action?pageId=7209156 13年1月24日木曜日
  21. 21. トランザクションなし 「MongoDBは ACIDトランザクションを提供しま せん。」 http://jp.docs.mongodb.org/manual/faq/ fundamentals/#does-mongodb-support- transactions 13年1月24日木曜日
  22. 22. アトミックな操作 「MongoDBは、一つのドキュメント でのアト ミックな操作をサポートしています」 http://www.mongodb.org/pages/viewpage.action? pageId=7209573 13年1月24日木曜日
  23. 23. トランザクション 「トランザクションが満たすべき技術的要件に ACID特性がある」 http://ja.wikipedia.org/wiki/トランザクション 13年1月24日木曜日
  24. 24. ACIDについて比較 RDB MongoDB 原子性 atomicity ○ △ 一貫性 consistency ○ ○ 独立性 isolation ○ ○ 永続性 durability ○ ○ http://ja.wikipedia.org/wiki/ACID_(コンピュータ科学) 13年1月24日木曜日
  25. 25. consistencyという言葉 「一貫性のあるサービスでは、操作は完了するか、一切何もしないかの、ど ちらかだ。 ギルバートとリンチは、一貫性ではなく原子性(atomic)という 語を証明の中で使っている。 理論的な文脈では原子性という方が要領を得 ている。 というのも細かい話をすると、一貫性(Consistent) は データ ベーストランザクションの望ましい性質をあらわす ACID の C に使われて おり、 これはあるデータが事前に与えられた制約を破った状態で残らない ことを意味している。 ところが CAP の C を分散システムでの事前の制約 とみなし、 同じデータをあらわす複数の値を許さないことにすると、抽象 化が漏れて紛らわしいことになる。 (それに、もしブリュワーが原子性 (atomic)という語を使っていたら AAP 定理になる。 こんなのを発音しよ うとしたら皆が病人みたいになってしまう。)」 http://www.hyuki.com/yukiwiki/wiki.cgi?BrewersCapTheorem 13年1月24日木曜日
  26. 26. Mongoidの概要 13年1月24日木曜日
  27. 27. ODM for MongoDB Mongoid (pronounced mann-goyd) is an Object- Document-Mapper (ODM) for MongoDB written in Ruby. It was conceived in August, 2009 during a whiskey-induced evening at the infamous Oasis in Florida, USA by Durran Jordan. http://mongoid.org/en/mongoid/index.html 13年1月24日木曜日
  28. 28. Mongoidの哲学 The philosophy of Mongoid is to provide a familiar API to Ruby developers who have been using Active Record or Data Mapper, while leveraging the power of MongoDB's schemaless and performant document-based design, dynamic queries, and atomic modifier operations. MongoIdの哲学は、MongoDBのスキーマレスとパフォー マンスのドキュメントベースの設計、動的なクエリ、お よびアトミックな更新操作のパワーを活用しながら、 ActiveRecordやDataMapperを使用しているRubyの開発 者におなじみのAPIを提供することです。 13年1月24日木曜日
  29. 29. Documents Fields Array BigDecimal Boolean Date class Person DateTime include Mongoid::Document Float field :first_name, type: String Hash field :middle_name, type: String Integer field :last_name, type: String Moped::BSON::ObjectId end Range Regexp String Symbol Time TimeWithZone 13年1月24日木曜日
  30. 30. Persistence Standard Model.create, Model.create!, Model#save, Model#save!, Model#update_attributes, Model#update_attributes!, Model#update_attribute, Model#upsert, Model#touch, Model#delete, Model#destroy, Model.delete_all, Model.destroy_all この辺はActiveRecordと同じ感じ 13年1月24日木曜日
  31. 31. Persistence Atomic Model#add_to_set, Model#bit, Model#inc, Model#pop, Model#pull, Model#pull_all, Model#push, Model#push_all, Model#rename, Model#set, Model#unset コールバックもバリデーションも呼ばれないから 要注意 13年1月24日木曜日
  32. 32. relations Mongoid Mongoid ActiveRecord Reference Embed belongs_to belongs_to embedded_in has_many has_many embeds_many has_one has_one embeds_one has_and_belongs_ has_and_belongs_ N/A to_many to_many 13年1月24日木曜日
  33. 33. IDの振り方 ActiveRecordでは、RDBサーバのautoincrementの カラムをidとして使用する。 MongoDBでは主キーとなる_idの値はクライアン トで生成する必要がある。 Mongoidではドライバ のmopedが行う。この値はUUIDを用いる。衝突す る可能性はxxxくらい。 13年1月24日木曜日
  34. 34. Mongoid3の変更点 13年1月24日木曜日
  35. 35. 構造 13年1月24日木曜日
  36. 36. origin One of MongoDB's greatest features is its ability to execute dynamic queries, which Origin abstracts in a familiar Arel-style DSL that Mongoid includes. 13年1月24日木曜日
  37. 37. moped Goal of this gem Thread-safe No more extension (bson_ext) Cleanest API Better ReplicaSet management 13年1月24日木曜日
  38. 38. moped limitation Limitation Ruby 1.9 only No GridFS support The only case where moped is slower than bson_ext is the ObjectId generation 13年1月24日木曜日
  39. 39. MongoDB2.2 13年1月24日木曜日
  40. 40. New Features Aggregation Framework TTL Collections Concurrency Improvements Improved Data Center Awareness with Tag Aware Sharding Fully Supported Read Preference Semantics http://docs.mongodb.org/manual/release-notes/ 13年1月24日木曜日
  41. 41. Mongoidでのモデリング 13年1月24日木曜日
  42. 42. Tengineの例 TengineはGroovenautsが開発している分散環境 におけるイベントハンドリングおよびジョブの 実行制御のためのフレームワーク https://github.com/tengine/tengine 13年1月24日木曜日
  43. 43. 分散処理でのカウント 13年1月24日木曜日
  44. 44. 分散処理でのカウント 13年1月24日木曜日
  45. 45. 分散処理でのカウント 13年1月24日木曜日
  46. 46. 公式ドキュメントの例 http://mongoid.org/en/mongoid/docs/ relations.html 13年1月24日木曜日
  47. 47. モデリングのポイント ◦ リレーショナルモデリングは利用可能なデータ構造駆動 だ。この設計をするにあたって重要なテーマは"自分は どの答えを持っているか?" ◦ NoSQLデータモデリングはアプリケーション特有のアク セスパターン、すなわち、サポートされるクエリの型駆 動だ。メインテーマは"自分は何を知りたいのか?" https://gist.github.com/2396234 13年1月24日木曜日
  48. 48. 組み込み vs 参照 再び http://www.mongodb.org/pages/viewpage.action?pageId=7209156 13年1月24日木曜日
  49. 49. 関連テーブルとか 13年1月24日木曜日
  50. 50. 関連テーブルとか 13年1月24日木曜日
  51. 51. 参考文献 12年後のCAP定理: "法則"はどのように変わったか http://www.infoq.com/jp/articles/cap-twelve-years-later-how-the-rules- have-changed MongoDBドキュメント http://www.mongodb.org/display/DOCSJP/Home Cloudの技術的特徴について 丸山先生 http://qcontokyo.com/tokyo-2009/pdf/GeneralSession-Day2- Maruyama.pdf 13年1月24日木曜日
  52. 52. 参考文献 NoSQLデータモデリング技法 https://gist.github.com/2396234 ブリュワーの CAP 定理 http://www.hyuki.com/yukiwiki/wiki.cgi?BrewersCapTheorem 13年1月24日木曜日

×