【问题标题】:How to properly serialize and deserialize paging_size in Python?如何在 Python 中正确序列化和反序列化 paging_size?
【发布时间】:2021-01-22 12:55:04
【问题描述】:

在我的Python 应用程序中,我查询Cassandra 数据库。我正在尝试通过 cassandra-driver 包实现分页。从下面的代码可以看出,paging_state 返回bytes 数据类型。我可以将此值转换为string 数据类型。然后我将str_paging_state 变量的值发送给客户端。如果此客户再次向我发送str_paging_state,我想在我的查询中使用它。

这部分代码有效:

query = "select * from users where user_type = 'clients';"
statement = SimpleStatement(query, fetch_size=10)
results = session.execute(statement)

paging_state = results.paging_state
print(type(paging_state)) # <class 'bytes'>

str_paging_state = str(paging_state)
print(str_paging_state) # "b'\\x00C\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x03_hk\\x00\\x00\\x00\\x11P]5C#\\x8bGD~\\x8b\\xc7g\\xda\\xe5rH\\xb0\\x00\\x00\\x00\\x03_rk\\x00\\x00\\x00\\x18\\xee\\x14\\xf7\\x83\\x84\\x00tTmw[\\x00\\xec\\xdb\\x9b\\xa9\\xfd\\x00\\xb9\\xff\\xff\\xff\\xff\\xfe\\x01\\x00'"

这部分代码引发错误:

results = session.execute(
    statement,
    paging_state=bytes(str_paging_state.encode())
)

错误

[ERROR] NoHostAvailable: ('Unable to complete the operation against any hosts')
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 51, in lambda_handler
    results = cassandra_connection.execute(statement, paging_state=bytes(paging_state.encode()))
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 2618, in execute
    return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state, host, execute_as).result()
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 4877, in result
    raise self._final_exceptionEND RequestId: 4b7bf588-a2d2-45e5-ad7e-8611f1704313

在 Java documentation 中,我找到了 .fromString 方法,该方法从先前使用 toString() 生成的字符串创建 PagingState 对象。不幸的是,我在 Python 中没有找到与此方法等效的方法。

我还尝试使用codecs 包对paging_state 进行解码和编码。

str_paging_state = codecs.decode(paging_state, encoding='utf-8', errors='ignore')
# "\u0000C\u0000\u0000\u0000\u0002\u0000\u0000\u0000\u0003_hk\u0000\u0000\u0000\u0011P]5C#GD~grH\u0000\u0000\u0000\u0003_rk\u0000\u0000\u0000\u0018\u0014\u0000tTmw[\u0000ۛ\u0000\u0001\u0000"

# Raise error
results = session.execute(statement, paging_state=codecs.encode(str_paging_state, encoding='utf-8', errors='ignore'))

在这种情况下,我会看到下一个 错误

[ERROR] ProtocolException: <Error from server: code=000a [Protocol error] message="Invalid value for the paging state">
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 50, in lambda_handler
    results = cassandra_connection.execute(
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 2618, in execute
    return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state, host, execute_as).result()
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 4877, in result
    raise self._final_exceptionEND RequestId: 979f098a-a566-4904-821a-2ce06522d909

在我的例子中,protocol version 是 4。

cluster = Cluster(..., protocol_version=4)

我将不胜感激!

【问题讨论】:

    标签: python python-3.x cassandra nosql cql


    【解决方案1】:

    只需将二进制数据转换为十六进制字符串或 base64 - 为此使用 binascii module。例如,对于第一种情况函数 hexlify/unhexlify(或在 Python 3 中使用 .hex 二进制数据方法),对于 base64 - 使用函数 b2a_base64/a2b_base64

    【讨论】:

    • 感谢您的回答!我测试了所有这些。我可以说hexlifyunhexlifya2b_base64 函数可以正常工作。但是b2a_base64 函数有问题。在我的情况下,函数只能在没有 asterisk characternewline 选项的情况下工作。
    • hex 最稳定,base64 + HTTP 可能会破坏一些字符...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2022-01-02
    • 2018-12-26
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多