RethinkDB 基礎架設
RethinkDB 是一個開源的即時(Realtime)且可擴展(Scalable)的資料庫,資料以 JSON 格式儲存。
曾被網友譽為
MongoDB done things right.
但終究無法打入市場,公司倒閉。最後由 CNCF(Cloud Native Computing Foundation)以 2.5 萬美元買下並貢獻給 Linux基金會。
RethinkDB 提供了一個方便的的 Web UI 供開發人員管理測試伺服器。
作為可開發即時應用程序的資料庫,RethinkDB 提供了高階抽象化的查詢語法:
- RethinkDB 一般查詢的語法:
r.table(‘users’).get(‘coffeemug’).run()
- RethinkDB 主動推送更新查詢的語法:
r.table(‘users’).get(‘coffeemug’).changes().run()
RethinkDB 與 MongoDB
MongoDB 作為同是文件導向的資料庫(Document-oriented database),其中一個主要的差別是它們的語法,MongoDB 使用嵌套物件(Nested Objects),以函數名稱作為鍵(Key),參數作為值(Value)。而 RethinkDB 使用鏈式函數(Chained Function),容易造成程式碼上的視覺混淆。
下列是對於 RethinkDB 與 MongoDB 之間更詳細的技術比較:
https://www.rethinkdb.com/docs/comparison-tables/
https://www.juristat.com/blog/a-comparison-of-mongodb-and-rethinkdb-with-patent-data
Ubuntu 上安裝 RethinkDB
取得資源庫
$ source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list$ wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -$ sudo apt-get update$ sudo apt-get install rethinkdb
編譯原始碼
安裝相關套件
$ sudo apt-get install build-essential protobuf-compiler python \
libprotobuf-dev libcurl4-openssl-dev \
libboost-all-dev libncurses5-dev \
libjemalloc-dev wget m4
取得原始碼
$ wget https://download.rethinkdb.com/dist/rethinkdb-2.3.6.tgz$ tar xf rethinkdb-2.3.6.tgz
建置伺服器
$ cd rethinkdb-2.3.6$ ./configure --allow-fetch$ make## 如果你的機器是四核心,可以使用後述指令加速 make -j 4$ sudo make install
啟動伺服器
要注意 Web UI 會開在 8080 port,不要和其他服務相衝!
$ rethinkdbinfo: Creating directory 'rethinkdb_data'info: Listening for intracluster connections on port 29015info: Listening for client driver connections on port 28015info: Listening for administrative HTTP connections on port 8080info: Server ready
安裝客戶端驅動
RethinkDB 提供多種程式語言作為驅動,其中官方提供 JavaScript、Python、Ruby 及 Java:https://www.rethinkdb.com/docs/install-drivers/
下述只說明 Python 驅動:
From version 1.14, the Python driver for RethinkDB supports Python 2 and 3. Version 1.13 and older support Python 2 only.
$ sudo pip install rethinkdb
用法
$ pythonimport rethinkdb as rr.connect('localhost', 28015).repl()r.db('test').table_create('tv_shows').run()r.table('tv_shows').insert({ 'name': 'Star Trek TNG' }).run()