Ask Your Question
1

Connecting to MongoDB from Python

asked 2019-04-30 02:56:37 -0500

aimad.goussa@gmail.com gravatar image

updated 2019-04-30 07:59:11 -0500

I have a problem connecting to MongoDB from Python.

Problem : No module named mongodb in <script> at line number 9

Driver name : MongoDB Java Driver » 3.11.0-beta2

Link Oracle: /opt/cloudera/parcels/STREAMSETS_DATACOLLECTOR-3.8.1/streamsets-libs-extras/streamsets-datacollector-jdbc-lib/lib/ojdbc8.jar

Link mongoDB: /opt/cloudera/parcels/STREAMSETS_DATACOLLECTOR-3.8.1/streamsets-libs-extras/streamsets-datacollector-jython_2_7-lib/lib/mongo-java-driver-3.11.0-beta2.jar

Python code:

import sys
import json
from java.sql import DriverManager
from java.sql import Types
import string
sys.path.append("mongo-java-driver-3.11.0-beta2.jar")
from com.mongodb import *
from com.mongodb.client.model import *
from org.bson import *
import org.bson.Document as Document

con_ora = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.123.26)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=F2000Q)))", "FFF_RF2K", "FffqWeb*service")
con_mdb = MongoClient("FFF-MONQ-BCK01.fff.lan:27017")

con_ora.autoCommit=0
db = con_mdb.getDatabase("competition")
cpt=0
for record in records:
    collection = db.getCollection("testcompetitions")
    document = Document()
    document.putAll(record.value['data'])
    collection.insertOne(document);
    stmt = con_ora.prepareCall("begin FFF_RF2K.IO_TRAIT_F2K_MDB.Modification(?,?,?); end;")
    stmt.setString(1,str(record.value['KEY']))
    stmt.setString(2,"P" )
    stmt.setString(3,record.value['FIN_TRAIT'] )
    stmt.execute()
    con_ora.commit()
    stmt.close()
con_ora.close()

UPDATED: I installedl the MongoDB and Oracle drivers on driver package also i remove sys.path.append on the code but i have the same problem

import sys
import json
from java.sql import DriverManager
from java.sql import Types
import string
#sys.path.append("mongo-java-driver-3.11.0-beta2.jar")
from com.mongodb import *
from com.mongodb.client.model import *
from org.bson import *
import org.bson.Document as Document

con_ora = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.123.26)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=F2000Q)))", "FFF_RF2K", "FffqWeb*service")
con_mdb = MongoClient("FFF-MONQ-BCK01.fff.lan:27017")

con_ora.autoCommit=0
db = con_mdb.getDatabase("competition")
cpt=0
for record in records:
    collection = db.getCollection("testcompetitions")
    document = Document()
    document.putAll(record.value['data'])
    collection.insertOne(document);
    stmt = con_ora.prepareCall("begin FFF_RF2K.IO_TRAIT_F2K_MDB.Modification(?,?,?); end;")
    stmt.setString(1,str(record.value['KEY']))
    stmt.setString(2,"P" )
    stmt.setString(3,record.value['FIN_TRAIT'] )
    stmt.execute()
    con_ora.commit()
    stmt.close()
con_ora.close()
edit retag flag offensive close merge delete

Comments

Is it the exact same error message? Can you verify which directory on the disk contains the Oracle and Mongo DB drivers? Please edit your question rather than creating an answer.

metadaddy gravatar imagemetadaddy ( 2019-04-30 05:15:17 -0500 )edit

You need to install the Oracle lib for the Jython stage library as well as the JDBC. The stage libraries are isolated, so any drivers need to be installed again for each place you need to use them

metadaddy gravatar imagemetadaddy ( 2019-04-30 09:11:02 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2019-05-07 17:24:39 -0500

iamontheinet gravatar image

Hi!

Also refer to this https://ask.streamsets.com/question/4... to see how you can connect to MongoDB and execute queries from Jython Evaluator.

Cheers, Dash

edit flag offensive delete link more
0

answered 2019-04-30 03:39:27 -0500

metadaddy gravatar image

You need to use Package Manager to install the MongoDB and Oracle drivers for the Jython stage lib:

image description

You must also remove the sys.path.append call from your code.

The blog entry Calling External Java Code from Script Evaluators has some examples.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-04-30 02:56:37 -0500

Seen: 85 times

Last updated: May 07