Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中正确序列化和反序列化分页大小?_Python_Python 3.x_Cassandra_Nosql_Cql - Fatal编程技术网

如何在Python中正确序列化和反序列化分页大小?

如何在Python中正确序列化和反序列化分页大小?,python,python-3.x,cassandra,nosql,cql,Python,Python 3.x,Cassandra,Nosql,Cql,在我的Python应用程序中,我对Cassandra数据库进行查询。我正在尝试通过包实现分页。从下面的代码可以看出,paging_state返回字节数据类型。我可以将此值转换为字符串数据类型。然后我将str\u paging\u state变量的值发送给客户端。如果此客户端再次向我发送str\u paging\u state,我希望在查询中使用它 这部分代码的作用是: query = "select * from users where user_type = 'clients';&q

在我的
Python
应用程序中,我对
Cassandra
数据库进行查询。我正在尝试通过包实现分页。从下面的代码可以看出,
paging_state
返回
字节
数据类型。我可以将此值转换为
字符串
数据类型。然后我将
str\u paging\u state
变量的值发送给客户端。如果此客户端再次向我发送
str\u paging\u 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'"
错误

[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
[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
在Java中,我找到了
.fromString
方法,该方法从先前使用
toString()
生成的字符串创建
PagingState
对象。不幸的是,我没有在Python中找到此方法的等效项

我还尝试使用package对
分页状态
进行解码和编码

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] 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
[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

我将感谢任何帮助

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

谢谢您的回答!我测试了它们。我可以说,
hexlify
unexlify
a2b_base64
函数工作没有任何问题。但功能有问题。在我的例子中,函数只在没有
星号字符
换行符
选项的情况下工作。十六进制是最稳定的,base64+HTTP可能会中断某些字符。。。