[英]Select and decode blob using python cassandra driver
I am trying to query the traces Cassandra table which is part of the Jaeger architecture.我正在尝试查询属于 Jaeger 架构一部分的跟踪 Cassandra 表。 As you can see the refs field is a list:如您所见, refs 字段是一个列表:
cqlsh:jaeger_v1_dc1> describe traces
CREATE TABLE jaeger_v1_dc1.traces (
trace_id blob,
span_id bigint,
span_hash bigint,
duration bigint,
flags int,
logs list<frozen<log>>,
operation_name text,
parent_id bigint,
process frozen<process>,
refs list<frozen<span_ref>>,
start_time bigint,
tags list<frozen<keyvalue>>,
PRIMARY KEY (trace_id, span_id, span_hash)
)
from the python code:来自python代码:
traces = session.execute('SELECT span_id,refs from traces')
for t in traces:
if t.refs is not None:
parentTrace=t['refs'][0].trace_id
trace_id: 0x00000000000000003917678c73006f57
.从使用 cqlsh 的终端,我得到这个结果: trace_id: 0x00000000000000003917678c73006f57
。 However, from a python cassandra client I got this trace_id=b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x009\\x17g\\x8cs\\x00oW'
any idea what transformation happened to it?但是,从 python cassandra 客户端我得到了这个trace_id=b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x009\\x17g\\x8cs\\x00oW'
知道它发生了什么转变吗? How can decode it since I want to use to query the table again.由于我想再次用于查询表,因此如何对其进行解码。parentTrace
, you mean the root span of the trace (the first span), then you can search for spans where refs
is null
because a root span has no parent.但值得注意的是,如果parentTrace
是指跟踪的根跨度(第一个跨度),那么您可以搜索refs
为null
跨度,因为根跨度没有父跨度。 Another way to identify a root span is if the trace_id
== span_id
.另查明根跨度方式是,如果trace_id
== span_id
。trace_id
is stored as a binary blob. trace_id
存储为二进制 blob。 What you see from cassandra client is an array of 16 bytes with each octet element represented as two hexadecimal values.您从 cassandra 客户端看到的是一个 16 字节的数组,每个八位字节元素表示为两个十六进制值。 To convert it to the hex string you see in cqlsh, you'll need to convert the entire array to a single hex string.要将其转换为您在 cqlsh 中看到的十六进制字符串,您需要将整个数组转换为单个十六进制字符串。 See the following python example that does this:请参阅以下执行此操作的 python 示例:from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
rows = session.execute("select * from jaeger_v1_test.traces")
trace = rows[0]
hexstr = ''.join('{:02x}'.format(x) for x in trace.trace_id)
print("hex=%s, byte_arr=%s, len(byte_arr)=%d" % (hexstr, trace.trace_id, len(trace.trace_id)))
cluster.shutdown()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.