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:

  1. CREATE TABLE bios ( user_name varchar PRIMARY KEY,
  2. bio blob
  3. );
  4. INSERT INTO bios (user_name, bio) VALUES ('fred', bigintAsBlob(3));
  5. SELECT * FROM bios;
  6. user_name | bio
  7. -----------+--------------------
  8. fred | 0x0000000000000003

This example shows how to use blobAsBigInt.

  1. ALTER TABLE bios ADD id bigint;
  2. INSERT INTO bios (user_name, id) VALUES ('fred', blobAsBigint(0x0000000000000003));
  3. SELECT * FROM bios;
  4. user_name | bio | id
  5. -----------+--------------------+----
  6. 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

  1. def test_can_insert_blob_type_as_bytearray(self):
  2. """
  3. Tests that blob type in Cassandra maps to bytearray in Python
  4. """
  5. s = self.session
  6.  
  7. s.execute("CREATE TABLE blobbytes (a ascii PRIMARY KEY, b blob)")
  8.  
  9. params = ['key1', bytearray(b'blob1')]
  10. s.execute("INSERT INTO blobbytes (a, b) VALUES (%s, %s)", params)
  11.  
  12. results = s.execute("SELECT * FROM blobbytes")[0]
  13. for expected, actual in zip(params, results):
  14. self.assertEqual(expected, actual)

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

  1. from cassandra.cluster import Cluster
  2.  
  3. cluster = Cluster(["10.178.209.161"])
  4. session = cluster.connect('my_keyspace')
  5.  
  6. s = session
  7. s.execute("CREATE TABLE blobbytes (a ascii PRIMARY KEY, b blob)")
  8. params = ['key1', bytearray(b'blob1')]
  9. s.execute("INSERT INTO blobbytes (a, b) VALUES (%s, %s)", params)
  10. results = s.execute("SELECT * FROM blobbytes")[0]
  11. for expected, actual in zip(params, results):
  12. 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. 迅雷中Peer连接信息中的状态解释(转)

    在标准 Peer-to-Peer(P2P 点对点网络)中,以"Flags"表示 Peer Status(Peer 状态).其中: D - 正从 Peer 下载(感兴趣:解阻塞)搜索 ...

  2. 移除array中重复的item

    //move the repeated item            NSInteger index = [orignalArray count] - 1;            for (id o ...

  3. Android 源码编译记录

    问题1:Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /etc/ ...

  4. editplus重新载入文档

    editplus重新载入文档 :document->reload

  5. 转: 浅析Fusion-IO和Intel SSD

    from: http://alanwu.blog.51cto.com/3652632/865235 标签:SSD 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...

  6. 设计模式——介绍与工厂模式(扁平管理模式VS职业经理人模式)

    本文主要对设计模式进行大概解说.特别是对工厂模式进行简明的解析: 一.设计模式的分类 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式. ...

  7. nyoj 1077 小博弈 【另类巴什博奕】

    分析:分析当整除(a+b)的时候肯定是后者胜利,假设余数不等于0的时候.假设余数大于b肯定是前者胜利,否则后者胜利. 代码: import java.math.*; import java.util. ...

  8. Windows 如何为绿色软件运行时添加参数 如最小化,无窗口运行

    1 有些软件运行的时候需要或者可以添加参数来实现一些特殊要求,比如开机自启动,运行时不显示主界面,不显示托盘图标等,比如下面的这个流量精灵软件,"urlcore.exe /h /r /t 4 ...

  9. C - The C Answer (2nd Edition) - Exercise 1-16

    /* Revise the main routine of the longest-line program so it will correctly print the length of arbi ...

  10. MySQL的timeout那点事

    http://www.mysqlops.com/2011/11/24/mysql_timeout.html