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
testwith thedbcommand. - Then, we add the
table_createcommand 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/
rethinkDB python入门的更多相关文章
- python入门简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- python入门学习课程推荐
最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...
- Python运算符,python入门到精通[五]
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...
- Python基本语法[二],python入门到精通[四]
在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...
- Python基本语法,python入门到精通[二]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...
- visual studio 2015 搭建python开发环境,python入门到精通[三]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...
- python入门教程链接
python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...
- Python学习【第二篇】Python入门
Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
随机推荐
- 【转载】C#使用as关键字将对象转换为指定类型
在C#的编程开发过程中,很多时候涉及到数据类型的转换,可使用强制转换的方式,不过强制转换数据类型有时候会抛出程序异常错误,可以使用as关键字来进行类型的转换,如果转换成功将返回转换后的对象,如果转换不 ...
- iOS开发-NSArray
忙了一上午,解决了几个bug,现在终于抽出来一点时间喝点水休息下, 想着系列这么浩大的一个工程,才刚刚开始写,不能断了,就跟写小说一样,既然是系列,那么就需要不停更... 好吧. 简单的说说iOS开发 ...
- Linux基础命令汇总109条
1 文件管理 1.1 basename 1.1.1 功能说明 从文件名中去掉路径和扩展名 例:basename include/stdio.h .h Output &quo ...
- 类中变量私有化和调用:__x和getx/setx或者property
__xx:双前置下划线,子类不可继承属性.方法,父类私有. 详见:https://www.cnblogs.com/andy9468/p/8299448.html 例子1:隐藏数据:私有化后,用get和 ...
- win10家庭版本不能连接远程桌面
出现身份验证错误 要求的函数不受支持,CredSSP 加密 Oracle 修正 然后网上大多数教程是叫你修改组策略,然后试过坑的我发现自己的家庭版没有组策略,天真的我在网上找寻了装组策略的代码,一顿捣 ...
- linux----centos7 yum安装lnmp+zabbix
安装yum utils工具包,若不安装则会找不到命令yum-config-manageryum -y install yum-utils 启用yum仓库yum-config-manager --ena ...
- Markdown 语法介绍
Markdown 语法介绍 from:https://coding.net/help/doc/project/markdown.html 文章内容 1 Markdown 语法介绍 1.1 标题 1.2 ...
- linux网络编程之system v共享内存
接着上次的共享内存继续学习,这次主要是学习system v共享内存的使用,下面继续: 跟消息队列一样,共享内存也是有自己的数据结构的,system v共享内存也是随内核持续的,也就是说当最后一个访问内 ...
- P2085 最小函数值[优先队列]
题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...
- PL/SQL 使用控制流程
一.条件分支语句 1.if判断 IF <布尔表达式> THEN PL/SQL 和 SQL语句 END IF; 2.if else判断 IF <布尔表达式> THEN PL/SQ ...