我試圖找到一種方法來記錄從python代碼在Cassandra上完成的所有查詢。具體記錄,因爲他們已經完成使用BatchStatement
使用cassandra-python-driver記錄所有查詢
執行執行是否有任何掛鉤或回調我可以用來登錄?
我試圖找到一種方法來記錄從python代碼在Cassandra上完成的所有查詢。具體記錄,因爲他們已經完成使用BatchStatement
使用cassandra-python-driver記錄所有查詢
執行執行是否有任何掛鉤或回調我可以用來登錄?
2個選擇:
棒到session.add_request_init_listener
從源代碼:
一個)BoundStatement
https://github.com/datastax/python-driver/blob/3.11.0/cassandra/query.py#L560
傳遞的值存儲在raw_values
,你可以嘗試提取它
B)BatchStatement
https://github.com/datastax/python-driver/blob/3.11.0/cassandra/query.py#L676
它存儲用於構建該對象在_statements_and_parameters
的所有聲明和參數。 似乎可以拿來雖然它不是一個公共屬性
c)只有這個鉤子被調用時,我沒能找到任何其他掛鉤 https://github.com/datastax/python-driver/blob/master/cassandra/cluster.py#L2097
但它無關查詢實際執行 - 它只是一個檢查類型的查詢已建成什麼樣,或許添加額外的回調方式/ errbacks從不同的角度
方法,並使用痕跡
https://datastax.github.io/python-driver/faq.html#how-do-i-trace-a-request https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.ResponseFuture.get_all_query_traces
通過在Session.execute_async()中設置trace = True,可以爲任何請求啓用請求跟蹤。
bs = BatchStatement() bs.add_all(['insert into test.test(test_type, test_desc) values (%s, %s)', 'insert into test.test(test_type, test_desc) values (%s, %s)', 'delete from test.test where test_type=%s', 'update test.test set test_desc=%s where test_type=%s'], [['hello1', 'hello1'], ['hello2', 'hello2'], ['hello2'], ['hello100', 'hello1']]) res = session.execute(bs, trace=True) trace = res.get_query_trace() for event in trace.events: if event.description.startswith('Parsing'): print event.description
它產生以下輸出:
通過等待未來,然後ResponseFuture.get_query_trace()
下面是BatchStatement
使用選項2跟蹤的示例查看結果
Parsing insert into test.test(test_type, test_desc) values ('hello1', 'hello1')
Parsing insert into test.test(test_type, test_desc) values ('hello2', 'hello2')
Parsing delete from test.test where test_type='hello2'
Parsing update test.test set test_desc='hello100' where test_type='hello1'
您是否考慮過爲您的execute
或同等產品(例如execute_concurrent
)創建裝飾器,以記錄用於您的語句或準備好的語句的CQL查詢?
您可以通過僅在查詢成功執行時才記錄CQL查詢的方式來編寫此代碼。
我找不到如何在批處理執行後訪問查詢字符串。 –
您可以將回調添加到execute_async():https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.ResponseFuture.add_callback –
回調函數返回一個ResultSet',它不會沒有包括執行的quieres。 –
add_request_init_listener(fn, *args, **kwargs)
添加帶有參數的回調創建的任何請求的情況下被調用。
創建的每個客戶端請求後,它會被調用爲FN(response_future,* ARGS,** kwargs),併發送請求之前,*
使用回調,你可以輕鬆登錄的所有查詢那屆會議。
實施例:
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
class RequestHandler:
def on_request(self, rf):
# This callback is invoked each time a request is created, on the thread creating the request.
# We can use this to count events, or add callbacks
print(rf.query)
auth_provider = PlainTextAuthProvider(
username='cassandra',
password='cassandra'
)
cluster = Cluster(['192.168.65.199'],auth_provider=auth_provider)
session = cluster.connect('test')
handler = RequestHandler()
# each instance will be registered with a session, and receive a callback for each request generated
session.add_request_init_listener(handler.on_request)
from time import sleep
for count in range(1, 10):
print(count)
for row in session.execute("select * from kv WHERE key = %s", ["ed1e49e0-266f-11e7-9d76-fd55504093c1"]):
print row
sleep(1)
這是一個很好的方法,但是在執行查詢之前調用該回調函數,並返回語句對象而不是查詢字符串。 在'Preparedstatement'和'BatchStatement'的情況下,查詢和它的值和編碼,我找不到解碼它們的方法。 –
我正在使用'BatchStatement'。我嘗試訪問'_statements_and_parameters',但查詢和值被編碼。 當請求被啓動時,鉤子被調用,而不是在查詢完成時執行。因此,如果我使用它,我可能會記錄未能執行的查詢。 –
也試過跟蹤。不會返回由'BatchStatement'執行的查詢的完整日誌。 –
已添加代碼示例 – ffeast