rethinkDB

This post will describe installation and interaction with rethinkDB using Javascript/Node.js. Ultimately I want to hook rethinkDB to Shiny. I am working on Debian/Jessie. Currently (August, 2017) rethinkDB will not install on Stretch.

Installation

1
2
3
4
#echo "deb http://download.rethinkdb.com/apt `lsb_release -cs` main" | tee /etc/apt/sources.list.d/rethinkdb.list
#wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add -
#apt-get update
#apt-get install rethinkdb

Start the server with the command “rethinkdb” at a terminal prompt. By default administrative tools are available at localhost:8080/.

Client driver installation

cd to the working directory then install a node project

1
2
3
4
5
mbc@dc7700s: cd ./syncd/prog/shiny/music
mbc@dc7700s:~/syncd/prog/shiny/music$ npm init
mbc@dc7700s:~/syncd/prog/shiny/music$ npm install rethinkdb --save
mbc@dc7700s:~/syncd/prog/shiny/music$ node
>r = require('rethinkdb')

Set the conn variable and interact with the database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mbc@dc7700s:~/syncd/prog/shiny/music$ node
>r = require('rethinkdb')
>var connection = null;
r.connect( {host: 'localhost', port: 28015}, function(err, conn) {
if (err) throw err;
connection = conn;
})
>r.db('shiny').table('music').run(connection, function(err, cursor) {
if (err) throw err;
cursor.toArray(function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
});
});

Insert

1
2
3
4
5
6
7
8
r.db('shiny').table('music').insert([{artist:'Boston',
songs: [ {title:'song1',genre:'rock',url:'https://www.youtube.com/watch?v=SSR6ZzjDZ94'},
{title:'song2',genre:'rock',url:'https://www.youtube.com/watch?v=SSR6ZzjDZ94'}]
}]).run(connection, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
})

Update

1
2
3
4
5
6
7
8
9
r.db('shiny').table('music').
filter(r.row("artist").eq("Boston")).
update({songs:
[ {title:'song1',genre:'rock',url:'https://www.youtube.com/watch?v=SSR6ZzjDZ94'},
{title:'song2',genre:'rock',url:'https://www.youtube.com/watch?v=SSR6ZzjDZ94'},
{title:'song3',genre:'rock',url:'https://www.youtube.com/watch?v=SSR6ZzjDZ94'}]}).run(connection, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));})

Show all

1
2
3
r.db('shiny').table('music').distinct().run(connection, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));})

Delete all

1
2
3
r.db('shiny').table('music').delete().run(connection, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));})

Launch script

The ‘launch.browser’ option to runApp() deals with the random port assigned by Shiny.

music.sh
1
2
3
4
5
#!/bin/bash
rethinkdb -d /home/mbc/shiny/music/rethinkdb_data/ &
R -e "shiny::runApp(appDir='/home/mbc/shiny/music', launch.browser=TRUE)"
Share