2017-05-24 3 views
2

Ich bin auf Linux-Plattform mit Cassandra-Datenbank. Ich möchte Bilder Daten in Cassandra-Datenbank mit Python-Code von einem Remote-Server einfügen. Zuvor hatte ich einen Python-Code geschrieben, der Bilder von einem entfernten Server in die MySQL-Datenbank einfügt. Bitte beachten Sie den Code unten für MySQLFüllen Cassandra-Datenbank mit Python

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import MySQLdb as mdb 
import psycopg2 
import sys 
import MySQLdb 
def read_image(i): 
    filename="/home/faban/Downloads/Python/Python-Mysql/images/im" 
    filename=filename+str(i)+".jpg" 
    print(filename) 
    fin = open(filename)  
    img = fin.read() 
    return img 
con = MySQLdb.connect("192.168.50.12","root","faban","experiments") 
with con: 
    print('connecting to database') 
    range_from=input('Enter range from:') 
    range_till=input('Enter range till:') 
    for i in range(range_from,range_till): 
    cur = con.cursor() 
    data = read_image(i) 
    cur.execute("INSERT INTO images VALUES(%s, %s)", (i,data,)) 
    cur.close() 
    con.commit() 
con.close() 

Dieser Code erfolgreich fügt Daten in MySQL-Datenbank, die bei .12 befindet Ich möchte den gleichen Code ändern, Daten in Cassandra Datenbank einzufügen, die auch an entfernt .12 Bitte helfen Sie mir in dieser Hinsicht.

+2

versuchen Python Cassandra-Treiber. In Cassandra gibt es Blob-Unterstützung. https://datastax.github.io/python-driver/ –

+0

Vielen Dank! –

Antwort

1

Wenn ich eine einfache Tabelle wie folgt aus:

CREATE TABLE stackoverflow.images (
    name text PRIMARY KEY, 
    data blob); 

ich diese Bilder mit Python-Code laden, die ähnlich wie bei Ihnen ist, aber mit einigen geringfügigen Änderungen der DataStax Python Cassandra Treiber zu verwenden (pip install cassandra-driver):

#imports for DataStax Cassandra driver and sys 
from cassandra.cluster import Cluster 
from cassandra.auth import PlainTextAuthProvider 
from cassandra.cluster import SimpleStatement 
import sys 

#reading my hostname, username, and password from the command line; defining my Cassandra keyspace as as variable. 
hostname=sys.argv[1] 
username=sys.argv[2] 
password=sys.argv[3] 
keyspace="stackoverflow" 

#adding my hostname to an array, setting up auth, and connecting to Cassandra 
nodes = [] 
nodes.append(hostname) 
auth_provider = PlainTextAuthProvider(username=username, password=password) 
ssl_opts = {} 
cluster = Cluster(nodes,auth_provider=auth_provider,ssl_options=ssl_opts) 
session = cluster.connect(keyspace) 

#setting my image name, loading the file, and reading the data 
name = "IVoidWarranties.jpg" 
fileHandle = open("/home/aploetz/Pictures/" + name) 
imgData = fileHandle.read() 

#preparing and executing my INSERT statement 
strCQL = "INSERT INTO images (name,data) VALUES (?,?)" 
pStatement = session.prepare(strCQL) 
session.execute(pStatement,[name,imgData]) 

#closing my connection 
session.shutdown() 

Hoffe, dass hilft!

+0

Vielen Dank @Aaron! sudo pip install --install-option = "- no-cython" speicherte meinen Tag. –

+0

@MuhammadKaramShehzad gut zu hören, es hat geholfen! Vergiss nicht zu upvotieren und/oder zu akzeptieren! – Aaron

Verwandte Themen