RethinkDB

From Bauman National Library
This page was last modified on 14 June 2016, at 15:25.
</td></tr>
RethinkDB
Initial release 24 July 2009 (2009-07-24) [1]
Stable release
2.3.2 / May 2, 2016 (2016-05-02)
Repository {{#property:P1324}}
Development status Active
Written in C++, JavaScript, Bash
Operating system UNIX, Linux, OS X, BSD
Available in English
License AGPL
Website rethinkdb.com

Definition

RethinkDB is an open source, NoSQL, distributed document-oriented database[2]. It stores JSON documents with dynamic schemas, and is designed to facilitate pushing real-time updates for query results to applications.

Description

RethinkDB is a schemaless JSON document store, but it also supports relational features like table joins. RethinkDB supports clustering, which makes it easy to scale. You can configure sharding and replication for the cluster through the database's built-in administrative Web interface. The RethinkDB query language, which is called ReQL, embeds natively into the code in which you write your application.

Quickstart

RethinkDB supports the following programming languages:

Besides our four official drivers, we also have many third-party drivers supported by the RethinkDB community. Here's a few:

  • C#/.NET: RethinkDb.Driver, rethinkdb-net
  • Clojure: clj-rethinkdb
  • Elixir: rethinkdb-elixir
  • Go: GoRethink
  • Haskell: haskell-rethinkdb
  • PHP: php-rql
  • Scala: rethink-scala

Introduction to ReQL

RethinkDB databases contain tables, which store conventional JSON documents. The JSON object structures can be deeply nested. Every RethinkDB document has a primary key, an id property with a value that is unique within the document's table. You can reference the primary key in a ReQL query to efficiently fetch an individual document.

Writing ReQL queries in an application feels a lot like using a SQL query builder API. The following is a simple ReQL query, written in JavaScript, that finds the number of unique last names in the users table:

    r.table("users").pluck("last_name").distinct().count()

In a ReQL query, each function in the chain operates on the output of the previous function. It works a little bit like the pipeline paradigm from Unix shell scripting. Specifically, the functions in the above query perform the following operations:

  • table accesses a specific table in the database
  • pluck extracts a specific property (or multiple properties) from every record
  • disctinct eliminates the duplicate values, leaving one instance of each unique value
  • count - counts the number of items and returns the total

ReQL includes an insert function, which you can use to add new JSON documents to a table:

     r.table("fellowship").insert([
         { name: "User1", species: "host1" },
         { name: "User2", species: "host2" },
      ])

The filter function retrieves documents that match particular parameters:

     r.table("fellowship").filter({species: "host1"})

How ReQL works

RethinkDB client libraries are responsible for integrating ReQL into the underlying programming language. A complete client library implements functions for all of the query operations supported by the database. Under the hood, ReQL query expressions evaluate into structured objects that look a bit like an abstract syntax tree. To execute a query, the client library translates the query objects into RethinkDB's JSON wire protocol format, which it then transmits to the database server.

The ReQL run function, chained to the end of a query, translates the query, executes it on the server, and returns the output. You typically provide the run function with a reference to a RethinkDB server connection that it can use to perform the operation. In the official client libraries, connection handling is a manual process. You have to create the connection and close it when you are finished.

The following example shows how to perform a RethinkDB query in Node.js with the JavaScript client driver. The query retrieves all of the halflings from the fellowship table and displays them in the terminal:

    var r = require("rethinkdb");
    r.connect().then(function(conn) {
    return r.table("fellowship")
         .filter({species: "host1"}).run(conn)
    .finally(function() { conn.close(); });
    })
    .then(function(cursor) {
    return cursor.toArray();
    })
    .then(function(output) {
    console.log("Query output:", output);
    })

The rethinkdb module in Npm provides RethinkDB's official JavaScript client driver. The connect method establishes the connection, which the run function uses to perform the query. The query itself returns a cursor, which is sort of like an open window into the contents of the database. Cursors support lazy data fetching, offering an efficient way to iterate over a large data set. In the example above, I simply chose to convert the contents of the cursor to an array because the output is relatively small.

References

Notes

Cite error: Invalid <references> tag; parameter "group" is allowed only.

Use <references />, or <references group="..." />
  1. Jeremy Zawodny, RethinkDB: Rethinking the Database using Modern Assumptions, Linux Magazine, August 24, 2009
  2. A document-oriented database, or document store, is a computer program designed for storing, retrieving, and managing document-oriented information, also known as semi-structured data. Document-oriented databases are one of the main categories of NoSQL databases, and the popularity of the term "document-oriented database" has grown with the use of the term NoSQL itself.