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
- 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 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)
最新文章
- [AHOI 2009] 维护序列(线段树模板题)
- JavaScript高级程序设计(三):基本概念:数据类型
- POJ 1321-棋盘问题(DFS 递归)
- Android进程间通信(IPC)机制Binder简介和学习计划
- 关于JDEV的连接问题
- 作业2——需求分析&;原型设计
- 一张图认识Python(附基本语法总结)
- 用理论告诉你 三极管和MOS管的区别在哪
- HTML5 头部【快速编写】
- lvs-dr
- 002.MySQL高可用主从复制部署
- OpenStack容器网络项目Kuryr(libnetwork)
- Ogre2.1 Hlms与渲染流程
- 一个简单的sel server 函数的自定义
- 运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration!
- golang 原子操作函数
- 图论-桥/割点/双连通分量/缩点/LCA
- #C++初学记录(ACM试题2)
- .NET 如何隐藏Console Application的窗口
- --whole-archive和--no-whole-archive
热门文章
- git pull出现fatal: unable to access 'https://github.com/XXX/YYY.git'
- Eoeclient源代码分析---SlidingMenu的使用
- Cocos2d-x 避免手工输入项目需要编译的cpp文件到Android.mk里
- python(2)- python程序的编写简单介绍
- substr使用注意
- 小胖学PHP总结4-----PHP的字符串操作
- caffe搭建--WINDOWS+VS2013下生成caffe并进行cifar10分类测试
- Linux dnsmasq 服务
- 网站web.cofig配置用户的权限
- EasyDarwin开源流媒体云平台之语音对讲功能设计与实现