简体   繁体   English

在cassandra中保留一个zip文件并将其读回

[英]Persisting a zip file in cassandra and reading it back

I need to persist a zip file in cassandra data base. 我需要在cassandra数据库中保存一个zip文件。 And I need to get it back in another program. 我需要在另一个程序中恢复它。 For persisting it, I am using the following code- 为了坚持下去,我使用以下代码 -

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

This bin_data I can persist to cassandra in string format- 这个bin_data我可以用字符串格式保存到cassandra-

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

However when reading it back, I am unable to get the bin_data in the format in which it was in the beginning. 但是当读回它时,我无法以开头的格式获取bin_data。 Hence unable to recreate the zip file. 因此无法重新创建zip文件。 Please help. 请帮忙。 This is what I tried during reading- 这是我在阅读时尝试的 -

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

The data I am printing while reading is not same as the bin_data I got from the zip file. 我在阅读时打印的数据与我从zip文件中获取的bin_data不同。

Cassandra and CQL know a blob type which is probably what you want. Cassandra和CQL知道blob类型,这可能是你想要的。 See: https://docs.datastax.com/en/archived/cassandra/1.2/cassandra/tools/use_about_data_types_c.html 请参阅: https//docs.datastax.com/en/archived/cassandra/1.2/cassandra/tools/use_about_data_types_c.html

You are transcoding the binary data with bin_data=bin_data.decode('latin-1').encode("utf-8") which should not be necessary at all. 您正在使用bin_data=bin_data.decode('latin-1').encode("utf-8")对二进制数据进行转码,这根本不需要。

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

Edit: found a test from the DataStax driver using blob here https://github.com/datastax/python-driver/blob/master/tests/integration/standard/test_types.py 编辑:在这里使用blob从DataStax驱动程序中找到一个测试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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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