Question
· Jul 16, 2018

Connect to IRIS with Python?

I have seen the Cache Python binding documentation and would like to know if it's also possible with IRIS so I can write data and also read/get it from the DB? The cachesys folder does not contain any python libraries.

Discussion (7)1
Log in or sign up to continue

Here is a quick example program I wrote a couple of months ago.  This uses JDBC and the JayDeBeapi library others have mentioned.  Note the Credentials import provides a set of  login credentials in the following format.

LocalCreds = {"user":"SuperUser", "password":"SYS"}

Here is the code:

import jaydebeapi
import credentials
def get_database_connection(inpDBInstance):
    IRIS_JARFILE = "/home/ritaylor/InterSystemsJDBC/intersystems-jdbc-3.2.0.jar"
    IRIS_DRIVER = "com.intersystems.jdbc.IRISDriver"
    AA_JARFILE = "/home/ritaylor/Downloads/AtScale/hive-jdbc-uber-2.6.5.0-292.jar"
    AA_DRIVER = "org.apache.hive.jdbc.HiveDriver"
    # note connecting to two data sources in the same program requires that you 
    # reference the paths to BOTH jar files in a list in every call. Otherwise the
    # second connection attempt will fail.  It appears that the paths only get
    # added once within a process.
    JDBC_JARFILES = [IRIS_JARFILE,AA_JARFILE]
    # Database settings - this should be in a config file somewhere
    if (inpDBInstance == "local"):
        dbConn = jaydebeapi.connect(IRIS_DRIVER,
                           "jdbc:IRIS://18.119.2.28:1972/USER",
                           credentials.LocalCreds,
                           JDBC_JARFILES)
    else:
         dbConn = None
  
    return dbConn
def run_database_query(inpQuery, inpDBInstance):
    resultSet = None
    dbConn = get_database_connection(inpDBInstance)
    cursor = dbConn.cursor()
    cursor.execute(inpQuery)
    resultSet = cursor.fetchall()
    return resultSet
def print_db_result_set(resultSet):
    if (resultSet != None):
        for row in resultSet:
            print(row)
    else:
        print("Input result set is empty")