More Related Content
Similar to BMXUG つきじ#4 (20)
BMXUG つきじ#4
- 6. IBM Cloudant
• Apache CouchDB をベースに IBM が DBaaS 化して IBM Cloud から提供し
ているデータベースサービス
• Apache CouchDB の最新バージョンは 2.1.0
• IBM Cloudant は CouchDB 2.0.0 ベース
• この後に紹介する Design Document 機能は Apache CouchDB が所有している機能
• JSON ドキュメントをスケーラブルに格納する高速データベース
• 分散データベース
• 検索、トランザクション処理は苦手
• 「とにかく大量のデータを保存する」用途向き(ログ、IoT、・・・)
• Design Document という特殊なドキュメントで、設計要素を管理する
• View / List / Show
- 7. 例: prefs データベース 例えば関東地方は 8 <= code <= 14,
海なし県は code ∈ [ 9, 10, 11, 19, 20, 21, 25, 29 ]
- 8. View Design Document
• 「特定の条件を満たす JSON ドキュメントを『ビュー』という単位でまと
めてマッピング定義」したもの
• 上記条件を管理する特殊な JSON ドキュメント = Design Document
• 1つの Design Document の中に最大3つの定義を記述できる
• View
• List(後述)
• Show(後述)
- 10. View Design Document の例(1)
• 関東地方だけを抜き出す View Design Document (kanto_view.json)
{
"language": "javascript",
"views": {
"kanto": {
"map": "function( doc ){
if( doc.code && 8 <= doc.code && doc.code <= 14 ){
emit( doc._id, { code: doc.code, pref: doc.pref, capital: doc.capital, lat: doc.lat, lng: doc.lng } );
}
}"
}
}
}
全てのドキュメントが doc としてこの関数の引数となって実行される
emit されたデータはビューに含まれる
doc.code が存在して、かつ8以上14以下だった場合に emit する
- 11. View Design Document の例(2)
• 海なし県だけを抜き出す View Design Document (nosea_view.json)
{
"language": "javascript",
"views": {
"nosea": {
"map": "function( doc ){
if( doc.code && [ 9, 10, 11, 19, 20, 21, 25, 29 ].indexOf( doc.code ) > -1 ){
emit( doc._id, { code: doc.code, pref: doc.pref, capital: doc.capital, lat: doc.lat, lng: doc.lng } );
}
}"
}
}
}
- 12. View Design Document の適用
$ curl -u 'username:password' -H 'Content-Type: application/json'
-XPUT 'https://username.cloudant.com/prefs/_design/nosea' -d @nosea_view.json
- 13. List Design Document
• View Design Document がビューのマッピング条件を記述したもので
あったのに対して、List Design Document はビューの見栄えを定義す
るもの
- 14. List Design Document の例
• 海なし県ビューの見栄えを定義する List Design Document
{
"_rev": "xxxxx…..xxxxx",
"language": "javascript",
"views": { .. },
"lists": {
"nosea": "function( head, row ){
start( { 'headers': { 'content-type': 'text/html' } } );
send( '<ul>' );
var row;
while( row = getRow() ){
var url = '../../_show/pref';
send( '<li><a href=¥"' + url + row.id + '¥"' + row.value.prefecture + '(' + row.value.capital + ')</a></li>' );
}
send( '</ul>' );
}"
}
}
<ul>
<li><a href="xx">[県名]([都市名])</a></li>
:
</ul>
ビュー表示用の HTML テンプレート
- 15. List Design Document の適用
$ curl -u 'username:password' -H 'Content-Type: application/json'
-XPUT 'https://username.cloudant.com/prefs/_design/nosea' -d @nosea_view.json
https://username.cloudant.com/prefs/_design/nosea/_list/nosea/nosea
- 16. Show Design Document
• List Design Document はビューの見栄えを定義するものであったの
に対し、Show Design Document はビューから選択した1ドキュメント
の見栄えを定義するもの
- 17. Show Design Document の例
• 海なし県の見栄えを定義する Show Design Document
{
"_rev": "yyyyy…..yyyyy",
"language": "javascript",
"views": { .. }, "lists": { .. },
"shows": {
"nosea": "(function( doc, req ){
if( doc ){
return '<h2>' + doc.prefecture + '</h2>'
+ '<h3>' + doc.capital + '</h3><hr/>'
+ '緯度: ' + doc.lat + '<br/>'
+ '経度: ' + doc.lng;
}else{
return 'empty';
}
})"
}
}
<h2>[県名]</h2>
<h3>[都市名]</h3>
<hr/>
緯度: [緯度]<br/>
経度: [経度]
- 18. Show Design Document の適用
$ curl -u 'username:password' -H 'Content-Type: application/json'
-XPUT 'https://username.cloudant.com/prefs/_design/nosea' -d @nosea_show.json
https://username.cloudant.com/prefs/_design/nosea/_list/nosea/nosea
- 19. Design Document のまとめ
• IBM Cloudant / Apache CouchDB が持つ特徴的な機能
• ビューの定義や見栄え、各文書の見栄えを定義する特殊なドキュメ
ント
• アプリケーションサーバーを使わなくても簡易テンプレートエンジンの
ように使うことができる
• データとロジック、見た目を分離する流行とは必ずしも一致しない
- 20. おまけ
• IBM Cloudant / Apache CouchDB はデータドキュメントも設計要素の
ドキュメントも、いずれもドキュメントとしてデータベース内に格納され
る
ノーツも同じ
実は CouchDB の開発者は元 IBM/Lotus エンジニア
https://www.linkedin.com/in/damienkatz/