如何使用blob数据类型在Cassandra中保存base64(图像)?
这是我的创建表查询:
CREATE TABLE IF NOT EXISTS xadvisor.session ( user_id varchar, images set <blob>, PRIMARY KEY(user_id));
这是我的插入查询:
INSERT INTO xadvisor.session ( user_id , images) VALUES ('XAU1', {\'"+base64+"\'});
base64是从我当地的图片中获得的:
var base64 = fs.readFileSync(path.resolve(__dirname + '/../tmp/assets/images_png/capture00001.png'), 'base64');
我得到了这样的错误:
{"name":"ResponseError","stack":"Error: Invalid set literal for images: value 'iVBORw0KGgoAAAANSUhEUgAABHQAAAHuCAYAAAD+yVs........
提前感谢您的关注和建议:)
答案 0 :(得分:0)
在来自datastax的nodejs驱动程序中,blob等于缓冲区,请参见此处: http://datastax.github.io/nodejs-driver/features/datatypes/
但是如果你打算使用base64 - 你只是处理一些文本,根本不需要使用blob。
提醒:保持'blob'足够小以提高性能(单位数兆字节)。
更新:哦,我忽略了这一套。
你有任何错误吗?那么请提供它们。
CQL中的集合受限 - 值必须小于64kb。 收藏(套装):收藏限额:约20亿(231);值大小: 65535(216-1)
http://docs.datastax.com/en/cql/3.3/cql/cql_reference/refLimits.html
更新:
typeAsBlob()可能是您的朋友(请参阅http://docs.datastax.com/en/cql/3.3/cql/cql_reference/blob_r.html) - 例如textAsBlob():
cqlsh:demo> DESCRIBE blob ;
CREATE TABLE demo.blob (
user_id text PRIMARY KEY,
payload blob
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';
cqlsh:demo> INSERT INTO blob (user_id, payload) VALUES ('demo_user','YXNsa2RramFzZAo=');
InvalidRequest: Error from server: code=2200 [Invalid query] message="Invalid STRING constant (YXNsa2RramFzZAo=) for "payload" of type blob"
cqlsh:demo> INSERT INTO blob (user_id, payload) VALUES ('demo_user',textAsblob('YXNsa2RramFzZAo='));
cqlsh:demo> SELECT * FROM blob ;
user_id | payload
-----------+------------------------------------
demo_user | 0x59584e7361325272616d467a5a416f3d
(1 rows)
cqlsh:demo>
由于base64编码的图像只是一个字符串,您也可以使用文本。并且为了存储您的图像,构建一个单独的表来保存它们,user_id作为分区键,一个唯一的标识符(可能是一个哈希)作为聚类列:
cqlsh> USE demo ;
cqlsh:demo> DESCRIBE TABLE demo.images ;
CREATE TABLE demo.images (
user_id text,
image_id text,
image_payload text,
PRIMARY KEY (user_id, image_id)
) WITH CLUSTERING ORDER BY (image_id ASC)
AND bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';
cqlsh:demo> INSERT INTO demo.images (user_id, image_id, image_payload) VALUES ('demo_user1', 'img000.png', 'YWRsa2tqYXNkaGpzZAo=');
cqlsh:demo> INSERT INTO demo.images (user_id, image_id, image_payload) VALUES ('demo_user1', 'img002.png', 'YWRsa2tqYXNkaGpzZAo=');
cqlsh:demo> INSERT INTO demo.images (user_id, image_id, image_payload) VALUES ('demo_user1', 'img017.png', 'YWRsa2tqYXNkaGpzZAo=');
cqlsh:demo> INSERT INTO demo.images (user_id, image_id, image_payload) VALUES ('demo_user2', 'img001.png', 'YWRsa2tqYXNkaGpzZAo=');
cqlsh:demo> INSERT INTO demo.images (user_id, image_id, image_payload) VALUES ('demo_user2', 'img002.png', 'YWRsa2tqYXNkaGpzZAo=');
cqlsh:demo> SELECT image_id, image_payload FROM demo.images WHERE user_id = 'demo_user1';
image_id | image_payload
------------+----------------------
img000.png | YWRsa2tqYXNkaGpzZAo=
img002.png | YWRsa2tqYXNkaGpzZAo=
img017.png | YWRsa2tqYXNkaGpzZAo=
(3 rows)
cqlsh:demo>