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


        });
    });
})

转载于:https://my.oschina.net/u/3734107/blog/1934584

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值