Search code examples
javamongodbmavenmahoutmongodb-java

MongoDB and Apache Mahout Connection Error


Trying to Connect Apache Mahout to a Mongo DB. However when I run it I get the following error. Not Sure what's wrong. I have Also made sure that the MongoClient Library connects.

Error:

java.lang.NoSuchMethodError: com.mongodb.DBCollection.ensureIndex(Lcom/mongodb/DBObject;)V

    at org.apache.mahout.cf.taste.impl.model.mongodb.MongoDBDataModel.buildModel(MongoDBDataModel.java:559)
    at org.apache.mahout.cf.taste.impl.model.mongodb.MongoDBDataModel.<init>(MongoDBDataModel.java:243)
    at util.Connection.connectToServer(Connection.java:23)

Method:

public MongoDatabase connectToServer() throws MongoSocketException, UnknownHostException {


        //Working
        MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");

            MongoClient mongoDB = new MongoClient(uri);
            MongoDatabase mongoConnection = mongoDB.getDatabase("loka");

        //Throws Error
            MongoDBDataModel dbm = new MongoDBDataModel("127.0.0.1",27017,"loka","BEACON_LOOKUP",true,false,null);

            return mongoConnection;
    }

POM Dependancies:

<dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.mahout</groupId>
            <artifactId>mahout-core</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.mahout</groupId>
            <artifactId>mahout-math</artifactId>
            <version>0.12.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.mahout</groupId>
            <artifactId>mahout-collections</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.mahout</groupId>
            <artifactId>mahout-integration</artifactId>
            <version>0.12.2</version>
        </dependency>

Solution

  • I'm no expert of Mahout nor Java. But the error message does look like you are using an incompatible version of MongoDB Java Driver.

    Note that there are 2 branches of MongoDB Java Driver:

    1. 2.x maintains old driver from before.
    2. To support async and new MongoDB features there's 3.x branch.

    The 3.x branch is not backward compatible. Methods like ensureIndex are changed to createIndex for example. There are bunch of other changes too. So back to your question, I think you should try latest 2.x branch, which is 2.14 by now.

    To check compatibility between Java Driver and MongoDB version, read the document.