1. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQLの基礎知識識
河野 達也 / Tatsuya Kawano
R&D ソフトウェアエンジニア
クラウディアン株式会社
2013年年7⽉月30⽇日
⽇日本データマネジメント・コンソーシアム(JDMC)
研究会テーマ2 第2回オープン研究会
2. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.2
本⽇日の内容
•NOSQLの特徴
•データモデルによる分類
•MongoDB、Cassandra、VoltDBの実演
•アーキテクチャによる分類(簡単に)
•主要な製品(7+1製品)の紹介
•活⽤用事例例(Cloudian)の紹介
3. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
この勉強会を終えると
•NOSQL製品を分類するための
キーワードが理理解できる
•主要な製品の特徴が説明できる
•NOSQLの使いどころが説明できる
3
4. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
駆け⾜足です
•本当は2⽇日くらいかけて学ぶ内容を
3時間でやります
•このスライドの置き場所
•http://bit.ly/jdmc-‐‑‒nosql
https://github.com/tatsuya6502/nosql-‐‑‒ja/tree/jdmc-‐‑‒jul-‐‑‒2013/slides
•推薦図書(4冊)も紹介します
4
5. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
河野 達也 / Tatsuya Kawano
エバンジェリスト
開発者
著者の⼀一⼈人
5
6. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
クラウディアン株式会社
• Cloudian
S3完全準拠オブジェクトストレージ
• Hibari DB
GBクラスのウェブメールに採⽤用
• NOSQL afternoon in Japanを主催(2010年年)
• グローバル開発リーダー Gary Ogasawara
• Inktomiで開発を経験(CAP定理理のE. Brewer⽒氏
らが⽴立立ち上げた会社)
6
7. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
本セミナー資料料のライセンス(使⽤用条件)
• スライド
クリエイティブコモンズ
表⽰示-‐‑‒⾮非営利利 3.0 ⾮非移植 (CC BY-‐‑‒NC 3.0)
• 原著作者のクレジットを表⽰示し、
• かつ、営利利⽬目的で利利⽤用しなければ、
• 本作品を複製、頒布、展⽰示、実演することができる。
• ⼆二次的著作物を作成することができる。
• ソースコード
MITライセンス
7
8. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQLの
特徴
スケーラビリティー
半構造のデータ
8
9. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQL: Not Only SQL
• ウィキペディア「NoSQL」のページより
• NoSQL(⼀一般に “Not only SQL” と解釈される)は、リレーショナル
データベース管理理システム (RDBMS) 以外のデータベース管理理システ
ムを指す、おおまかな分類語
• RDBをやみくもに使⽤用してきた⻑⾧長い歴史を打破し、それ以外のデータ
ベースの利利⽤用・発展を促進させようとする運動の標語となっている
• このようなデータベースの傾向として以下が挙げられる
• 固定されたスキーマに縛られない
• 関係モデルの結合操作を利利⽤用しないこと(場合によっては単にそ
のような機能が⽋欠落落しているだけ)
• ⽔水平スケーラビリティが確保しやすい事が多いこと
• トランザクションを利利⽤用できないものが多いこと
9
http://ja.wikipedia.org/wiki/NoSQL
10. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
http://www.slideshare.net/sunsuk7tp/hbase-at-line
10
なぜ NOSQL なのか?
11. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
M2M センサーデータ
0序
章
ビ
ッ
グ
デ
ー
タ
の
時
代
図0-3 経済産業省「スマートメーター制度研究会報告書」より抜粋
11
12. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
従来のリレーショナルDBでは対応が難しい
•膨⼤大な量量
•12TB/⽇日
•速く処理理する
•12TB ÷ 80MB/秒 ≒ 42時間
•半構造データ
•全てのデータを均⼀一に整えるのは難しい
12
13. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
ビッグデータに対応するための新技術
BigtableやDynamoの論文発表が契機となり、ソフトウェアによる大
規模分散技術は俄かに脚光を浴びました。ビッグデータに直面していた
Webサービスのエンジニア達がこれに触発され、BigtableやDynamoの
図0-2 Google BigtableとAmazon Dynamoの発表論文(表紙の一部)
13
14. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
⼤大⼿手Webサービスが利利⽤用を開始
ンソースとしてリリースされました。その後 Cassandraは、大規模な
ニュース関連SNSであるDigg(ディッグ)や、クラウドサービスの大手
図1-2 代表的なNOSQLの利用企業の例
14
15. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
SQL以外のデータベースを総称
•NOSQLは世界に100種類以上
•“One size does not fit all”
•知名度度だけで選ぶと⼤大失敗する
•⽤用途に合った製品を選ぶためには、NOSQLに
使われる技術を理理解し、個々の製品の特徴を
把握する必要がある
15
16. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
⼀一般的なNOSQL製品の特徴
• スケールアウト性(⽔水平スケーラビリティー)に優れるものが多い
• 汎⽤用的で価格のこなれたハードウェアを多数並べることで、処理理能
⼒力力、データの格納容量量、故障への耐性などを⾼高める
2第
2
章
N
O
S
Q
L
の
デ
ー
タ
モ
デ
ル
す。こうした対策方法を「スケールアップ」と呼びます(図
の対策は、新たにサーバーを追加し、各サーバーにデータを
するという方法です。データが増えた場合、サーバーを次々
とで、データの保存容量を拡張していくというものです。こ
ケールアウト」と呼んでいます(図2-7)。NOSQLの基本的な
ップのイメージ
スケールアップ
2.3
の方法を「スケールアウト」と呼んでいます(図2-7)。NO
考え方は、スケールアップではなくこちらの方です。
スケールアウト
図2-7 スケールアウトのイメージ
16
17. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
⼀一般的なNOSQL製品の特徴(続き)
•固定されたスキーマに縛られない
•複雑で多様に変化するデータ構造に対応
•⾼高可⽤用性と⾼高信頼性
•耐障害性
•サービス無停⽌止でノード追加
17
18. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQLは⽤用途を絞り込んだデータベース
•機能が少ない
•リレーショナルモデルの結合操作ができない
•トランザクションが使えないものが多い
•逆に、ある⽤用途のために機能を特化・強化し
たものもある(例例:グラフ型NOSQL)
•データの整合性が緩い(結果整合性など)
•製品の成熟度度?
•主要な製品は成熟期にさしかかりつつある
18
19. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
データモデル
による分類
キー・バリュー型
ドキュメント指向型
カラムファミリー型
グラフ型
http://beautifuldata.net/2012/01/
telling-stories-with-network-data-instagram-in-china/
19
20. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQLのデータモデル
http://www.neo4j.org/learn/nosql
20
21. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
リレーショナルDB(RDB)のデータモデル
•⾏行行と列列で構成されるテーブルを定義
•テーブル間の関係性を定義
•正規化により冗⻑⾧長⽣生と不不整合を排除
RDBでは、行と列で構成されるテーブル(表)の形でデータ構造の設
計をし、そのうえでテーブル間の関係性を定義します。図にすると図2-1
2-1 RDBのデータモデルのイメージ
_責.indd 050_責.indd 050 2012/04/06 10:59:322012/04/06 10:59:32
21
22. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
RDBにおけるデータ分散
分散環境では性能が
スケールしずらい
キーを指定するだけでバリューを探し出せるキー・バリュー型で
におけるデータ分割のイメージ
サーバー 1 サーバー 2
サーバー 3 サーバー 4
22
23. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
キー・バリュー型
•辞書のようなデータ形式
す。新しいデータが追加されるごとに、行が加えられて
が増えるに従って、表が縦の方向に伸びていくイメージ
ー型に該当するNOSQLデータベースには、Dynamo、
、Hibari、Redis、Scalaris(スカラリス)、Tokyo Cabi-
あります。
型データモデルのイメージ
23
24. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
キー・バリュー型におけるデータ分散
テーブル間のリレーション
やトランザクションがない
ため、容易易にスケールする
2第
2
章
N
O
S
Q
L
の
デ
ー
タ
モ
デ
ル
複数のサーバーに複数のバリューを複製する
サーバー 1 サーバー 2
サーバー 3 サーバー 4
・バリュー型におけるデータ分割のイメージ
24
25. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
キー・バリュー型におけるデータの複製
のような課題に対するソリューションを実装しておらず、単独のサー
サーバー 1 サーバー 2
サーバー 3
複製複製
複製
0 キー・バリュー型におけるデータの複製
25
26. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
ドキュメント指向型
•バリューに構造を持ったデータを格納
•各項⽬目にインデックスを付けられる
{ author: 'joe',
created: new Date('03/28/2009'),
title: 'Yet another blog post',
text: 'Here is the text...',
tags: [ 'example', 'joe' ],
comments : [ { author: 'jim', comment: 'I disagree'},
{ author: 'nancy', comment: 'Good post' }]
}
26
27. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
カラムファミリー型
27
http://www.datastax.com/docs/1.1/ddl/column_family
28. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
カラムファミリー型(続き)
0 6 32.4 カラム指向型の特徴
ファミリー」を定義します。その中でツィートIDは、図2-14のように、新
しいツィートが発生するごとに、行として縦方向に追加されていきます。
このデータベースにおいて、ユーザーのツィートを取得するには、ユー
ザーライン・カラムファミリーからユーザーのツィートIDを取得し、各
図2-14 カラム指向型における行の追加
ツィート・カラムファミリー
行キー
キー(ツィートID)
新しいツィートごとに新しい行を加える
t342
タイムスタンプ
1234567
ボディ
NOSQL is fun
ユーザーネーム
gemini
ユーザー ID
u2415
カラム
72_第2章_責.indd 06372_第2章_責.indd 063 2012/04/06 10:59:472012/04/06 10:59:4728
2第
2
章
N
O
図2-13 カラムの名前を固定しなくてよい
ユーザーライン・カラムファミリー
行キー タイムスタンプ
ツィート ID
キー(ユーザー ID)
新しいツィートごとに新しい行を加える
1234569
t389
1234568
t353
1234567
t342u2415
カラム
新しいツィート毎に新しい列列を加える
29. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
グラフ型
29
http://www.neo4j.org/learn/try
• グラフ構造の扱いに特化
• データ同⼠士の繋がりを可視化、独⾃自のクエリ⾔言語で分析
• 最短経路路の検索索
• ソーシャルグラフ、ジオメトリックグラフとリコメンド計算
• データ構造上、スケールアウトは困難
30. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Demo
ドキュメント指向型NOSQL
30
31. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
このデモでは
•MongoDB を⽤用いてドキュメント指向型
NOSQLのデータモデルとクエリを確認し
ます。
•⽐比較のために、インメモリ型リレーショ
ナルDBMSの VoltDB も紹介します。
31
デモで使⽤用するコード:
https://github.com/tatsuya6502/nosql-‐‑‒ja/tree/jdmc-‐‑‒jul-‐‑‒2013/mongodb-‐‑‒ruby
32. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモの流流れ
•リレーショナルDBMS(VoltDB)で、
MovieLensのデータ構造を確認
•VoltDBでSQLを使ったクエリ
•MongoDBでデータ構造と、その柔軟性
を確認
•MongoDBでSQLと同等のクエリを実⾏行行
32
33. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモのまとめ
•ドキュメント内のフィールドはドキュメント
毎に⾃自由に持てる。データ構造に対する柔軟
性が⾼高い
•MongoDBでは様々なクエリがサポートされ
ている。集計はMap Reduceで⾏行行う
33
34. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモのまとめ(続き)
• MongoDBだけで⼗十分?
• ⼤大半の⽤用途に⼗十分な性能を持つ
• 以下のようなケースでは、カラムファミリー型やキ
ー・バリュー型も検討する
• さらに⼤大規模なデータを扱いたい
• さらに低いレイテンシー、または、⾼高いスループッ
トが求められる
• ACIDなトランザクションは実現できない
• リレーショナルモデルが最適な⽤用途なら、VoltDBのよ
うなNewSQLも選択肢に
34
35. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモのまとめ(続き)
•VoltDB は NewSQL と呼ばれ、NOSQLに匹
敵するスケーラビリティーを持ちながら、リ
レーショナルモデルとACIDなトランザクシ
ョンを提供する
•⼀一⽅方で柔軟性やエラスティック性(弾⼒力力性)
を犠牲にしている
•サービス中のスキーマ変更更ができない
•サービス中のノード追加に制限がある
35
36. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
アーキテクチャ
による分類
データ分割
CAP定理理
36
37. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
データ分割 コンシステント・ハッシング
して、その整理番号に従って、リングの各スペースにキーを割り当て
。キーが割り当てられたスペースを時計回りで進み、そこで最初に配
れているノードにデータを書き込むというルールのアルゴリズムです。
3-13では、ハッシュ値を0.0から1.0の間で設定しています。4つの
ドがリング状に配置され、ノードAには0.0、ノードBには0.25、ノー
コンシステント・ハッシングの概念図
0.0
0.5
グレーのノードにデータの複製を割り当て
ノード
A
ノード
C
ノード
D
ノード
B
0.75 0.25
キーのハッシュ値
0.20
データ複製データ複製
ノード C が取り除かれた場合
キーのハッシュ値
0.45
図3-16 コンシステント・ハッシングにおける負荷割り当ての調整
0.0
ノード C にデータの割り当てを増やす
ノード
A
ノード
D
ノード
B
0.75
ノード
C
0.70
0.25
37
38. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
データ分割 シャーディング
3-19のようにBigtableでは、辞書配列に従ってキーの値がソートさ
Bigtableのデータ割当て
タブレット(リージョン)
行キー:
[ Arkansas , California )
行キー:
[ California , California-LosAngeles )
行キー:
[ California-LosAngeles , Colorado )
行キー:
[ Colorado , Florida )
タブレット
(リージョン)
サーバー 1
タブレット
(リージョン)
サーバー 2
ベージコレクション(Garbage Collection:GC)とは、プログラムが実行の際に確保したメモリ
域のうち、不要になった領域を解放する処理です。同様に、ハードディスク上で不要になった領域38
39. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Eric BrewerのCAP定理理
• 注意
• C or Aはクエリの都度度
調整可能
• 証明されておらず
厳密には定理理ではない
3第
3
章
ア
ー
キ
テ
ク
チ
ャ
の
基
本
概
念
と
技
術
ACM(Association for Computing Machinery)が主催したPODC(Prin-
ciple Of Distributed Computing)シンポジウムにおける「Towards Ro-
bust Distributed Systems」と題した基調講演の中でのことでした。
図3-6 Eric BrewerのCAP定理
「分散システムにおいては、
これら 3 つのうち最大 2 つ
しか満たすことができない」
(2000 年 7 月 19 日 PODC 基調講演)
Availability
可用性
Consistency
整合性
Partitions
分断耐性
Tolerance to network
39
40. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
データ複製時の整合性
①クライアント1がAに対して更新要求を出し、Aは自身の持つデー
タを更新する。
②Aはデータが更新されたことをBに伝え、Bは自身の持つデータ
クライアント 1 クライアント 2
通常のケース:
(1)クライアントはデータの更新要求を A または B のいずれか
一方に送る。
(2)A と B はデータが更新されたことを他方に通知する。
(3)通知を受けた側は、自分のデータにその更新を反映する。
レプリケーション(複製)
リクエスト
BA
データデータ
40
41. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
分割耐性
•ネットワークが分断されても
その特性(CP、または、AP)を維持する
41
42. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
CPとAP ネットワーク分割が発⽣生
3第
3
章
ア
ー
キ
テ
ク
チ
ャ
の
基
本
概
念
と
技
術
ョンや業務上の要求条件次第です。
生じた場合
クライアント 2クライアント1
CP(整合性と分断耐性)の場合:
リクエストは片方のグループ(仮に A)
だけが受け付け、他のグループは
自主的に停止する。
A B
AP(可用性と分断耐性)の場合:
リクエストは全てのグループが受け
付けるが、A と B のデータは不整合
ク
ラ
イ
ア
ン
ト
が
書
き
込
み
リ
ク
A B
ライアント1 クライアント 2
A B
クライアント 2クライアント1
A B
AP(可用性と分断耐性)の場合:
リクエストは全てのグループが受け
付けるが、A と B のデータは不整合
となる。
データデータ
ア
ン
ト
が
書
き
込
み
リ
ク
エ
ス
ト
を
送
る
42
43. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
整合性と性能のトレードオフ
R+W>Nの場合には、整合性が保証できる
図3-10 Quorumの概念図[R+W<N]
書き込み(W=1) 読み出し(R=1)
R+W<N の場合
複製(N=3)
書き込みが 1 つのノードに行われているが、ノード
1.2.3 のどのノードから 1 つだけデータを読み出すか
はわからない。
ノード 1 ノード 2 ノード 3
? ? ?? ? ?
43
•結果整合性(Eventual Consistency)
44. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
整合性と性能のトレードオフ(続き)
0 8 93.2 データの整合性に関する概念と技術
果的にデータの整合性を保証できることになります。
図3-11 Quorumの概念図[R+W>N]
書き込み(W=2) 読み出し(R=2)
R+W>N の場合
複製(N=3)
書き込みが 2 つのノードに行われ、読み出しが 2 つ
のノードに行われれば、必ず書き込みが行われた 2
つのノードのうちの 1 つに行きつく。
ノード 1 ノード 2 ノード 3
44
•強い整合性(Strong Consistency)
45. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
広域複製の課題
•ネットワークの遅延が⼤大きい環境
•Strong Consistencyでは性能が⼤大きく劣劣化
•Eventual Consistencyではアプリケーショ
ン・ロジックが複雑になる
•イベントの因果関係に着⽬目した Causal
Consistencyという⼿手法もある
45
46. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Causal Consistency
• Aさんが、写真をアップロード
• Aさんが、写真をアルバムに登録
• B君が、Aさんのアルバムを開く
• Eventual Consistencyではビューに不不整合が発⽣生する
かもしれない
• 写真がまだ登録されていないのに、更更新されたアル
バムが⾒見見える
• Causal Consistencyではこのようなことは避けられる
46
47. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Causal Consistency
•Strong Consistencyより緩く、以下のよう
なことは実現できない
•トランザクショナルな操作
•ユニーク性などのintegrity制約
•Causal Consistencyは、今後、広域複製で
注⽬目されそうな⼿手法
47
48. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Demo
カラムファミリー型NOSQL
48
49. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
このデモでは
•Cassandra に MovieLens を格納します。
•CQL3のテーブル定義を確認
•CQL3でクエリ
•moviesカラムファミリーとratingsカラムファ
ミリーの内部構成を確認
49
デモで使⽤用するコード:
https://github.com/tatsuya6502/nosql-‐‑‒ja/tree/jdmc-‐‑‒jul-‐‑‒2013/cassandra-‐‑‒cql3
50. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
CQL3層での MovieLens データ構造
50
movie_̲id title genres ...
1
788
Toy Story {Anime, Comedy, For-‐‑‒Children} ...
Relative Fear {Horror, Thriller} ...
movie_̲id user_̲id timestamp rating
1 1
1 2
788 1
788 4
... 5
... 3
... 3
... 3
movies テーブル
ratings テーブル
51. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Cassandraストレージ層でのデータ構造
51
⾏行行キー movies カラムファミリーmovies カラムファミリーmovies カラムファミリー ratings カラムファミリーratings カラムファミリーratings カラムファミリーratings カラムファミリーratings カラムファミリーratings カラムファミリーratings カラムファミリー
11
788788
title genres:Anime ... 1: 1:timestamp 1:rating 2: 2:timestamp 2:rating ...
Toy Story 空欄 空 ... 5 空 ... 3
title genres:Horror ... 1: 1:timestamp 1:rating 4: 4:timestamp 4:rating ...
Relative Fear 空欄 空 ... 3 空 ... 3
• ratingsカラムファミリーは横(列列⽅方向)に成⻑⾧長する
• このような数千・数万の列列を持つ⾏行行を「wide row」と呼ぶ
• moviesとratingsは、リレーショナルモデルで2つのテーブルを結合した時
とそっくりの構造
• このように、テーブルを結合した状態でデータを格納することをリレーシ
ョナルモデルでは、⾮非正規化と呼ぶ。管理理はしづらくなるが、性能を⼤大き
く改善できるテクニックのひとつ
52. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモのまとめ
52
•カラムファミリー型 NOSQL では、カラム
を⾃自由に追加できる性質を利利⽤用して、リレ
ーショナルDBにおけるテーブルの⾮非正規化
と同じことを実現できる
•⾮非正規化により結合操作を代⽤用できるだけ
でなく、Readを⾼高速化できる
53. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
デモのまとめ(続き)
53
• CassandraではSQL⾵風のクエリ⾔言語とスキーマを導
⼊入し、モデリングの難しさを排除しようとしてい
る。wide rowもCQL3で複合キーを持つテーブルを
定義することで、内部的に作成される
• CQL3はスケーラビリティーを保つために、リレー
ショナルモデルの結合操作はサポートしない(代わ
りに wide row、つまり、複合キーを持つテーブル
やコレクション型を使う)
• ⼀一⽅方で、VoltDBでは柔軟性や弾⼒力力性を犠牲にした
54. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
主な製品
Cassandra
HBase
Riak
MongoDB
Redis
Infinispan
Neo4J
VoltDB (NewSQL)
CAP定理に基づいたNOSQLデータベースの分類が、Nathan Hurst
氏のブログ「Visual Guide to NOSQL Systems」*2
で紹介されています。
図3-7を見ると、ほぼ全てのNOSQLデータベースが、CP(整合性と分
断耐性)かAP(可用性と分断耐性)に分類されていることがわかると思い
図3-7 CAP定理によるNOSQLの分類
可用性 :
どのクライアントも
常に読み出しと書き込み
ができる
整合性 :
すべてのクライアント
が常に同一のデータを
見ている
分断耐性 :
物理ネットワークが分断
されても間違った結果を
引き起こさない
CA
AP
RDBMS(MySQL、
Postgres 等)
Aster Data
Greenplum
Vertica
Dynamo
Voldemort
Tokyo Cabinet
KAI
Cassandra
SimpleDB
CouchDB
Riak
CP
Bigtable
Hypertable
HBase
MongoDB
Terrastore
Scalaris
Berkeley DB
Memcache DB
Redis
2 者選択
A
C P
データモデル
リレーショナル型(比較対照)
キー・バリュー型
カラム指向型/テーブル
ドキュメント指向型
54
55. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Apache Cassandra
• Amazon DynamoとGoogle Bigtableを参考に設計
された
• カラムファミリー型
• 緩い整合性(調整可能AP→CP)
• コンシステント・ハッシングを採⽤用し、スケールア
ウト性に優れる
• ⼤大規模データに強く、100台程度度のクラスターの運
⽤用実績が豊富。Writeが⾼高速なことが特徴
• 最新のバージョンでは、CQL3というSQL⾵風のクエリ
⾔言語を備える
55
56. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• Google Bigtableを参考に設計
• カラムファミリー型
• 強い整合性(CP)
• ⾃自動シャーディングを採⽤用し、スケールアウト性に
優れる
• ⼤大規模データに強く、100台程度度のクラスターの運
⽤用実績が豊富。 Hadoop環境と相性がよい
56
57. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• Amazon Dynamoを参考に設計
• キー・バリュー型
• JSONの扱いに優れ、使い勝⼿手はドキュメント指向
型に近い
• 緩い整合性(調整可能AP→CP)
• コンシステント・ハッシングを採⽤用し、スケールア
ウト性に優れる
• ⼤大規模データに強く、 50台程度度のクラスターの運⽤用
実績が豊富。 安定性が⾼高く、扱いやすいことが特徴
57
Basho Riak
58. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
•ドキュメント指向型
•強い整合性(CP)
•分散環境では 緩い整合性(AP)になる
•データモデルの扱いやすさと、強⼒力力なクエリ
が特徴。ウェブアプリ開発で特に⼈人気が⾼高い
•分散構成では、⾃自動シャーディングとレプリ
ケーションクラスターを組み合わせることに
なり、リソースの使⽤用効率率率が悪い
58
59. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• キーバリュー型
• リストやハッシュなど、データ構造をネイティブで
サポート
• 強い整合性(CP)
• インメモリ、⾼高速
• リッチなデータ構造と⾼高速性から、ランキング情報、
ユーザーのアクティビティ統計情報などのデータ格納
先として⼈人気がある
• ⾼高可⽤用性構成に対応しているが、現バージョンでは、
スケールアウトはできない
59
Redis
60. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• JBoss Cacheから派⽣生した、データグリッド製品
• JavaのキャッシュAPIに準拠
• キーバリュー型、インメモリ、⾼高速
• Java以外のクライアントもサポート
• 強い整合性(CP)だが、⾮非同期操作などのスループットを⾼高める
仕組みが豊富
• ACIDなトランザクションをサポート
• コンシステント・ハッシングを採⽤用し、スケールアウト性に優れる
• キャッシュローダーにより、既存のデータストアとバックグラウン
ドでデーターを同期できる
• RDBMS、Amazon S3、Cassandraなどに対応
60
61. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• 主な⽤用途
• キャッシュとして
• Hibernateの2次キャッシュ
• JBoss AS(Application Server)のセッション
キャッシュ
• Luceneインデックスの格納
• データグリッドとして
• 株式のトレーディングシステム
• ニア・リアルタイムのデータ分析、リスク計算
61
続き
62. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
•グラフ型
•グラフエンジンのデファクトスタンダード
•深いグラフのトラバースでは、RDBMSの
1000倍以上の性能を発揮
•ACIDなトランザクションをサポート
•専⽤用のクエリ⾔言語を持ち、グラフの可視化が
できる(対話型の分析ツールにもなる)
62
63. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
•オンライントランザクション処理理に特化した
インメモリ型のリレーショナルデータベース
•Postgres や Ingres などのデータベース開発
を⼿手がけた Michael Stonebraker⽒氏が設計
•NewSQLと呼ばれる
•SQLとACIDなトランザクションが使える
•⾼高速でスケールアウト性に優れる
63
64. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
• 柔軟性や弾⼒力力性(エラスティック性)が犠牲に
• 事前にストアドプロシージャを書いてコンパイルする。
アドホックなクエリでは性能を発揮できない
• 結合できるテーブル数に限りがある
• 分析⽤用途には不不向き。RDBMSやHadoopなどへの差分
データ連携機能を備える
• サービス稼働中にスキーマの変更更はできない
• サービス稼働中のノード追加に制限がある
• 現状、エンタープライズ版のみノード追加が可能。さら
に、複製機能をオフにしたテーブルのみが対象となる
64
66. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
事例例
Cloudian
Redis
Cassandra
HyperStore
http://cloudian.jp/cloud-storage-products/cloudian.html
66
67. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Cloudian™の論論理理アーキテクチャ
67
Admin
サーバー
S3サーバー
QoS DB
(Redis)
認証情報
(Redis)
アカウント情報
(Cassandra)
ユーザーデータ
(Cassandra,
HyperStore)
レポート
(Cassandra)
Servlets
管理理コンソール
ログイン
アカウント設定/
セキュリティキー
レポート
Data Explorer
HTTPS
HTTPS
HTTP
HTTP
ウェブUI
アプリケーション
HTTP
HTTP or
HTTPS
(S3)
データサーバー
68. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
オブジェクトストアとしてのCassandra
•BLOBストレージ
• グループごとに column family を作成
• ⾏行行キー <バケットID>/<オブジェクトID>
•オブジェクトのメタデータ
• ACL (アクセスコントロールリスト)、オブジェクトのサイズ・・・
• Cassandra ⾏行行キャッシュを活⽤用
•巨⼤大なS3オブジェクトのサポート
• マルチパート Amazon S3 multi-‐‑‒part APIを使ってアップロード
• チャンキング ⼤大きなオブジェクトを⼩小さなチャンク(例例 10MB)に分割して保存
• HTTPレンジヘッダー ダウンロード時は HEAD リクエストでオブジェクトのサイズを
取得してから、スタートのバイト位置と⻑⾧長さを指定してダウンロード
• HyperStore™ 巨⼤大なオブジェクトをネイティブなファイルシステムに保存
68
69. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
⼤大きなデータの扱いで性能が劣劣化
Cassandra 1.0 28.551 9.992 379.745 155.680 1,699 3,060,198
Cassandra 0.8.6 34.099 8.402 1,015.888 333.048 1,336 2,406,446
遅延時間の中央値
(ミリ秒)
遅延時間の中央値
(ミリ秒)
遅延時間の
95パーセンタイル値
(ミリ秒)
遅延時間の
95パーセンタイル値
(ミリ秒)
スループット
(件/秒)
30分間で
処理理した
件数
Get Put Get Put
スループット
(件/秒)
30分間で
処理理した
件数
Cassandra 1.0 1.016 0.949 2.476 4.789 8,748 15,755,306
Cassandra 0.8.6 1.282 0.948 5.729 2.243 8,700 15,668,017
バリューサイズ 1KB
出典:「NOSQLの基礎知識識」 リックテレコム、2012年年4⽉月出版
バリューサイズ 100KB
69
70. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
HyperStore™(特許出願中)
HyperStore
• ストレージのハイブリッド化により
処理性能とディスク利用効率の向上を実現
• オブジェクトの大きさに応じて
最適なストレージを自動選択
• メタデータは引き続きCassandraに格納
• パーティショニング、レプリケーション、ノード
の死活監視は、Cassandraの分散機能を使用
Admin
サーバー
S3
サーバー
QoS
(Redis)
認証情報
(Redis)
Accounting
(Cassandra)
Data Store
(Cassandra)
Reporting
(Cassandra)
Cloudian
サーバー
データストア
HyperStore™
Manager
70
71. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
HyperStore: レイテンシーの測定
0
12.5
25.0
37.5
50.0
0 1 10 100 1000
KB
PUT-Cass
PUT-HS
ms
0
15.0
30.0
45.0
60.0
0 1 10 100 1000
GET-Cass
GET-HS
ms
>30% faster
>400% faster
KB
71
72. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
推薦図書
72
73. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NOSQLの基礎知識識
ビッグデータを活かす
データベース技術
本橋信也 (著)
河野達也 (著)
鶴⾒見見利利章 (著)
太⽥田 洋 (監修)
256ページ
出版社: リックテレコム (2012/4/25)
ISBN-‐‑‒10: 4897978874
ISBN-‐‑‒13: 978-‐‑‒4897978871
73
74. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NoSQLデータベースフ
ァーストガイド
佐々⽊木 達也 (著)
MongoDB、Redis
232ページ
出版社: 秀和システム (2011/04)
ISBN-‐‑‒10: 4798029599
ISBN-‐‑‒13: 978-‐‑‒4798029597
74
75. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
NoSQLプログラミング
実践活⽤用技法
Shashank Tiwari (著)
中村 泰久 (監修)
⻑⾧長尾 ⾼高弘 (翻訳)
HBase、Cassandra、
MongoDB、Redis
407ページ
出版社: 翔泳社 (2012/5/18)
ISBN-‐‑‒10: 4798126055
ISBN-‐‑‒13: 978-‐‑‒4798126050
75
76. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
まつもとゆきひろ
コードの未来
まつもと ゆきひろ (著)
MongoDB、VoltDB
360ページ
出版社: ⽇日経BP社 (2012/5/17)
ISBN-‐‑‒10: 4822234630
ISBN-‐‑‒13: 978-‐‑‒4822234638
76
77. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
この勉強会を終えると(再)
•NOSQL製品を分類するための
キーワードが理理解できる
•主要な製品の特徴が説明できる
•NOSQLの使いどころが説明できる
77
78. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Questions?
http://gplus.to/tatsuya6502
http://twitter.com/tatsuya6502
78
Slides: http://bit.ly/jdmc-‐‑‒nosql
https://github.com/tatsuya6502/nosql-‐‑‒ja/tree/jdmc-‐‑‒jul-‐‑‒2013/slides
79. Cloudian Inc. & KK – Except where otherwise noted, content on this document is licensed
under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
クリエイティブコモンズ
表⽰示 -‐‑‒ ⾮非営利利 3.0 ⾮非移植 ライセンス
CC Attribution-‐‑‒NonCommercial 3.0 Unported License
79
• この⽂文書は クリエイティブ・コモンズ 表⽰示-‐‑‒⾮非営利利 3.0 ⾮非移植 (CC BY-‐‑‒NC 3.0) ライセンス の下に提供
されています。使⽤用許諾諾条件を⾒見見るには、http://creativecommons.org/licenses/by-‐‑‒nc/3.0/ をチェッ
クしてください。
• あなたは以下の条件に従う場合に限り、⾃自由に
• 本作品を複製、頒布、展⽰示、実演することができます。
• ⼆二次的著作物を作成することができます。
• あなたの従うべき条件は以下の通りです。
• 表⽰示 ̶— あなたは原著作者のクレジットを表⽰示しなければなりません。
• ⾮非営利利 ̶— あなたはこの作品を営利利⽬目的で利利⽤用してはなりません。