cassandra 存储二进制data

Blob type 

The Cassandra blob data type represents a constant hexadecimal number defined as 0[xX](hex)+ where hex is a hexadecimal character, such as [0-9a-fA-F]. For example, 0xcafe. The maximum theoretical size for a blob is 2 GB. The practical limit on blob size, however, is less than 1 MB. A blob type is suitable for storing a small image or short string.

Blob conversion functions 

These functions convert the native types into binary data (blob):
  • typeAsBlob(value)
  • blobAsType(value)
For every native, nonblob data type supported by CQL, the typeAsBlob function takes a argument of that data type and returns it as a blob. Conversely, the blobAsType function takes a 64-bit blob argument and converts it to a value of the specified data type, if possible.

This example shows how to use bigintAsBlob:

CREATE TABLE bios ( user_name varchar PRIMARY KEY, bio blob ); INSERT INTO bios (user_name, bio) VALUES ('fred', bigintAsBlob(3)); SELECT * FROM bios; user_name | bio -----------+-------------------- fred | 0x0000000000000003 

This example shows how to use blobAsBigInt.

ALTER TABLE bios ADD id bigint; INSERT INTO bios (user_name, id) VALUES ('fred', blobAsBigint(0x0000000000000003)); SELECT * FROM bios; user_name | bio | id -----------+--------------------+---- fred | 0x0000000000000003 | 3

官方说了,见 https://datastax.github.io/python-driver/getting_started.html?highlight=blob :

Type Conversions

For non-prepared statements, Python types are cast to CQL literals in the following way:

Python TypeCQL Literal Type
NoneNULL
boolboolean
float
float
double
int
long
int
bigint
varint
smallint
tinyint
counter
decimal.Decimaldecimal
str
unicode
ascii
varchar
text
buffer
bytearray
blob

摘自:https://github.com/datastax/python-driver/blob/master/tests/integration/standard/test_types.py

    def test_can_insert_blob_type_as_bytearray(self):
        """
        Tests that blob type in Cassandra maps to bytearray in Python
        """
        s = self.session

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

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

        results = s.execute("SELECT * FROM blobbytes")[0]
        for expected, actual in zip(params, results):
            self.assertEqual(expected, actual)

 

最后的可以工作的python代码:

from cassandra.cluster import Cluster

cluster = Cluster(["10.178.209.161"])
session = cluster.connect('my_keyspace')

s = session
s.execute("CREATE TABLE blobbytes (a ascii PRIMARY KEY, b blob)")
params = ['key1', bytearray(b'blob1')]
s.execute("INSERT INTO blobbytes (a, b) VALUES (%s, %s)", params)
results = s.execute("SELECT * FROM blobbytes")[0]
for expected, actual in zip(params, results):
     print (expected, actual)

 

转载于:https://www.cnblogs.com/bonelee/p/6515854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值