我是 cassandra 和 python 的新手。我正在尝试使用 python 在 cassandra 的表中插入图像。我想在从数据库中检索后查看我正在插入的图像和相同的图像。
import cassandra;
print cassandra.__version__ #3.4.1
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1:9042']) # TEST CLUSTER 127.0.0.1:9042
session = cluster.connect('prueba')
#Open the image file on lecture and binary mode
file = open('gato.jpg', 'rb') #file = open('gato.jpg', 'r')
print file
print type(file) #file
imagen_gato = file.read()
print type(imagen_gato) #str
#r_data = binascii.unhexlify(imagen_gato.strip())
#print type(r_data)
stream = io.BytesIO(imagen_gato)
print type(stream) #<type '_io.BytesIO'>
我看到图像是正确的
#Let's show the image just loaded
import Tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
#path = 'gato.jpg'
file = open('gato.jpg', 'rb') #dont know why i need to do this again
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
#img = ImageTk.PhotoImage(Image.open(path))
img = ImageTk.PhotoImage(Image.open(file))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
我尝试在 casandra 表中插入文件并检索它(这里是错误)
#guardamos dicho archivo en cassandra
print 'Last Successfull print'
session.execute( #ON THIS LINE IS THE ERROR
"""
INSERT INTO fotos (id_foto, imagen )
VALUES (%s, %s)
""",
(1,imagen_gato)
)
print 'Success'
#imprimimos dicho archivo desde casandra
print 'retrive'
rows = session.execute('SELECT id_foto, imagen FROM fotos')
for foto_row in rows:
#print foto_row.id_foto, foto_row.imagen
file2 = foto_row.imagen
print 'Success2'
然后我尝试再次打印图像
#This creates the main window of an application
window = tk.Tk()
window.title("Join2")
window.geometry("300x300")
window.configure(background='grey')
#path = 'gato.jpg'
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(file2))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
但是我得到了一个错误
d4b4a173636c461a8911bb30cdcd9041aa7d4c8950f4239645bf335afe19fe55fc6ff008d31216f3
3066a6e3b8cb82727e2e72cd4434314266e453f73ffd9272c200909312c20277573756172696f203
127290a09'; java.nio.charset.MalformedInputException: Input length = 1">
我不知道如何进行这项工作,也不知道为什么会出现错误。我认为这是图像的格式,cassandra 采用 blob ,
最佳答案
下面是我的例子的目录结构
[vagrant@casstest ImageInsert]$ ll
total 24
-rw-rw-r--. 1 vagrant vagrant 1128 May 22 10:15 Cass.py
-rw-rw-r--. 1 vagrant vagrant 17715 Sep 13 2019 sample.jpg
下面是 Cass.py,它将把图像插入到 cassandra
import base64
from cassandra.cluster import Cluster
cluster = Cluster(['192.168.0.85'])
#create session
session = cluster.connect()
# create keyspace "event" and table "blobtest" if not exist
session.execute("CREATE KEYSPACE IF NOT EXISTS event WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;")
session.execute("CREATE TABLE IF NOT EXISTS event.blobtest ( Name varchar PRIMARY KEY, Image blob)")
#will print your cassandra version
print(session.execute("SELECT release_version FROM system.local").one())
# convert local image into base64
image = open('sample.jpg', 'rb') #open binary file in read mode
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
# define which keyspace/database to use for further query
session.execute('USE event')
strCQL = "INSERT INTO blobtest (Name,Image) VALUES (?,?)"
pStatement = session.prepare(strCQL)
session.execute(pStatement,["Sample",image_64_encode])
# print binary data of image from query
print(session.execute("SELECT * FROM blobtest").one())
关于python - 使用python将图像插入到cassandra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087355/