-1
我正在使用Ruby on Rails,并且我想直接将图像存储到cassandra数据库中。如何使用cassandra cql存储二进制数据
如何将cassandra-cql gem文件存储到cassandra数据库中? 而我该如何显示这个文件在image_tag?
我正在使用Ruby on Rails,并且我想直接将图像存储到cassandra数据库中。如何使用cassandra cql存储二进制数据
如何将cassandra-cql gem文件存储到cassandra数据库中? 而我该如何显示这个文件在image_tag?
首先确保您的列族验证程序是BytesType。然后,您可以将您的IMG到卡桑德拉像这样:
File.open('/tmp/image.jpg', 'r') do |f|
img = f.read.force_encoding('ASCII-8BIT') # ensure cassandra-cql knows this is binary
handle.execute("insert into img (KEY, colA) values ('rowA', ?)", img);
end
为了把它抓回来了:
img = handle.execute("select colA from img where KEY = 'rowA'").fetch[0]
从轨服务这一点,你需要做一个控制器的动作流的IMG使用send_data
与disposition => 'inline'
:
class ImgController < ApplicationController
def img
row_key = params['row_key']
col_name = params['col_name']
img = handle.execute("select ? from img where KEY = ?", col_name, row_key).fetch[0]
send_data(img, :filename => 'img.jpg', :type => 'image/jpeg', :disposition => 'inline')
end
end
然后你可以使用IMAGE_TAG链接到该控制器。看看this question for more info on streaming images from a controller。