はじめに
この記事は分散DBであるnomsのはじめ方の日本語記事です
インストール
Goをインストール
この辺を見てGoをインストール。
僕の場合GOPATHは.bashrc
に以下を設定した。
export GOPATH=$HOME/go_work
export PATH=$PATH:$GOPATH/bin
nomsをインストール
go install
でインストールします。
git clone https://github.com/attic-labs/noms $GOPATH/src/github.com/attic-labs/noms
go install github.com/attic-labs/noms/cmd/noms
動作確認
デモ用のDBに接続します。
noms log http://demo.noms.io/cli-tour::sf-film-locations
ローカルでサーバを立ち上げる
noms serve ldb:/tmp/uhuru-ds
JSから接続する
Node.JSは6.xがいいです。
mkdir norm_js_example
cd norm_js_example
echo '{"name":"norm_js_example","version":"0.0.1"}' > package.json
npm install @attic/noms -S
noms serverに接続するためのmain.js
を用意します。
"use strict";
const noms = require('@attic/noms');
const db = noms.DatabaseSpec.parse('http://localhost:8000').database();
let ds = new noms.Dataset(db, 'sensor');
// prints: null
ds.head().then(console.log);
setInterval(function() {
let data = noms.newStruct('', {
temp: getSensorValue()
});
ds.commit(data).
then(r => ds = r);
}, 1000);
function getSensorValue() {
return Math.floor(Math.random() * 100);
}
作成したスクリプトを実行します。
noms-js-example$ node main.js
noms-js-example$ noms ds http://localhost:8000
sensor
syuhei@P280-MAC:noms-tour$ noms show http://localhost:8000::sensor
struct Commit {
meta: struct {},
parents: Set<Ref<Cycle<0>>>,
value: struct {
temp: Number,
},
}({
meta: {},
parents: {
vm27jrrllt0ue2n74u00dcddatur97g8,
},
value: {
temp: 47,
},
})
Histotyを確認する。
noms-js-example$ noms log http://localhost:8000::sensor
commit u1p29fssi2dt7rjni7a4cafln6ii4upa
Parent: vm27jrrllt0ue2n74u00dcddatur97g8
(root) {
- temp: 83
+ temp: 47
}
commit vm27jrrllt0ue2n74u00dcddatur97g8
Parent: d932uavaltd9aq2t6iv4kl4a5dveprtc
(root) {
+ temp: 83
- temp: 200
+ temp: 83
}
commit d932uavaltd9aq2t6iv4kl4a5dveprtc
Parent: 0bsvt46et6vqkks30rlr7bemtab63fhh
(root) {
- temp: 100
+ temp: 200
}