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.
- typeAsBlob(value)
- blobAsType(value)
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 Type | CQL Literal Type |
---|---|
None |
NULL |
bool |
boolean |
float |
float double |
int long |
int bigint varint smallint tinyint counter |
decimal.Decimal |
decimal |
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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」