Getting Started with RethinkDB
()
About this ebook
About This Book
- Make the most of this open source, scalable database—RethinkDB —to ease the construction of web applications
- Run powerful queries using ReQL, which is the most convenient language to manipulate JSON documents with
- Develop fully-fledged real-time web apps using Node.js and RethinkDB
Who This Book Is For
Getting Started with RethinkDB is ideal for developers who are new to RethinkDB and need a practical understanding to start working with it. No previous knowledge of database programming is required, although a basic knowledge of JavaScript or Node.js would be helpful.
What You Will Learn
- Download and install the database on your system
- Configure RethinkDB’s settings and start using the web interface
- Import data into RethinkDB
- Run queries using the ReQL language
- Create shards, replicas, and RethinkDB clusters
- Use an index to improve database performance
- Get to know all the RethinkDB deployment techniques
In Detail
RethinkDB is a high-performance document-oriented database with a unique set of features. This increasingly popular NoSQL database is used to develop real-time web applications and, together with Node.js, it can be used to easily deploy them to the cloud with very little difficulty.
Getting Started with RethinkDB is designed to get you working with RethinkDB as quickly as possible. Starting with the installation and configuration process, you will learn how to start importing data into the database and run simple queries using the intuitive ReQL query language.
After successfully running a few simple queries, you will be introduced to other topics such as clustering and sharding. You will get to know how to set up a cluster of RethinkDB nodes and spread database load across multiple machines. We will then move on to advanced queries and optimization techniques. You will discover how to work with RethinkDB from a Node.js environment and find out all about deployment techniques.
Finally, we’ll finish by working on a fully-fledged example that uses the Node.js framework and advanced features such as Changefeeds to develop a real-time web application.
Style and approach
This is a step-by-step book that provides a practical approach to RethinkDB programming, and is explained in a conversational, easy-to-follow style.
Related to Getting Started with RethinkDB
Related ebooks
Building a Web Application with PHP and MariaDB: A Reference Guide Rating: 0 out of 5 stars0 ratingsMastering RethinkDB Rating: 0 out of 5 stars0 ratingsReact Projects: Build 12 real-world applications from scratch using React, React Native, and React 360 Rating: 0 out of 5 stars0 ratingsAdvanced Express Web Application Development Rating: 0 out of 5 stars0 ratingsBuilding a RESTful Web Service with Spring Rating: 5 out of 5 stars5/5MongoDB High Availability Rating: 5 out of 5 stars5/5ReactJS by Example - Building Modern Web Applications with React Rating: 4 out of 5 stars4/5Getting Started with React Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsBootstrap for ASP.NET MVC - Second Edition Rating: 5 out of 5 stars5/5Learning Azure DocumentDB Rating: 0 out of 5 stars0 ratingsMastering Backbone.js Rating: 0 out of 5 stars0 ratingsGetting Started with hapi.js Rating: 5 out of 5 stars5/5Modern Web Development with Deno: Develop Modern JavaScript and TypeScript Code with Svelte, React, and GraphQL (English Edition) Rating: 0 out of 5 stars0 ratingsLearn Python in 7 Days Rating: 0 out of 5 stars0 ratingsGetting Started with React Native Rating: 4 out of 5 stars4/5Building Web Applications with Python and Neo4j Rating: 0 out of 5 stars0 ratingsImplementing OpenShift Rating: 0 out of 5 stars0 ratingsOptimizing Hadoop for MapReduce Rating: 0 out of 5 stars0 ratingsElasticsearch Indexing Rating: 0 out of 5 stars0 ratingsMastering Sass Rating: 0 out of 5 stars0 ratingsGetting Started with Review Board Rating: 0 out of 5 stars0 ratingsInstant Redis Optimization How-to Rating: 0 out of 5 stars0 ratingsImplementing DevOps on AWS Rating: 0 out of 5 stars0 ratingsKnockoutJS Blueprints Rating: 0 out of 5 stars0 ratingsPHP 7 Programming Blueprints Rating: 0 out of 5 stars0 ratingsReSharper Essentials Rating: 4 out of 5 stars4/5JavaScript Unlocked Rating: 5 out of 5 stars5/5
Programming For You
HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsJava for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5The Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5
Reviews for Getting Started with RethinkDB
0 ratings0 reviews
Book preview
Getting Started with RethinkDB - Tiepolo Gianluca
Table of Contents
Getting Started with RethinkDB
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Introducing RethinkDB
Rethinking the database
Changefeeds
Horizontal scalability
Powerful query language
Developer-oriented
Document-oriented
Lock-free architecture
Immediate consistency
Secondary indexes
Distributed joins
Installing RethinkDB
Installing RethinkDB on Ubuntu/Debian Linux
Installing RethinkDB on CentOS and Fedora
Installing RethinkDB on OS X
Installing RethinkDB using Homebrew
Building RethinkDB from source
Configuring RethinkDB
Running as a daemon
Creating a configuration file
Starting RethinkDB
Running a query
Summary
2. The ReQL Query Language
Documents
Document databases
JSON document format
Keys
Arrays
Embedded documents
Data modeling
Introducing ReQL
An explicit query language
Building a query
Inserting data
Batch inserts
Reading data
Filtering results
Manipulating results
Updating data
Updating existing attributes
Adding new attributes
Deleting data
Removing all documents
Deleting a table
Deleting a database
Summary
3. Clustering, Sharding, and Replication
An introduction to scaling
What kind of system is it?
Scaling reads
Scaling writes
Scaling data
Clustering RethinkDB
Creating a cluster
Adding a server to the cluster
Running queries on the cluster
Replication
Adding a secondary replica
Failover
Sharding
Sharding a table
Summary
4. Performance Tuning and Advanced Queries
Performance tuning
Increasing the cache size
Increasing concurrency
Using soft durability mode
Bulk data import
Introducing indexing
Evaluating query performance
Creating and using an index
Compound indexes
Advanced queries
Limits, skips, and sorts
The limit command
The skip command
Sorting documents
Finding a random document
Grouping
Aggregations
Average
Maximum
The pluck command
Summary
5. Programming RethinkDB in Node.js
Introducing Node.js
An increasingly popular technology
An event-driven design
Installing Node.js
Installing on Linux
Installing on Mac OS X
Running Node.js
Installing the RethinkDB module
Connecting to RethinkDB
Running a simple query
Inserting documents
Reading documents
Updating and deleting documents
Introducing Changefeeds
A simple example using Changefeeds
Summary
6. RethinkDB Administration and Deployment
RethinkDB administration tools
Backing up your data
Backing up a single table
Setting up automatic backups
Restoring your data
Securing RethinkDB
Securing the web interface
Securing the driver port
Monitoring RethinkDB
Monitoring issues
Monitoring running jobs
Deploying RethinkDB
Summary
7. Developing Real-Time Web Applications
Introducing real-time web applications
Examples of real-time web apps
Going real time on the Web
Polling
AJAX
WebSockets
Developing web applications with Node.js
Express.js
Routing
Templating
Socket.io
Using RethinkDB in Node.js web applications
Database polling
Message queues
Changefeeds
Your first real-time web application
Structuring the Notes web application
Creating the Node.js server
Configuring the router
Writing the application logic
Interacting with RethinkDB
Implementing Changefeeds
Programming Socket.io
Programming the frontend
The view
Running our web application
Summary
Index
Getting Started with RethinkDB
Getting Started with RethinkDB
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2016
Production reference: 1100316
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-760-4
www.packtpub.com
Credits
Author
Gianluca Tiepolo
Reviewer
Brandon Martin
Acquisition Editor
Rahul Nair
Content Development Editor
Arshiya Ayaz Umer
Technical Editor
Rupali R. Shrawane
Copy Editor
Yesha Gangani
Project Coordinator
Kinjal Bari
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Graphics
Jason Monteiro
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
About the Author
Gianluca Tiepolo is an accomplished software engineering professional and entrepreneur with several years of experience in developing software and products on a variety of technologies, from consumer applications to revolutionary projects focused on computer vision, data engineering, and database programming. His software stack ranges from traditional platforms, such as Hadoop and OpenCV, to modern platforms, such as Node.js and Redis.
He is the founder of Defrogs, a start-up that is building a new-generation data engineering platform to handle big data called TrisDB. Bringing in innovative data process development approaches, this organization focuses on cutting-edge technologies designed to scale small to large distributed data clusters. To date, TrisDB is used by more than 3 million developers around the world. He previously co-founded Sixth Sense Solutions, a start-up that develops interactive solutions for the retail and fashion industries. In 2013, he helped produce the largest touch-enabled surface in the world. Currently, he's working on a fashion platform called Stylobag and maintains several open source projects on his GitHub account.
In 2015, he reviewed the book called Building Web Applications with Python and Neo4j published by Packt Publishing.
Acknowledgement
Writing a book is both an uphill and demanding task that cannot be accomplished by a single person without the support of several others, and this book is no different. I would like to thank everyone who has played a role in helping me write this book or helped me in my career. I am indebted and grateful to everyone; however, I would like to mention a few people who have been extremely important to me over these last few months.
First of all, I have to thank the team of editors, reviewers, and the entire team at Packt Publishing for this book. I especially want to thank Izzat Contractor, who initially suggested me as the author of this book, Rahul Nair, the acquisition editor for this project, and also Arshiya Ayaz, my content editor.
I also want to thank all my friends and colleagues who have unconditionally supported me throughout the writing of this book and have given me great inspiration. I especially want to thank Diego Frasson for everything that he has taught me and Marco Ippolito who always reminded me that impossible projects become possible if you believe in them. You guys have given me a great dose of motivation, and this book simply wouldn't have been possible without you!
I want to thank the creators of RethinkDB. I am extremely thankful to the RethinkDB team and its great community. I also have to thank the entire open source community that contributed to the technologies on which RethinkDB is based. Without these technologies, the database and, in turn, this book would not have been possible.
Last but not least, I want to thank my mum Claire for understanding me and supporting me during my long hours of work and writing.
About the Reviewer
Brandon Martin is a full-stack experienced programmer who has been programming for 10 years. Currently, he works for Lumi Inc (www.lumi.com) as the lead engineer where they are using RethinkDB in production. He has been happily married for 15 years and has two children.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Preface
Databases are all around us. In the modern web, almost every website that we visit and the web-based applications that we use have a database system working behind the frontend. Web developers are constantly looking for new database solutions that adapt to the modern web, allowing them to store data in a simpler manner.
RethinkDB is both the simplest and the most powerful document database technology available across Linux and OS X platforms. Based on robust and feature-rich software, RethinkDB provides a bunch of features that can be used to develop some real-time web applications that can be scaled incredibly easily. RethinkDB is also open source, so the source code is freely downloadable from the database GitHub repository.
This book provides an introduction to RethinkDB. The following chapters will give you some understanding and coding tips to install and configure the database and start developing web applications with RethinkDB in no time.
What this book covers
Chapter 1, Introducing RethinkDB, explains how to download and install RethinkDB on both Linux and OS X.
Chapter 2, The ReQL Query Language, explains the basics of RethinkDB's query language and how to use it to run simple queries on the database.
Chapter 3, Clustering, Sharding, and Replication, explores the different techniques you can use to scale RethinkDB.
Chapter 4, Performance Tuning and Advanced Queries, gives out best practices to obtain optimal performance and explores more advanced queries.
Chapter 5, Programming RethinkDB in Node.js, explains how to interact with the database using the Node.js programming language.
Chapter 6, RethinkDB Administration and Deployment, teaches you how to maintain your RethinkDB database instance and how to deploy it to the cloud.
Chapter 7, Developing Real-Time Web Applications, explores how to develop a full-fledged Node.js web application based on RethinkDB.
What you need for this book
To get the most out of this book, you'll need a computer or server running OS X or any other Linux distribution.
You will also need an Internet connection and administration privileges to download and install the database.
Finally, you will need a text editor to edit configuration files and write code. There are many freely available editors, such as Nano, Emacs, or Gedit. Choose the one you prefer!
Who this book is for
This book is targeted at anyone interested in learning how to get started with the RethinkDB database. No prior database programming experience is required, however you should be comfortable with installing software, editing configuration files, and using the terminal or commandline. A basic knowledge of Node.js is recommended, but not required.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: The default RethinkDB package includes various control scripts including the init script /etc/init.d/rethinkdb.
A block of code is set as follows:
r.db(test
).table(people
).indexStatus(name_and_age
)
{
function
:
geo
: false ,
index
: name_and_age
,
multi
: false ,
outdated
: false ,
ready
: true
}
Any command-line input or output is written as follows:
rethinkdb import -f data.json --table test.people sudo pip install rethinkdb
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: You can do this from the Data Explorer section of the web interface by clicking on the Options icon and checking the query profiler checkbox, as you can see from the following screenshot.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either