How to connect to MongoDB from Talend Open Studio?

Viewed 734

I am trying to connect to MongoDB database from Talend Open Studio, but am having some issues. At first, I didn't even have the components for connecting to MongoDB (tMongoDBConnection, tMongoDBInput) so I had to download and import them (following the steps from the first answer here: talend , mongoDB connection).

Now, I have a database called session_store and within, a collection called tutorialprogresses that has an attribute events of type Array. However, when I try to get the data from that attribute in Talend, it is always returning null...

This is what my job looks like: Job screenshot

And this is the configuration of my tMongoDBInput component and schema definition: tMongoDBInput

And this is what I get as a result:

.--------+---------+------.
|        tLogRow_1        |
|=-------+---------+-----=|
|courseId|studentId|events|
|=-------+---------+-----=|
|477     |2468     |null  |
|477     |2468     |null  |
|477     |2468     |null  |
|155     |2468     |null  |
|477     |2468     |null  |
|155     |2468     |null  |
'--------+---------+------'

I even tried doing this by writing my own Java code in tJava component, however I have run into another problem with that. This is my code:

System.out.println("HELLO");

Mongo mongo = new Mongo("127.0.0.1", 27017);

mongo.getDatabaseNames().forEach(System.out::println);

DB db = mongo.getDB("session_store");// db name

db.getCollectionNames().forEach(System.out::println);

DBCollection collection = db.getCollection("tutorialprogresses");

BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("studentId", "2468");
DBCursor cursor = collection.find(searchQuery);

while (cursor.hasNext()) {
    System.out.println(cursor.next());
}

However, it doesn't even enter the while loop, as if there are no rows to print, when there are... I tried debugging and concluded that it successfully gets the database and collection (rows 4 and 6), however obviously can't access the data in the collection and I don't know why...

So any help would be much appreciated, either with the first or the second approach, or a suggestion for a completely new one!

1 Answers

I never worked with MongoDB, but I guess this is more a talend-related problem .

Talend does not manage "Array" types. I guess you should try to get "events" as an Object type in Talend. Then put a tJavaRow after your tDBInput component, and try to parse this object as an array using java code. (tJavarow component allows you to import required libraries if not present).

Related