简体   繁体   English

使用 python cassandra 驱动程序选择和解码 blob

[英]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
  1. My first question is it possible to directly select the parent trace without iterating through the result?我的第一个问题是否可以直接选择父跟踪而不遍历结果? Is there a way i can get the first element in the list and then get the elements inside from the select statment?有没有办法可以获取列表中的第一个元素,然后从 select 语句中获取元素?
  2. From the terminal using cqlsh ,I am getting this result: 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.由于我想再次用于查询表,因此如何对其进行解码。
  1. To my knowledge, there is no easy way as there is no guarantee that the spans are stored in a specific order.据我所知,没有简单的方法,因为不能保证跨度以特定顺序存储。 Worth noting though, is if by 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是指跟踪的根跨度(第一个跨度),那么您可以搜索refsnull跨度,因为根跨度没有父跨度。 Another way to identify a root span is if the trace_id == span_id .另查明根跨度方式是,如果trace_id == span_id
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM