rethinkDB python入门

Start the server

For a more detailed look, make sure to read the quickstart.

$ rethinkdb

Import the driver

First, start a Python shell:

$ python

Then, import the RethinkDB driver:

import rethinkdb as r

Open a connection

When you first start RethinkDB, the server opens a port for the client drivers (28015 by default). Let’s open a connection:

r.connect( "localhost", 28015).repl()

Create a new table

By default, RethinkDB creates a database test. Let’s create a table authors within this database:

r.db("test").table_create("authors").run() 

The result will be:

{
    "config_changes": [
        <table configuration data> ], "tables_created": 1 } 

(The config_changes field contains metadata about the newly created table; for more details, read about the table_createcommand.) There are a couple of things you should note about this query:

  • First, we select the database test with the db command.
  • Then, we add the table_create command to create the actual table.
  • Lastly, we call run() in order to send the query to the server.

All ReQL queries follow this general structure. Now that we’ve created a table, let’s insert some data!

Insert data

Let’s insert three new documents into the authors table:

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": "Jean-Luc Picard", "tv_show": "Star Trek TNG", "posts": [ {"title": "Civil rights", "content": "There are some words I've known since..."} ] } ]).run() 

Documents in a table

To retrieve all documents from the table authors, we can simply run the query r.table('authors'):

cursor = r.table("authors").run() for document in cursor: print(document) 

The query returns the three previously inserted documents, along with the generated id values.

Since the table might contain a large number of documents, the database returns a cursor object. As you iterate through the cursor, the server will send documents to the client in batches as they are requested. The cursor is an iterable Python object so you can go through all of the results with a simple for loop.

Filter documents based on a condition

Let’s try to retrieve the document where the name attribute is set to William Adama. We can use a condition to filter the documents by chaining a filter command to the end of the query:

cursor = r.table("authors").filter(r.row["name"] == "William Adama").run() for document in cursor: print(document)

Retrieve documents by primary key

We can also efficiently retrieve documents by their primary key using the get command. We can use one of the ids generated in the previous example:

r.db('test').table('authors').get('7644aaf2-9928-4231-aa68-4e65e31bf219').run()

Realtime feeds

Feel free to skip this section if you don’t want to learn about realtime feeds yet. You can always go back and start a feed later.

RethinkDB inverts the traditional database architecture by exposing an exciting new access model – instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in realtime.

To start a feed, open a new terminal and open a new RethinkDB connection. Then, run the following query:

cursor = r.table("authors").changes().run() for document in cursor: print(document) 

Now switch back to your first terminal. We’ll be updating and deleting some documents in the next two sections. As we run these commands, the feed will push notifications to your program. 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" } }

RethinkDB will notify your program of all changes in the authors table and will include the old value and the new value of each modified document. See the changefeeds documentation entry for more details on how to use realtime feeds in RethinkDB.

 

参考:https://rethinkdb.com/docs/guide/python/

转载于:https://www.cnblogs.com/bonelee/p/6392565.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值