2017-01-09 8 views
0

RethinkDBデータベースの複数のテーブルにデータを挿入または削除するAPIをテストしています。 APIを使用しているときにデータベースに何が起こっているかを監視するために、すべてテーブルの変更を印刷したいと思います。データベース内のすべてのテーブルに対してRethinkDBチェンジフィードを生成する方法

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 

このスクリプトを実行する前に、私はRethinkDBデータベースを初期化するrethinkdb --port-offset 1を実行します:

は、ここで私が達成しようとしているもののいくつかの「擬似コード」です。

このスクリプトが実行されると、私は(例えば、localhost:8081のウェブUIを使用して)table1table2のいずれかにデータを挿入し、スクリプトを実行している端末で印刷された変更を確認したいと思います。ただし、r.table('table1' and 'table2')はおそらく有効なReQLクエリではないため、これは動作しません。

どのようにして両方のテーブルの変更を監視できますか?

答えて

2

あなたはr.unionを使用して単一のクエリで複数のchangefeedsに従うことができます:

r.union(
    r.table('table1').changes(), 
    r.table('table2').changes() 
).run(conn) 
1

Iは、別のスレッドで各テーブルのchangefeedsを実行してしまった:これらのオブジェクトはスレッドセーフではないように私はforループ内conn接続オブジェクトを再定義

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() 

注意。私は変更は、私が「ファイル名を指定して実行」を押すたびに追加されて表示さ崇高なランナーで

enter image description here

は、メソッドをテストするために、私は localhost:8081でのWeb UIを開き、次 insertのコマンドを使用しましたボタン:

enter image description here

私がでtable1table2を選択した場合は、この両方の作品コマンド。

関連する問題