RethinkDB - goddes4/python-study-wiki GitHub Wiki
When is RethinkDB a good choice?
RethinkDB is a great choice when your applications could benefit from realtime feeds to your data.
- Collaborative web and mobile apps
- Streaming analytics apps
- Multiplayer games
- Realtime marketplaces
- Connected devices
When is RethinkDB not a good choice?
- RethinkDB is not a good choice if you need full ACID support or strong schema enforcementโin this case you are better off using a relational database such as MySQL or PostgreSQL.
- If you are doing deep, computationally-intensive analytics you are better off using a system like Hadoop or a column-oriented store like Vertica.
- In some cases RethinkDB trades off write availability in favor of data consistency. If high write availability is critical and you donโt mind dealing with conflicts you may be better off with a Dynamo-style system like Riak.
Install and Run
Install the server (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
Install the python driver
pip install rethinkdb
Start the server
$ rethinkdb
info: Creating directory /home/user/rethinkdb_data
info: Listening for intracluster connections on port 29015
info: Listening for client driver connections on port 28015
info: Listening for administrative HTTP connections on port 8080
info: Server ready
Multiple RethinkDB instances on a single machine
ํด๋ฌ์คํฐ ๊ตฌ์ฑ์ด ์ด๋ฃจ์ด์ง๋ฉด,
- ๋ธ๋ผ์ฐ์ ๋ฅผ ์ด์ฉํ์ฌ localhost:8080 ๋๋ localhost:8081 ์ ์ ์ํด ์๋ฒ ํํฉ์ ๋ณผ ์ ์๋ค.
- ํด๋ผ์ด์ธํธ๋ localhost:28015 ๋๋ localhost:28016 ์ ์ฐ๊ฒฐํ์ฌ ๋ช ๋ น์ ์คํํ ์ ์๋ค.
$ rethinkdb --port-offset 1 --directory rethinkdb_data2 --join localhost:29015
info: Creating directory /home/user/rethinkdb_data2
info: Listening for intracluster connections on port 29016
info: Attempting connection to 1 peer...
info: Connected to server "Chaosknight" e6bfec5c-861e-4a8c-8eed-604cc124b714
info: Listening for client driver connections on port 28016
info: Listening for administrative HTTP connections on port 8081
info: Server ready
A RethinkDB cluster using multiple machines
first machine
$ rethinkdb --bind all
second machine
$ rethinkdb --join IP_OF_FIRST_MACHINE:29015 --bind all
์ฃผ์ ์ต์
- --bind all : ์๋ฒ๊ฐ ๋ฐ์ธ๋ฉํ ์์ดํผ ์ฃผ์
- --http-port 8081 : ์น ๊ด๋ฆฌ์ ํ์ด์ง ํฌํธ
- --port-offset 1 : ๋จ์ผ ๋ฌผ๋ฆฌ ์๋ฒ์์ ๋ค์ค ์ธ์คํด์ค๋ฅผ ์คํํ ๊ฒฝ์ฐ ํฌํธ๊ฐ ๊ฒน์น์ง ์๊ธฐ ์ํด ์ฆ๊ฐํ๋ ํฌํธ ๊ฐ
- --directory rethinkdb_data2 : rethinkdb ๊ฐ ์ฌ์ฉํ ๋๋ ํ ๋ฆฌ ์ง์ (๋ค๋ฅธ ์ธ์คํด์ค์ ๊ฒน์น๋ฉด ์๋จ)
- --join localhost:29015 : ์๋ก์ด ์ธ์คํด์ค๊ฐ ํฉ๋ฅ๋ฅผ ์๋ํ ๊ธฐ์กด ์ธ์คํด์ค ์ฃผ์
RethinkDB Guide in python
Import the driver
import rethinkdb as r
Open a connection
conn = r.connect('localhost', 28015)
conn.use('taeyo_db')
or
r.connect(db='taeyo_db').repl()
Manipulating databases
r.db_create('taeyo_db').run(conn)
r.db_drop('taeyo_db').run(conn)
r.db_list().run(conn)
Manipulating tables
r.db("taeyo_db").table_create('authors').run(conn)
r.db("taeyo_db").table_drop('authors').run(conn)
r.db("taeyo_db").table_list().run(conn)
r.table('authors').index_create('post_id').run(conn)
r.table('authors').index_drop('post_id').run(conn)
r.table('authors').index_list().run(conn)
r.table('authors').index_rename('post_id', 'message_id').run(conn)
r.table('authors').index_status().run(conn)
The result will be:
{
"config_changes": [
<table configuration data>
],
"tables_created": 1
}
Insert data
r.table("authors").insert([
{ "name": "William Adama", "tv_show": "Battlestar Galactica",
"posts": [
{"title": "Decommissioning speech", "content": "The Cylon War is long over..."},
{"title": "We are at war", "content": "Moments ago, this ship received..."},
{"title": "The new Earth", "content": "The discoveries of the past few days..."}
]
},
{ "name": "Laura Roslin", "tv_show": "Battlestar Galactica",
"posts": [
{"title": "The oath of office", "content": "I, Laura Roslin, ..."},
{"title": "They look like us", "content": "The Cylons have the ability..."}
]
},
{ "name": "Jean-Luc Picard", "tv_show": "Star Trek TNG",
"posts": [
{"title": "Civil rights", "content": "There are some words I've known since..."}
]
}
]).run(conn)
The result will be:
{
"unchanged": 0,
"skipped": 0,
"replaced": 0,
"inserted": 3,
"generated_keys": [
"7644aaf2-9928-4231-aa68-4e65e31bf219",
"064058b6-cea9-4117-b92d-c911027a725a",
"543ad9c8-1744-4001-bb5e-450b2565d02c"
],
"errors": 0,
"deleted": 0
}
Retrieve documents
All documents in a table
cursor = r.table("authors").run()
for document in cursor:
print(document)
Filter documents based on a condition
cursor = r.table("authors").filter(r.row["name"] == "William Adama").run()
for document in cursor:
print(document)
Retrieve documents by primary key
This way we can retrieve the document directly without iterating through a cursor.
r.db('test').table('authors').get('7644aaf2-9928-4231-aa68-4e65e31bf219').run()
Realtime feeds
cursor = r.table("authors").changes().run()
for document in cursor:
print(document)
The code above will print the following messages in the second terminal:
{
"new_val": {
"id": "1d854219-85c6-4e6c-8259-dbda0ab386d4",
"name": "Laura Roslin",
"posts": [...],
"tv_show": "Battlestar Galactica",
"type": "fictional"
},
"old_val": {
"id": "1d854219-85c6-4e6c-8259-dbda0ab386d4",
"name": "Laura Roslin",
"posts": [...],
"tv_show": "Battlestar Galactica"
}
}
Update documents
Updating All documents in a table
r.table("authors").update({"type": "fictional"}).run()
The result will be:
{
"unchanged": 0,
"skipped": 0,
"replaced": 3,
"inserted": 0,
"errors": 0,
"deleted":0
}
Updating William Adama's record
r.table("authors").
filter(r.row['name'] == "William Adama").
update({"rank": "Admiral"}).run()
The result will be:
{
"unchanged": 0,
"skipped": 0,
"replaced": 1,
"inserted": 0,
"errors": 0,
"deleted": 0
}
Delete documents
r.table("authors").
filter( r.row["posts"].count() < 3 ).
delete().run()
The result will be:
{
"unchanged": 0,
"skipped": 0,
"replaced": 0,
"inserted": 0,
"errors": 0,
"deleted": 2
}