一、使用cassandra-cli创建keyspace和column family
1、创建keyspace
[default@unknown] create keyspace keyspace1 with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' and strategy_options = [{replication_factor:1}];2、创建column family
[default@unknown] use keyspace1; [default@keyspace1] create column family Image WITH comparator = UTF8Type AND key_validation_class=UTF8Type AND column_metadata = [ {column_name: imageData, validation_class: BytesType} ];
二、图片的存取操作
1、图片的存储
import sys import pycassa from pycassa.columnfamily import ColumnFamily from pycassa.pool import ConnectionPool from pycassa.columnfamilymap import ColumnFamilyMap import StringIO pool = ConnectionPool('keyspace1', ['10.15.62.100:9160']) cf_image = pycassa.ColumnFamily(pool, 'Image') # store pictures into cassandra file = open('/home/Image/07018720.jpg', 'rb') data = file.read() file.close() cf_image.insert('07018720', {'imageData':data})
此时,如果使用cassandra-cli查询对应图片的话将得到这样的结果:
[default@keyspace1] get Image['07018720']; d4e3bf7fb448f0a910ca63f2c46e88cd295d8b16155f0a0ed2a3563b47fad7e7d3b75229......(16进制格式)而如果使用python直接从cassandra数据库中读取之后输出的话讲会显示乱码。
2、图片的提取
import sys import pycassa from pycassa.columnfamily import ColumnFamily from pycassa.pool import ConnectionPool from pycassa.columnfamilymap import ColumnFamilyMap import StringIO pool = ConnectionPool('keyspace1', ['10.15.62.100:9160']) cf_image = pycassa.ColumnFamily(pool, 'Image') # read picture from cassandra dataOut = cf_image.get('07018720').get('imageData') file2 = open('/home/Image/07018720-bak.jpg', 'wb') file2.write(dataOut) file2.close
三、相关注意事项
1、在10.15.62.100上面有Cassandra的数据库,端口为默认的9160,版本是1.0.9;
2、pycassa的版本是1.7.0,相关的安装与使用可参考它的官方文档;
3、之前看到有人在向Cassandra数据库存储图片的时候将图片装换为Byte[],然后又将此数组装换成string类型才存入到数据库当中,这样装换的步骤增多了,而且占用了更多的空间。我们使用BytesType类型(对应CQL里面的blob类型)直接将图片以二进制方式读取之后就可以存入Cassandra数据库当中去,更加的高效。