我正在参加 Yelp 数据集挑战赛,并使用 RethinkDB 来存储每个不同数据集的 JSON 文档。
我有以下脚本:
import rethinkdb as r
import json, os
RDB_HOST = os.environ.get('RDB_HOST') or 'localhost'
RDB_PORT = os.environ.get('RDB_PORT') or 28015
DB = 'test'
connection = r.connect(host=RDB_HOST, port=RDB_PORT, db=DB)
query = r.table('yelp_user').filter({"name":"Arthur"}).run(connection)
print(query)
Run Code Online (Sandbox Code Playgroud)
但是当我在 virtualenv 的终端上运行它时,我得到以下示例响应:
<rethinkdb.net.DefaultCursor object at 0x102c22250> (streaming):
[{'yelping_since': '2014-03', 'votes': {'cool': 1, 'useful': 2, 'funny': 1}, 'review_count': 5, 'id': '08eb0b0d-2633-4ec4-93fe-817a496d4b52', 'user_id': 'ZuDUSyT4bE6sx-1MzYd2Kg', 'compliments': {}, 'friends': [], 'average_stars': 5, 'type': 'user', 'elite': [], 'name': 'Arthur', 'fans': 0}, ...]
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用 pprint 来漂亮地打印输出,但我不明白如何解决的一个更大问题是以智能方式打印它们,就像不只是将“...”显示为输出的结尾。
有什么建议么?
我有一个RethinkDB表,其中的文档看起来像这样:
[
{ "date": Mon Oct 05 2015 19:30:00 GMT+01:00, "category": 1, "name": "Alice" },
{ "date": Wed Oct 07 2015 14:00:00 GMT+01:00, "category": 2, "name": "Ben" },
{ "date": Mon Nov 16 2015 12:30:00 GMT+01:00, "category": 1, "name": "Charles" },
{ "date": Sun Nov 01 2015 22:15:00 GMT+01:00, "category": 1, "name": "Donald" }
(...)
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个查询,以按月/年分组我的数据,然后按类别(在每个组内)分组,然后对子组执行一些聚合.
根据ReQL文档(https://www.rethinkdb.com/api/javascript/group/)中给出的示例,这将给出第一步:
r.table("seances").group([r.row("date").month(), r.row("date").year()])
Run Code Online (Sandbox Code Playgroud)
我被困在这里 链接与group(r.row("category"))给出错误消息(无法调用group输出group).ungroup在第二个之前调用group也不起作用.
知道如何做到这一点?(ReQL看起来非常强大,但与我习惯使用的lodash完全不同......).
在以下JSON中,我想选择销售额> 12500的记录.如何在ReThinkDB和ReQL中执行此操作?
JSON是:
{
"address": {
"address_line1": "Address Line 1" ,
"address_line2": "Address Line 2" ,
"city": "Kochin" ,
"country": "India" ,
"state": "Kerala"
} ,
"id": "bbe6a9c4-ad9d-4a69-9743-d5aff115b280" ,
"name": "Dealer 1" ,
"products": [
{
"product_name": "Stabilizer" ,
"sales": 12000
} ,
{
"product_name": "Induction Cooker" ,
"sales": 14000
}
]
}, {
"address": {
"address_line1": "Address Line 1" ,
"address_line2": "Address Line 2" ,
"city": "Kochin" ,
"country": "India" ,
"state": "Kerala"
} ,
"id": "f033a4c2-959c-4e2f-a07d-d1a688100ed7" ,
"name": …Run Code Online (Sandbox Code Playgroud) 假设我有一张叫做的桌子bookshelf.我创建了一个书架文档,其中包含一组ID,这些ID引用另一个名为books的表中的书籍.我试图使用getAll和merge组合查询中的文档,但我似乎无法将所有元素传递到getAll.我相信getAll将ID数组解释为单个文字"对象".
书架文件:
{
id: 'MyShelf'
books: [ 1, 2, 6, 9 ]
}
Run Code Online (Sandbox Code Playgroud)
书籍文件:
{
id: 1
name: 'Merging in RethinkDB for Dummies'
}
Run Code Online (Sandbox Code Playgroud)
我的查询:
r.db('test').table('bookshelf').merge(function(bookshelf) {
return {
booksOnShelf: r.table('books').getAll(bookshelf('books')).coerceTo('array')
}
})
Run Code Online (Sandbox Code Playgroud)
预期结果:
{
id: 'MyShelf'
books: [ 1, 2, 6, 9 ],
booksOnShelf: [
{ id: 1, name: 'Merging in RethinkDB for Dummies' },
{ id: 2, name: 'Cool Title!' },
...
]
}
Run Code Online (Sandbox Code Playgroud)
实际结果:
{
id: 'MyShelf'
books: [ …Run Code Online (Sandbox Code Playgroud) 我正在尝试为RethinkDB中的一些查询基准测试任务.我真的没有得到一个问题的好答案为什么RethinkDB的count()操作如此缓慢?
我有2GB数据的查询:
r.db("2GB").table("table").between(40, r.maxval, {index:"price"})
Run Code Online (Sandbox Code Playgroud)
查询在5毫秒内执行但是一旦我想要计算数字项目
r.db("2GB").table("table").between(40, r.maxval, {index:"price"}).count()
Run Code Online (Sandbox Code Playgroud)
耗时超过6秒 每个使用count操作的查询都非常慢.我在github上看到了很多问题,但无法得到确切的理由.
更新:它不仅仅是,between()而是所有其他filter...... 就像count()是非常缓慢
给定如下的数组和数据结构,我想从表字段标记中删除与removeVals匹配的任何值.我找到了一个改变的解决方案,并尝试使用deleteAt,但得到一个indicesOf不是一个函数错误.
var removeTags = ["val1", "val3"];
{
name: "test1",
tags: ["val1", "val2", "val3", "val4", "val5"]
}
Run Code Online (Sandbox Code Playgroud)
所以我应该留下:
{
name: "test1",
tags: ["val2", "val4", "val5"]
}
Run Code Online (Sandbox Code Playgroud)
我的代码到目前为止:
var removeTags = ["val1", "val2"]
r.db('test').table('data').get('test1').update(function(row) {
return row('tags').indexesOf(function(rl) {
return r.expr(removeTags).contains(rl)
})(0).do(function(idx) {
return {
roles: row('tags').deleteAt(idx)
}
})
});
Run Code Online (Sandbox Code Playgroud)
这是我用作参考的问题/答案.任何帮助将不胜感激,非常感谢!
我正在测试一个API,它在RethinkDB数据库的多个表中插入或删除数据.为了监视使用API时数据库发生的情况,我想在其所有表中打印更改.
这是我正在努力实现的一些"伪代码":
import rethinkdb as r
# Prior to running this script, run "rethinkdb --port-offset 1" at the command line
conn = r.connect('localhost', 28016)
if 'test' in r.db_list().run(conn):
r.db_drop('test').run(conn)
r.db_create('test').run(conn)
r.table_create('table1').run(conn)
r.table_create('table2').run(conn)
feed = r.table('table1' and 'table2').changes().run(conn)
for document in feed:
print document
Run Code Online (Sandbox Code Playgroud)
在运行此脚本之前,我将运行rethinkdb --port-offset 1初始化RethinkDB数据库.
一旦这个脚本运行,我想将数据插入其中一个table1或table2(例如,使用Web UI localhost:8081),并查看运行脚本的终端中打印的更改.但是,这看起来不起作用,因为r.table('table1' and 'table2')它可能不是有效的ReQL查询.
如何监控两个表中的更改?
reql ×7
rethinkdb ×7
python ×2
filter ×1
javascript ×1
join ×1
json ×1
merge ×1
nested-query ×1
python-3.x ×1