推薦一款NoSQL型數據庫 rethindb

非關係型數據庫 (NoSQL)

用在實時數據分析上很不錯 (http://localhost:8080/) 可視化

適用場景(for the realtime web):

  • Web + mobile
  • apps Multiplayer games
  • Realtime marketplaces
  • Streaming analytics
  • Connected devices

支持pub- sub 功能 (publish - subscribe),發佈訂閱模式,能夠監聽數據庫變化

  1. Redis 也支持發佈訂閱,但用法不同,顯然沒這個方便
  2. changes 回調用於在數據庫變化時執行代碼,就是訂閱者(RealTime.js)
  3. 修改數據庫的代碼就是發佈者,因此叫發佈訂閱模式,發佈和訂閱是兩個東西

相關命令行:

rethinkdb

rethinkdb --config-file /etc/rethinkdb/default.conf

r.db("test").table("tv_shows")

相關demo

Ten-minute guide with RethinkDB and JavaScript:

r = require('rethinkdb')

//Open a connection
var connection = null;
r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
    if (err) throw err;
    connection = conn;

    /*
     *例子1: Create a new table
     */

    r.db('test').tableCreate('authors').run(conn, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2));
    })


    /*
     *例子2: Insert data
     */

    r.table('authors').insert([{
            name: "William Adama",
            tv_show: "Battlestar Galactica",
            posts: [
                { title: "Decommissioning speech", content: "The Cylon War is long over..." },
                { title: "We are at war", content: "Moments ago, this ship received word..." },
                { title: "The new Earth", content: "The discoveries of the past few days..." }
            ]
        },
        {
            name: "Laura Roslin",
            tv_show: "Battlestar Galactica",
            posts: [
                { title: "The oath of office", content: "I, Laura Roslin, ..." },
                { title: "They look like us", content: "The Cylons have the ability..." }
            ]
        },
        {
            name: "Jean-Luc Picard",
            tv_show: "Star Trek TNG",
            posts: [
                { title: "Civil rights", content: "There are some words I've known since..." }
            ]
        }
    ]).run(connection, function(err, result) {
        if (err) throw err;
        console.log("Insert data", JSON.stringify(result, null, 2));
    })


    /*
     *例子3:  Retrieve documents (檢索文檔)
     */

    r.table('authors').run(connection, function(err, cursor) {
        if (err) throw err;
        cursor.toArray(function(err, result) {
            if (err) throw err;
            console.log("例子3檢索文檔:", JSON.stringify(result, null, 2));
        });
    });


    /*
     *例子3.1:  Filter documents based on a condition (根據條件過濾文檔)
     *Filter1: 讓咱們嘗試檢索名稱屬性設置爲William Adama的文檔。
     */

    r.table('authors').filter(r.row('name').eq("William Adama")).
    run(connection, function(err, cursor) {
        if (err) throw err;
        cursor.toArray(function(err, result) {
            if (err) throw err;
            console.log(JSON.stringify(result, null, 2));


        });
    });

    /*
     *例子3.2: Filter2: 讓咱們再次使用filter來檢索全部超過兩篇文章的做者
     *This predicate contains two commands we haven’t seen before:
     *The count command returns the size of the array
     *The gt command returns true if a value is greater than the specified value (in this case, if the number of posts is greater than two).
     */

    r.table('authors').filter(r.row('posts').count().gt(2)).
    run(connection, function(err, cursor) {
        if (err) throw err;
        cursor.toArray(function(err, result) {
            if (err) throw err;
            console.log(JSON.stringify(result, null, 2));
        });
    });


    /*
     *例子3.3: Retrieve documents by primary key
     *We can also efficiently retrieve (有效地檢索)documents by their primary key using the get command. We can use one of the ids generated in the previous example:
     *因爲 primary keys是獨一無二的,經過這種方式,咱們能夠直接檢索文檔,而無需將光標轉換爲數組。
     */

    r.table('authors').get('b7f95e64-652e-40ed-9ad8-6113e09d7771').
    run(connection, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2)); //Laura Roslin

    });


    /*
     *例子4:  Realtime feeds (實時提要)
     *RethinkDB經過公開一個使人興奮的新訪問模型來顛覆傳統的數據庫體系結構——開發人員能夠告訴RethinkDB實時地將更新的查詢結果持續推送到應用程序中,而不是輪詢更改
     *要啓動提要,請打開一個新終端並打開一個新的RethinkDB鏈接。而後,運行如下查詢(運行 Realtime.js)
     * See the changefeeds documentation entry for more details on how to use realtime feeds in RethinkDB
     */


    /*
     *例子5: Update documents
     *同時結合例子4看 change結果 ()
     */

    /*
     *例子5.1: 表增長type字段
     *Let’s update all documents in the authors table and add a type field to note that every author 
     */

    r.table('authors').update({ type: "fictional" }).
    run(connection, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2));
    });

    /*
     *例子5.2: 對知足條件的記錄進行數據更新,增長rank字段
     *Let’s update William Adama’s record to note that he has the rank of Admira
     *Realtime.js運行着 能夠watch 到數據變化
     */

    r.table('authors').
    filter(r.row("name").eq("William Adama")).
    update({ rank: "Admiral" }).
    run(connection, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2));
    });

    /*
     *例子5.3: 針對 name是 Jean-Luc Picard 這條記錄,we’d like to add to his posts
     *The update command allows changing existing fields in the document, as well as values inside of arrays
     *Realtime.js運行着 能夠watch 到數據變化
     */

    r.table('authors').filter(r.row("name").eq("Jean-Luc Picard")).
    update({
        posts: r.row("posts").append({
            title: "Shakespeare",
            content: "What a piece of work is man..."
        })
    }).run(connection, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2));
    });

	/*
     *例子6:Delete documents
     *咱們想精簡咱們的數據庫,刪除全部少於三篇文章的文檔
     */

    r.table('authors').
    filter(r.row('posts').count().lt(3)).
    delete().
    run(connection, function(err, result) {
        if (err) throw err;
        console.log(JSON.stringify(result, null, 2));
    });

})

Realtime feeds: ( Realtime.js)

/*
 *例子4:  Realtime feeds (實時提要)
 *Now switch back to your first terminal.
 *We’ll be updating and deleting some documents in the next two sections. As we run these commands, the feed will push notifications to your program. The code above will print the following messages in the second terminal
 */
r = require('rethinkdb')

r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {

    r.table('authors').changes().run(conn, function(err, cursor) {
        if (err) throw err;
        cursor.each(function(err, row) {
            if (err) throw err;
            console.log(JSON.stringify(row, null, 2));

            //監聽到數據庫表 變化(增長了type字段)

            // {
            //     "new_val": {
            //       "id": "1d854219-85c6-4e6c-8259-dbda0ab386d4",
            //       "name": "Laura Roslin",
            //       "posts": [...],
            //       "tv_show": "Battlestar Galactica",
            //       "type": "fictional"
            //     },
            //     "old_val": {
            //       "id": "1d854219-85c6-4e6c-8259-dbda0ab386d4",
            //       "name": "Laura Roslin",
            //       "posts": [...],
            //       "tv_show": "Battlestar Galactica"
            //     }
            //   }


        });
    });
})
相關文章
相關標籤/搜索