Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为数据库中的所有表生成数据库changefeed_Python_Rethinkdb_Reql - Fatal编程技术网

Python 如何为数据库中的所有表生成数据库changefeed

Python 如何为数据库中的所有表生成数据库changefeed,python,rethinkdb,reql,Python,Rethinkdb,Reql,我正在测试一个API,它可以在数据库的多个表中插入或删除数据。为了在使用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().

我正在测试一个API,它可以在数据库的多个表中插入或删除数据。为了在使用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
在运行此脚本之前,我将运行RejectionDB-port offset 1来初始化RejectionDB数据库

这个脚本运行后,我想使用localhost:8081上的web UI将数据插入表1或表2中,并查看运行脚本的终端中打印的更改。然而,这似乎不起作用, 因为r.表“table1”和“table2”可能不是有效的ReQL查询


如何监视两个表中的更改?

我最后在一个单独的线程中运行了每个表的ChangeFeed:

import rethinkdb as r
import threading

# Prior to running this script, run "rethinkdb --port-offset 1" at the command line
conn = r.connect('localhost', 28016)

def clear_test_database():
    '''Clear the contents of the "test" database by dropping and re-creating it.'''
    if 'test' in r.db_list().run(conn):
        r.db_drop('test').run(conn)
    r.db_create('test').run(conn)

clear_test_database()

def monitor_changes(table_name, conn):
    feed = r.table(table_name).changes().run(conn)
    for document in feed:
        print document

tables = ['table1', 'table2']

for table in tables:
    conn = r.connect('localhost', 28016)
    r.table_create(table).run(conn)
    thread = threading.Thread(target=monitor_changes, args=(table, conn))
    thread.start()
注意,我在for循环中重新定义了conn connection对象,因为这些对象不是线程安全的

为了测试该方法,我在localhost:8081打开了web UI,并使用了以下insert命令:

在Sublime runner中,我每次按Run按钮时都会看到添加的更改:


当我在insert命令中选择table1或table2时,这两种方法都有效。

我最终在一个单独的线程中为每个表运行changefeeds:

import rethinkdb as r
import threading

# Prior to running this script, run "rethinkdb --port-offset 1" at the command line
conn = r.connect('localhost', 28016)

def clear_test_database():
    '''Clear the contents of the "test" database by dropping and re-creating it.'''
    if 'test' in r.db_list().run(conn):
        r.db_drop('test').run(conn)
    r.db_create('test').run(conn)

clear_test_database()

def monitor_changes(table_name, conn):
    feed = r.table(table_name).changes().run(conn)
    for document in feed:
        print document

tables = ['table1', 'table2']

for table in tables:
    conn = r.connect('localhost', 28016)
    r.table_create(table).run(conn)
    thread = threading.Thread(target=monitor_changes, args=(table, conn))
    thread.start()
注意,我在for循环中重新定义了conn connection对象,因为这些对象不是线程安全的

为了测试该方法,我在localhost:8081打开了web UI,并使用了以下insert命令:

在Sublime runner中,我每次按Run按钮时都会看到添加的更改:


这在我在insert命令中选择table1或table2时都有效。

您可以使用r.union在一个查询中跟踪多个ChangeFeed:


您可以使用r.union在单个查询中跟踪多个ChangeFeed: