RethinkDB is a new NoSQL database that was designed for realtime apps. What’s so special in it? It provides a new way of developing web applications. RethinkDB can push updated query results to the application.
This DB fits for multiplayer games, streaming analytics applications, connected devices and other cases that can benefit from realtime feeds.
You definitely should not use Rethink when you need full ACID support and when you cannot concede writing ability to data consistency.
Now RethinkDB is available for Linux and OS X and its installation is very easy (Ubuntu):
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- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add - sudo apt-get update sudo apt-get install rethinkdb
Then execute command “rethinkdb” to start the server. If everything goes OK you’ll see RethinkDB web UI by reaching http://localhost:8080
Working with RethinkDB
The RethinkDB provides ReQL-interfaces (drivers) for many programming languages. ReQL consists of sequences of language-specific commands. Let’s see how to communicate with Rethink using Python driver.
At first, install rethinkdb driver
> sudo pip install rethinkdb > python >>> import rethinkdb as r >>> r.connect( "localhost", 28015).repl()
Let’s create a database:
>>> r.db_create(“library”).run()
create a table:
>>> r.db(“library”).table_create(“books”).run()
insert a record:
>>> r.db("library").table("books").insert([{'name':'The Hunger Games', 'pages':342},'name':'1984', 'pages':240}]).run()
count records:
>>> r.db("library").table("books").count().run()
list all records:
>>> for res in r.db("library").table("books").run(): ... print res
You will see the list of objects printed out:
{u’pages’: 342, u’id’: u’a5d62a15-4504-42bf-a3b8-e4df402413bd’, u’name’: u’1984′}
{u’pages’: 342, u’id’: u’a808f0ad-1cf0-42cc-8fcf-7732eb989be8′, u’name’: u’The Hunger Games’}
We can retrieve a single document:
>>> r.db("library").table("books").get('a808f0ad-1cf0-42cc-8fcf-7732eb989be8').run()
So here we have learned some RethinkDB basics. Let’s move to advanced features.
Changefeeds
Now let’s talk about the most interesting part of RethinkDB. If you insert “changes()” command before “run()” it will create a new changefeed thread. Changefeed will notify your app about all changes of your dataset.
A closer look. Run this code in your terminal:
>>> cursor = r.table("books").changes().run() >>> for d in cursor: ... print d
Then from another terminal try to modify the dataset:
>>> r.db("library").table("books").filter(r.row["name"]== "1984").delete().run()
In the first terminal changefeed notification will be displayed:
{u’old_val’: {u’pages’: 342, u’id’: u’a5d62a15-4504-42bf-a3b8-e4df402413bd’, u’name’: u’1984′}, u’new_val’: None}
Easy clustering
RethinkDB supports clustering. To create a cluster just start another server with join parameter:
> rethinkdb --port-offset 1 --directory rethinkdb_data2 --join localhost:29015
Leave a Reply
Be the First to Comment!