推荐一款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"
            //     }
            //   }


        });
    });
})
相关文章
相关标签/搜索