Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Mastering RethinkDB
Mastering RethinkDB

Mastering RethinkDB: Master the skills of building real-time apps dramatically easier with open source, scalable database - RethinkDB

eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Mastering RethinkDB

Chapter 2. RethinkDB Query Language

ReQL means RethinkDB query language. It offers a powerful and easy way to perform operations on JSON documents. It is one of the most important parts of the RethinkDB architecture. It is built on three important principles: embedding ReQL in a programming language, ReQL queries being chainable, and ReQL queries being executed on the server.

Here is a list of topics we are going to cover, along with the mentioned principles:

  • Performing conditional queries
  • ReQL queries are chainable
  • ReQL queries are executed on a server
  • Traversing over nested fields
  • Performing string operations
  • Performing MapReduce operations
  • Calling HTTP APIs using ReQL
  • Handling binary objects
  • Performing JOINS
  • Accessing changefeed (real-time feed) in RethinkDB
  • Performing geolocation operations
  • Performing administrative operations

Let us look over each one of them.

Embedding ReQL in a programming language

RethinkDB provides client drivers for various programming languages. To explain, I am going to consider Node.js, and the steps are as follows:

  1. You can start the ReQL exploration journey by connecting to the database.
  2. Install the RethinkDB client module and make sure you have the RethinkDB server ready and running, listening to the default port.
  3. Make sure you have done npm install rethinkdb before running the following code:
          var rethinkdb = require('rethinkdb'); 
          var connection = null; 
          rethinkdb.connect({host : 'localhost', port :
                             28015},function(err,conn) { 
          if(err) {  
          throw new Error('Connection error');  
           } else { 
          connection = conn; 
           } 
           }); 
    

The preceding simple code snippet written in Node.js is importing the rethinkdb module and connecting to the RethinkDB server on the default port. It returns the callback function with error and the...

ReQL queries are chainable

Almost all ReQL queries are chainable. You can chain ReQL queries using the dot operator, just like you do with pipe in Unix. Data flows from left to right and data from one command is passed to the next one until the query gets executed. You can chain queries until your query is done.

Just like we performed some queries on the previous section, we chained the get() function with update() or delete() to perform the query.

Here is an example:

rethinkdb.table('users').delete(); 
rethinkdb.table('users').get('<<id>>').update({id : 10}); 
rethinkdb.db('test').table('users').distinct().count(); 

This way of design provides a natural way of reading and understanding queries. It's easy to learn, modify, and read.

ReQL queries are executed on a server

Queries are formed in the client but will be sent to server for execution when you run them. This makes sure there is no network round trip and bandwidth allocation. This provides efficiency in query execution.

We also mentioned in Chapter 1, The RethinkDB Architecture and Data Model, that RethinkDB executes queries in a lazy manner. It only fetches the data asked and required for the query to complete. Here is an example:

r.db('test').table('users').limit(5) 

To perform this query, RethinkDB will look for only the five documents only in the users table. It will perform enough operations to perform the data collection requested in the query. This avoids extra computation costs and CPU cycles.

To provide the highest level of efficiency, RethinkDB automatically parallelizes the query as much as possible across the server, CPU cores, or even data centers. RethinkDB automatically processes the complex queries into stages, parallelizes them...

Performing conditional queries

ReQL supports conditional queries using subqueries, expressions, and the lambda function. In this section, we will look at each one of them using sample code written in Node.js.

In order to perform these queries, I have populated our users table in the test database with some documents. Here is the query executed from the RethinkDB web administrative screen:

r.db('test').table('users').insert([{ 
name : "John", 
age : 24 
}, { 
name : "Mary", 
age : 32 
},{ 
name : "Michael", 
age : 28 
}]) 

Note

In the web administrative screen, you do not need to provide the run function with a connection; it automatically appends and executes the query on the server.

Let us run a query to find out documents with an age greater than 30 years. We are going to execute the following code after getting a connection to the database, the same as we did in the former section:

rethinkdb.table('users').filter(function (user...

Embedding ReQL in a programming language


RethinkDB provides client drivers for various programming languages. To explain, I am going to consider Node.js, and the steps are as follows:

  1. You can start the ReQL exploration journey by connecting to the database.

  2. Install the RethinkDB client module and make sure you have the RethinkDB server ready and running, listening to the default port.

  3. Make sure you have done npm install rethinkdb before running the following code:

          var rethinkdb = require('rethinkdb'); 
          var connection = null; 
          rethinkdb.connect({host : 'localhost', port :
                             28015},function(err,conn) { 
          if(err) {  
          throw new Error('Connection error');  
           } else { 
          connection = conn; 
           } 
           }); 
    

The preceding simple code snippet written in Node.js is importing the rethinkdb module and connecting to the RethinkDB server on the default port. It returns the callback function...

ReQL queries are chainable


Almost all ReQL queries are chainable. You can chain ReQL queries using the dot operator, just like you do with pipe in Unix. Data flows from left to right and data from one command is passed to the next one until the query gets executed. You can chain queries until your query is done.

Just like we performed some queries on the previous section, we chained the get() function with update() or delete() to perform the query.

Here is an example:

rethinkdb.table('users').delete(); 
rethinkdb.table('users').get('<<id>>').update({id : 10}); 
rethinkdb.db('test').table('users').distinct().count(); 

This way of design provides a natural way of reading and understanding queries. It's easy to learn, modify, and read.

ReQL queries are executed on a server


Queries are formed in the client but will be sent to server for execution when you run them. This makes sure there is no network round trip and bandwidth allocation. This provides efficiency in query execution.

We also mentioned in Chapter 1, The RethinkDB Architecture and Data Model, that RethinkDB executes queries in a lazy manner. It only fetches the data asked and required for the query to complete. Here is an example:

r.db('test').table('users').limit(5) 

To perform this query, RethinkDB will look for only the five documents only in the users table. It will perform enough operations to perform the data collection requested in the query. This avoids extra computation costs and CPU cycles.

To provide the highest level of efficiency, RethinkDB automatically parallelizes the query as much as possible across the server, CPU cores, or even data centers. RethinkDB automatically processes the complex queries into stages, parallelizes them across clusters...

Performing conditional queries


ReQL supports conditional queries using subqueries, expressions, and the lambda function. In this section, we will look at each one of them using sample code written in Node.js.

In order to perform these queries, I have populated our users table in the test database with some documents. Here is the query executed from the RethinkDB web administrative screen:

r.db('test').table('users').insert([{ 
name : "John", 
age : 24 
}, { 
name : "Mary", 
age : 32 
},{ 
name : "Michael", 
age : 28 
}]) 

Note

In the web administrative screen, you do not need to provide the run function with a connection; it automatically appends and executes the query on the server.

Let us run a query to find out documents with an age greater than 30 years. We are going to execute the following code after getting a connection to the database, the same as we did in the former section:

rethinkdb.table('users').filter(function (user) {  
return...

Performing string operations


ReQL provides the following functions to manipulate and search strings:

  • Match() takes a string or a regular expression as an input and performs a search over the field. If it matches, it returns the data in the cursor, which we can loop over to retrieve the actual data.

  • For example, we have to find all the users whose name starts with J. Here is the query for the same:

      rethinkdb.table("users").filter(function(user) { 
      return user("name").match("^J"); 
      }).run(connection,function(err,cursor) { 
      if(err) { 
      throw new Error(err); 
        } 
       cursor.toArray(function(err,data) { 
       console.log(data); 
        }); 
      }); 
  • Here we are first performing a filter, and inside it, we put our match() condition. The filter gives every document to the match() function and it appends it to the cursor. Upon running, you should be able to view the users with names starting with J.

  • split...

Performing MapReduce operations


MapReduce is the programming model to perform operations (mainly aggregation) on distributed sets of data across various clusters in different servers. This concept was coined by Google and was used in the Google file system initially and later was adopted by the open source Hadoop project.

MapReduce works by processing the data on each server and then combine it together to form a result set. It actually divides into two operations namely Map and Reduce.

  • Map: This performs the transformation of the elements in the group or individual sequence

  • Reduce: This performs the aggregation and combines the results from Map into a meaningful result set

In RethinkDB, MapReduce queries operate in three steps as follows:

  • Group operation: To process the data into groups. This step is optional

  • Map operation: To transform the data or group of data into a sequence

  • Reduce operation: To aggregate the sequence data to form a resultset

So mainly it is a Group MapReduce (GMR) operation...

Calling HTTP APIs using ReQL


RethinkDB provides support to call an external API that returns data in a JSON object, which most of the large API provider do. You can call HTTP API directly from your database hence no need of writing piece of code to just call an API and then dump into database. RethinkDB also handles it asynchronously so performance won't be affected if the API takes a longer time.

Let us try one basic API call before moving ahead with storing those in our table. We all know and use OMDb for movies review. There is a website called http://omdbapi.com/ that provides APIs to find out the movie information present in the OMDB database. Let's call one with the following code to fetch information about the Avengers movie and see how it goes:

rethinkdb.http("http://www.omdbapi.com/?t=avengers&y=2015&plot=short&r=json").run(connection,function(err,data) { 
if(err) { 
throw new Error(err); 
  } 
console.log(data); 
}); 

You should be receiving...

Handling binary objects


As we have mentioned in this chapter about RethinkDB binary object support, let's look over how to use it using ReQL. The syntax to store binary objects differs from client to client. In Node.js it uses buffers to convert the stream into binary and we can use RethinkDB to insert that in a table.

Let us take an example from the preceding document. There is a key called Poster, which is the official poster of the movie in a JPEG image format. We can store the image directly in RethinkDB in a binary format.

Consider the following code:

rethinkdb.http("http://www.omdbapi.com/?t=avengers&y=2015&plot=short&r=json").run(connection,function(err,data) { 
if(err) { 
throw new Error(err); 
  } 
rethinkdb.table("movies").insert({ 
movieName :data.Title, 
posterImage :rethinkdb.http(data.Poster, {resultFormat : 'binary'}) 
  }).run(connection,function(err,data) { 
if(err) { 
throw new Error(err); 
    } 
console.log...

Performing JOINS


JOINS are one of the features of NoSQL databases. RethinkDB provides the ReQL functions to perform various types of JOINS, such as inner, outer, and so on. Please refer to Chapter 1, The RethinkDB Architecture and Data Model, to study this more in detail.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Master the powerful ReQL queries to manipulate your JSON data,
  • Learn how to develop scalable, real-time web applications using RethinkDB and Node.js and deploy them for production,
  • A detailed, step-by-step guide to help you master the concepts of RethinkDB programming with ease

Description

RethinkDB has a lot of cool things to be excited about: ReQL (its readable,highly-functional syntax), cluster management, primitives for 21st century applications, and change-feeds. This book starts with a brief overview of the RethinkDB architecture and data modeling, and coverage of the advanced ReQL queries to work with JSON documents. Then, you will quickly jump to implementing these concepts in real-world scenarios, by building real-time applications on polling, data synchronization, share market, and the geospatial domain using RethinkDB and Node.js. You will also see how to tweak RethinkDB's capabilities to ensure faster data processing by exploring the sharding and replication techniques in depth. Then, we will take you through the more advanced administration tasks as well as show you the various deployment techniques using PaaS, Docker, and Compose. By the time you have finished reading this book, you would have taken your knowledge of RethinkDB to the next level, and will be able to use the concepts in RethinkDB to develop efficient, real-time applications with ease.

Who is this book for?

This book caters to all the real-time application developers looking forward to master their skills using RethinkDB. A basic understanding of RethinkDB and Node.js is essential to get the most out of this book.

What you will learn

  • Master the web-based management console for data-center configuration (sharding, replication, and more), database monitoring, and testing queries.
  • Run queries using the ReQL language
  • Perform Geospatial queries (such as finding all the documents with locations within 5km of a given point).
  • Deal with time series data, especially across various times zones.
  • Extending the functionality of RethinkDB and integrate it with third party libraries such as ElasticSearch to enhance our search

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 16, 2016
Length: 330 pages
Edition : 1st
Language : English
ISBN-13 : 9781786461070
Category :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 16, 2016
Length: 330 pages
Edition : 1st
Language : English
ISBN-13 : 9781786461070
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 136.97
Getting Started with RethinkDB
$38.99
Mastering RethinkDB
$48.99
Apache Spark for Data Science Cookbook
$48.99
Total $ 136.97 Stars icon
Banner background image

Table of Contents

10 Chapters
1. The RethinkDB Architecture and Data Model Chevron down icon Chevron up icon
2. RethinkDB Query Language Chevron down icon Chevron up icon
3. Data Exploration Using RethinkDB Chevron down icon Chevron up icon
4. Performance Tuning in RethinkDB Chevron down icon Chevron up icon
5. Administration and Troubleshooting Tasks in RethinkDB Chevron down icon Chevron up icon
6. RethinkDB Deployment Chevron down icon Chevron up icon
7. Extending RethinkDB Chevron down icon Chevron up icon
8. Full Stack Development with RethinkDB Chevron down icon Chevron up icon
9. Polyglot Persistence Using RethinkDB Chevron down icon Chevron up icon
10. Using RethinkDB and Horizon Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
(1 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 100%
Rudi Mar 02, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Very bad book. Seems like the author has no clue what he is writing about. Code examples very bad not readable and full of errors. Code will never run when you type it. This book is not worth the money.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.