RethinkDB 让实时交互更顺畅

 

Rethinkdb介绍

Rethinkdb官方网站点这里

RethinkDB is the first open-source, scalable JSON database built from the ground up for the realtime web. It inverts the traditional database architecture by exposing an exciting new access model – instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in realtime. RethinkDB’s realtime push architecture dramatically reduces the time and effort necessary to build scalable realtime apps.

In addition to being designed from the ground up for realtime apps, RethinkDB offers a flexible query language, intuitive operations and monitoring APIs, and is easy to setup and learn.

See the post Advancing the realtime web for more technical details on RethinkDB’s mission.

以下场景适合使用Rethinkdb

  • Collaborative web and mobile apps
  • Streaming analytics apps
  • Multiplayer games
  • Realtime marketplaces
  • Connected devices

For example, when a user changes the position of a button in a collaborative design app, the server has to notify other users that are simultaneously working on the same project. Web browsers support these use cases via WebSockets and long-lived HTTP connections, but adapting database systems to realtime needs still presents a huge engineering challenge.

举例,当用户在一款协作设计类app里移动一个按钮的位置时,服务器必须同步通知给所有在同一个项目上的其他用户。浏览器是通过websockets和长连接也实现这个功能的,但要数据库层实现仍是一个巨大的战。

RethinkDB is the first open-source, scalable database designed specifically to push data to applications in realtime. It dramatically reduces the time and effort necessary to build scalable realtime apps.

以下场景不适合使用

When is RethinkDB not a good choice?

  • RethinkDB is not a good choice if you need full ACID support or strong schema enforcement—in this case you are better off using a relational database such as MySQL or PostgreSQL.
  • If you are doing deep, computationally-intensive analytics you are better off using a system like Hadoop or a column-oriented store like Vertica.
  • In some cases RethinkDB trades off write availability in favor of data consistency. If high write availability is critical and you don’t mind dealing with conflicts you may be better off with a Dynamo-style system like Riak.

安装

各平台的安装文档。os x 可以通过下载package安装 或 brew安装

启动

直接运行命令rethinkdb,注意将会在当前目录下创建一个数据库:rethinkdb_data。

启动成功后可以通过浏览器访问管理后台 http://localhost:8080/ (注意8080端口不要占用)

使用

Rethinkdb还是比较成熟的,通过这么丰富的安装工具,使用教程就可以看出来。所以使用上也很方便,官司方默认提供了Javascript/python/java/ruby的sdk,其它语言也有第三方提供了(全部语言的sdk列表)。下面以javascript使用进行举例,要先安装node/npm,然后安装rethinkdb的npm包。

>npm install rethinkdb

>node

输入以下代码

r = require('rethinkdb')
r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
  if(err) throw err;
  r.db('test').tableCreate('tv_shows').run(conn, function(err, res) {
    if(err) throw err;
    console.log(res);
    r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, function(err, res)
    {
      if(err) throw err;
      console.log(res);
    });
  });
});

将创建表tv_shows及一条记录。通过http://localhost:8080/的Data Explorer也可以操作库表,还自带语法提示,赞一个。

特性功能介绍

  • RethinkDB使用的是自定义的sql:ReQL,详细的使用方法参见官方教程
  • RethinkDB可以与多种流行平台进行整合,如RabbitMQ,可以订阅表数据发生的变化,主动推送消息给MQ(RethinkDB supports changefeeds, which allow you to subscribe to changes on a table. The database pushes these changes to you as they happen.)
  • 同时社区还贡献了大量的扩展组件,增强了RethinkDB的功能,如连接池/ORM。
  • 实时推送变化,使得RethinkDB既不同是MongoDB,也不同于实时推送API,如 Firebase, PubNub, or Pusher等。订阅一个变化只需要
  • r.table('users').get('coffeemug').changes().run()
  • 服务器启动时默认会检查新版本,并提交一些运行数据到RethinkDB,如果不期望提交数据,可以启动时加参数 no-update-check 

参考

  1. 增强实现一个实时网站
  2. RethinkDB vs MongoDB

转载于:https://my.oschina.net/swingcoder/blog/833577

RethinkDB 设计用来存储 JSON 文档的分布式数据库,可通过简单操作实现多机分布式存储。支持表的联合和分组查询。什么是RethinkDB?RethinkDB 是从头打造的第一个开源、可扩展的JSON数据库,用于搭建实时网页。全新的访问模型颠覆了传统的数据库结构:开发者只需告诉RethinkDB实时连 续地将查询新结果推送到应用就可以了,不用每次都去poll一遍。RethinkDB实时推送结构为搭建可扩展实时应用节省了大量时间精力。除了为实时应用提供了全新的设计之外,RethinkDB 还提供了一种灵活的查询语言、直观的操作和监控API,安装学习起来也非常容易。你可以查看这篇 Advancing the realtime web 得到多RethinkDB计划的技术细节。什么时候RethinkDB是一个好的选择?当你的应用很大程度上有赖于数据的实时反馈时,RethinkDB 就会成为一个很棒的选择。“查询-响应”式的数据库访问模型在web上的确很有用,它可以直接映射到HTTP的“请求-响应”。而现代应用则需要将数据直接实时地传送到客户端。能够最大化得益于RethinkDB实时推送架构的例子包括:协作网站和移动应用数据流分析应用多人在线游戏实时交易场所设备联机举个例子:在协作设计一个app的时候,其中一个用户改变了某个按钮的位置,服务器就必须在第一时间通知所有在完成同一项目的其他用户。网页浏览器 能够通过WebSockets和http持久连接来支持这一功能,但数据库系统要迎合实时需求仍然是一个大的工程难题。而RethinkDB作为第一个开 源、可扩展的数据库,就是特别为实时推送数据到应用而设计的。哪些人在用 RethinkDB?RethinkDB 的用户包括上百个科技创业公司、咨询工作室和世界五百强企业。这里是其中的一些:Jive Software 和 Mediafly 使用RethinkDB搭建强大的响应式网页和移动应用Pristine.io 和 Narrative Clip 使用RethinkDB搭建用于设备连接的云架构Platzi 和 Workshape.io 使用RethinkDB进行实时分析CMUNE 和 NodeCraft 使用RethinkDB构建大规模可扩展多人游戏RethinkDB 拥有超过十万开发者的活跃社区和上百个来自世界各地的代码贡献者。RethinkDB是基于现有技术的吗?高效实现实时推送架构需要重新设计绝大部分的数据库成分,包括查询执行引擎、分布式系统、超高速缓存子系统和存储引擎。因为架构影响到每一个数据库 组成部分,RethinkDB不得不从C 开始一步步写起来。RethinkDB 是由数据库专家组成的团队花了五年时间做出来的,还得到了来自世界各地上百个代码贡献者的帮助。RethinkDB和realtime sync不同在哪里?和Firebase, PubNub 或者Pusher 这类实时同步API相比,RethinkDB主要不同在以下三个方面:首先,实时同步API是云服务,而RethinkDB 是开源项目。RethinkDB也有云端,可以通过我们的合作伙伴 Compose.io 和 Amazon AWS获得。它还可以部署在你自己的架构中,没有任何限制。其次,同步实时API只局限于同步文档,而RethinkDB是一个有着普遍应用范围的数据库系统。 在RethinkDB中你可以运行任意query,包括table joins, subqueries, geospatial queries, aggregation, 还有map-reduce。实时同步服务有多查询功能上的限制。最后,实时同步API的设计是直接从浏览器访问。这使得基本的app能够快速地跑起来,然而一旦app扩展了,灵活性就会受到限制。 RethinkDB的设计是从应用服务器进行访问,这一点上像是传统的数据库。可能会要多花一点设置代码,但拥有足够的灵活性去适应应用的成熟。RethinkDB和MongoDB又不同在哪里?RethinkDB所基于的架构和MongoDB非常不同。开发者只需告诉RethinkDB实时连续地将查询新结果推送到应用就可以了,不用 每次都去poll一遍。你同样可以在RethinkDB上用传统的“查询-响应”范式来书写应用。然后在你开始为app添加实时功能时再去订阅实时数据 流。举个例子,这是你让RethinkDB查询一个文件时的命令:r.table('users').get('coffeemug').run()然后这是你从RethinkDB订阅新流时用到的语句,在任何时候文档发生了变化就会推送:r.table('users').get('coffeemug').changes().run()RethinkDB实时架构可以和MongoDB的oplog相提并论,但前者提供了高层次的抽象。RethinkDB的数据流与查询计算引擎无缝整合,并允许你订阅查询结果的变化,而不仅仅是把数据复制过来。这种架构大幅度地减少了搭建实时app所需的时间和精力。除了实时推送架构,RethinkDB 还有许多胜过 MongoDB的地方:一种高级的查询语言,能够支持table joins, subqueries 和大规模并行式分布计算。融合了查询语言的操作和监控API,大幅度降低了RethinkDB扩展的难度。简洁美观的UI 易于复制转发,拥有在线文档支持和查询语言建议。可以看看这篇 technical comparison of RethinkDB and MongoDB 里面的评论比较中立一些。想听听个人观点的,请看@coffeemug 的what makes RethinkDB different.什么时候RethinkDB是一个不好的选择?当你需要用到完整ACID支持或者强大的架构实施,RethinkDB就不大好用了。在这种情况下你最好用一些传统的MySQL或者PostgreSQL数据库。如果你需要做深度、密集型计算分析的话,你最好用Hadoop或者类似于Verticaa的面向列的存储工具。在某些情况下RethinkDB会在一定程度上牺牲书写可用性(write availability)来保证数据一致性(data consistency)。如果高要求的书写可用性对你来讲很重要,那你也不要纠结了,像Riak这样的Dynamo式系统可能适合你。想要多地学习RethinkDB?阅读 ten-minute guide 开始学习RethinkDB。对于熟悉分布式系统的程序员可以直接阅读 architecture overview 。走捷径用 cookbook,你可以看到许多常用的 RethinkDB查询例子。 标签:分布式数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值