pyignite 序列化和反序列化复杂类型

pyignite serialize and de serialize complex types

我正在尝试序列化和反序列化 json 地图(pyignite 中的地图复杂类型)。序列化似乎工作正常,但我在反序列化中遇到错误。我不确定我的序列化是否正确,但它转储了一个看起来像二进制字节数组的整数数组。

Traceback (most recent call last):
File "ignite-sql.py", line 44, in <module>
  print([[result[0], result[1], result[2], Map.from_python(result[3])] 
  for result in results])
File "ignite-sql.py", line 44, in <listcomp>
  print([[result[0], result[1], result[2], Map.from_python(result[3])] 
  for result in results])
File "env/lib/python3.7/site-packages/pyignite/datatypes/complex.py", line 276, in from_python
  for k, v in value.items():
AttributeError: 'list' object has no attribute 'items'

这是最简单的例子

client = Client()
client.connect('127.0.0.1', 10800)

PRESENCE_TABLE = '''
CREATE TABLE IF NOT EXISTS presence (
  subkey VARCHAR,
  channel VARCHAR,
  uuid VARCHAR,
  metadata BINARY,
  PRIMARY KEY (subkey, channel, uuid)
)'''

DROP_PRESENCE_TABLE = '''
DROP TABLE IF EXISTS presence
'''

INSERT_PRESENCE_TABLE = '''
INSERT INTO presence(
  subkey, channel, uuid, metadata
  ) VALUES (?, ?, ?, ?)
  '''


obj = Map.from_python({"foo":"bar"})
args = ["test","foo",str(uuid.uuid4()),obj]
client.sql(DROP_PRESENCE_TABLE)
client.sql(PRESENCE_TABLE)
client.sql(INSERT_PRESENCE_TABLE,query_args=args)

results = client.sql('select * from presence')

print([[result[0], result[1], result[2], Map.from_python(result[3])] for result in results])

我是 pyignite 的作者和维护者。

我会尽力回答您的问题,但由于不允许我在这里对问题发表评论,因此我将不得不对您尝试对代码执行的操作进行一些猜测。

  1. 您似乎想将地图对象放入 SQL 数据库中。不幸的是,Ignite 不是这样工作的。 Ignite SQL 使用的数据类型在 here 中有描述。如您所见,没有地图或其他复杂数据类型。

我不确定您要达到什么目的,但也许您可以为此目的在另一个 SQL table 上使用外键?对我来说,这比在 SQL 列中存储散列 table 更有意义。或者,您可以对数据进行 JSON 化并将其存储为 String (VARCHAR).

  1. 另一件事是您如何使用 from_python() 方法。这些方法仅供内部使用。从用户的角度来看,它们创建了无用的字节序列。

pyignite API 背后的第一个想法是用户应该只能使用内置 Python 类型在 Ignite 集群中存储和检索数据。所有序列化都在幕后。没有可用的序列化程序方法或对象。 Simple examples 就是为了给你这个想法而写的。

  1. Class Map 未记录,不应使用。关于如何使用类型提示的文档 MapObject class can be used as a type hint in that rare cases, when just giving pyignite a dict object is ambiguous. Please read this section

说了这么多,我希望我能为您指明正确的方向。如果我不是,请尝试澄清你的问题,我会改进我的答案。

祝您好运,感谢您使用 pyignite