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 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)

cassandra 存储二进制data的更多相关文章

  1. 数据库中用varbinary存储二进制数据

    问题描述:将图片.二进制文件内容等数据存储在数据库中,并能从数据库中取出还原为图片或文件,数据库存储二进制数据用varbinary字段. 分析:由于之前数据库中没有用过varbinary存储数据,首先 ...

  2. mssql sqlserver 可以存储二进制数据的字段类型详解

    转自: http://www.maomao365.com/?p=6738 摘要: 下文将从数据库的数据类型着手,剖析在sqlserver数据库中可以存储二进制数据的数据类型,如下所示: mssql s ...

  3. jquery在元素中存储数据:data()

    转自:http://www.php.cn/js-tutorial-405445.html 在元素中存储数据:data() 1 2 3 4 5 6 7 8 9 10 <!DOCTYPE html& ...

  4. python django中使用sqlite3数据库 存储二进制数据ByteArray

    在python中使用sqlite3数据库存储二进制流数据ByteArray,在django使用sqlite3数据库时,有时候也要注意最好使用二进制流ByteArray插入字符串. 使用ByteArra ...

  5. mysql 存储二进制数据

    晚上小研究了下MySQL存储于读取二进制数据的功能.关键步骤为以下三点: 最重要的一点:存储二进制数据的表的类型需要是blob类型(按长度不同分为tiny, media, long) 插入二进制数据时 ...

  6. python+ mysql存储二进制流的方式

    很多时候我们为了管理方便会把依稀很小的图片存入数据库,有人可能会想这样会不会对数据库造成很大的压力,其实大家可以不用担心,因为我说过了,是存储一些很小的图片,几K的,没有问题的! 再者,在这里我们是想 ...

  7. mongodb存储二进制数据

    mongodb 3.x存储二进制数据并不是以base64的方式,虽然在mongo客户端的查询结果以base64方式显示,请放心使用.下面来分析存储文件的存储内容.base64编码数据会增长1/3成为顾 ...

  8. Vue父子组件通信(父级向子级传递数据、子级向父级传递数据、Vue父子组件存储到data数据的访问)

    Vue父子组件通信(父级向子级传递数据.子级向父级传递数据.Vue父子组件存储到data数据的访问) 一.父级向子级传递数据[Prop]: ● Prop:子组件在自身标签上,使用自定义的属性来接收外界 ...

  9. mongodb存储二进制数据的二种方式——binary bson或gridfs

    python 版本为2.7 mongodb版本2.6.5 使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们 ...

随机推荐

  1. Wdatepick控件只能选当前时间以前的时间

    WdatePicker限制只能选当天,只能选以前的时间 (1)WdatePicker限制只能选以前的时间(不能选当天): onfocus="WdatePicker({startDate:'% ...

  2. javascript 函数初探 (六)--- 闭包初探#2

    首先,我们需要声明一个全局函数的占位符.尽管这种占位符不是必须的,但最好还是声明一下,然后我们重新将函数F()定义一下: var inner; var F = fucntion(){ var b = ...

  3. POJ2503字典树

    此代码原始出处:http://blog.csdn.net/cnyali/article/details/47367403 #include<stdio.h> #include<str ...

  4. Django的安装使用,以及建立本地网站

    一.安装Django pip install django 完成后即可 二.pycharm 建立django 点击file ——>new project 选择django项目——>more ...

  5. weblogic中部署项目报错org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken .

    原因: 原因是weblogic要查找自己的antlr,和lib下面的antlr包冲突.... 解决方法: 在weblogic.xml添加 <container-descriptor>    ...

  6. Oracle 【to_number】【instr】

    需求:对一个包含数字的字符串排序 search后参考了  http://www.cnblogs.com/m-cnblogs/archive/2012/03/30/2425938.html 截屏: (用 ...

  7. 摘自《Linux与unix shell编程指南》

    shift运行后,$#随之减少:如果需要知道命令行中输入的最后一个参数(通常是一个文件名),可以有两种选择:使用命令 eval echo \$$#;使用shift命令:shift 'expr $# - ...

  8. SharePoint ULS Log Viewer 日志查看器

    SharePoint ULS Log Viewer 日志查看器 项目描写叙述 这是一个Windows应用程序,更加轻松方便查看SharePoint ULS日志文件.支持筛选和简单的视图. 信息 这是一 ...

  9. (转载)JavaScript递归查询 json 树 父子节点

    在Json中知道某个属性名,想要确定该属性在Json树具体的节点,然后进行操作还是很麻烦的 可以用以下方法找到该属性所在的节点,和父节点 <!DOCTYPE html> <html ...

  10. Django框架学习——python模拟Django框架(转载)

    原贴来源 http://wiki.woodpecker.org.cn/moin/ObpLovelyPython/AbtWebModules python实现web服务器 web开发首先要有web服务器 ...