2

I am on Linux platform with Cassandra database. I want to insert Images data into Cassandra database using Python Code from a remote server. Previously, I had written a python code that inserts Images' data into MySQL database from a remote server. Please see the code below for MySQL

#!/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()

This code successfully inserts data into MySQL database which is located at .12 I want to modify the same code to insert data into Cassandra database which is also located at .12 Please help me out in this regard.

2

1 Answer 1

1

If I create a simple table like this:

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

I can load those images with Python code that is similar to yours, but with some minor changes to use the DataStax Python Cassandra driver (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()

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @Aaron! sudo pip install --install-option="--no-cython" saved my day.
@MuhammadKaramShehzad good to hear it helped! Don't forget to upvote and/or accept!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.