我试图找到一种方法来记录从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