【问题标题】:Persisting a zip file in cassandra and reading it back在 cassandra 中保存一个 zip 文件并将其读回
【发布时间】:2018-07-26 03:24:01
【问题描述】:

我需要在 cassandra 数据库中保存一个 zip 文件。我需要在另一个程序中恢复它。为了持久化它,我使用以下代码-

    bin_data = open("Model-File.zip", 'rb').read()
    bin_data=bin_data.decode('latin-1').encode("utf-8")

这个bin_data我可以以字符串格式保存到cassandra-

    CQLString = "INSERT INTO testkeyspacenew.model (modelid, data) 
    VALUES(%s,%s)"
    session.execute(CQLString, (model_id,bin_data))

但是,当读回它时,我无法以最初的格式获取 bin_data。因此无法重新创建 zip 文件。请帮忙。 这是我在阅读过程中尝试的-

    abc=session.execute(CQLString)
    for row in abc:
        data=row
    data=str(data)
    print (data.encode("utf-8").encode('latin-1'))

我在读取时打印的数据与我从 zip 文件中获得的 bin_data 不同。

【问题讨论】:

  • 数据列是什么类型的?
  • 文本类型。 session.execute(""" CREATE TABLE IF NOT EXISTS Model (ModelId text PRIMARY KEY, data text)""")

标签: python python-2.7 cassandra


【解决方案1】:

Cassandra 和 CQL 知道 blob 类型,这可能是您想要的。见:https://docs.datastax.com/en/archived/cassandra/1.2/cassandra/tools/use_about_data_types_c.html

您正在使用bin_data=bin_data.decode('latin-1').encode("utf-8") 对二进制数据进行转码,这根本不需要。

在顶部print (data.encode("utf-8").encode('latin-1')) 显示两个encode 调用。

编辑:在 DataStax 驱动程序中使用blob 找到了一个测试https://github.com/datastax/python-driver/blob/master/tests/integration/standard/test_types.py

        s.execute("CREATE TABLE blobbytes2 (a ascii PRIMARY KEY, b blob)")

        params = ['key1', bytearray(b'blob1')]
        s.execute("INSERT INTO blobbytes2 (a, b) VALUES (%s, %s)", params)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多