【问题标题】:Error in db.eval() in MongoDBMongoDB 中的 db.eval() 错误
【发布时间】:2015-05-26 14:41:16
【问题描述】:

我正在尝试使用 db.eval() 方法运行 map-reduce 脚本。

    MongoClient mongo = new MongoClient("localhost", 27017);
    DB mongodb = (DB) mongo.getDB("testDB");

    String script = "db.collection.mapReduce("
            + "function() {emit(this.class, this.marks);},"
            + "function(key, values) { return {\"sum\":Array.sum(values)};},"
               + "{ "
                   + " query : {_id:{$lt:50}},"
                   + "out:\"collectionMapReduce\""
               + "}"
           + ")";

    Object result = mongodb.eval(script);

我收到以下错误:

Exception in thread "main" com.mongodb.MongoException$Network: Read operation to server localhost:27017 failed on database testDB
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:298)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:269)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:84)
at com.mongodb.DB.command(DB.java:320)
at com.mongodb.DB.command(DB.java:299)
at com.mongodb.DB.command(DB.java:374)
at com.mongodb.DB.command(DB.java:246)
at com.mongodb.DB.doEval(DB.java:445)
at com.mongodb.DB.eval(DB.java:463)
at com.dev.TestMapReduce.main(TestMapReduce.java:25)

Caused by: java.io.EOFException
at org.bson.io.Bits.readFully(Bits.java:75)
at org.bson.io.Bits.readFully(Bits.java:50)
at org.bson.io.Bits.readFully(Bits.java:37)
at com.mongodb.Response.<init>(Response.java:42)
at com.mongodb.DBPort$1.execute(DBPort.java:164)
at com.mongodb.DBPort$1.execute(DBPort.java:158)
at com.mongodb.DBPort.doOperation(DBPort.java:187)
at com.mongodb.DBPort.call(DBPort.java:158)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:290)
... 9 more

我无法理解此错误的原因。我该如何解决这个问题?

注意:我可以使用db.collection.mapReduce() 来完成这项工作。 但我只是想使用db.eval()

来实现这一点

【问题讨论】:

    标签: java mongodb mapreduce mongo-java


    【解决方案1】:

    运行 map/reduce 作业的正确方法是通过 mapReduce command,它通过 MapReduceCommand helper 在 Java 驱动程序中公开。

    eval command 不适用于此目的(在 MongoDB 3.0 中也已弃用)。

    【讨论】:

      猜你喜欢
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2017-05-19
      • 2020-11-30
      • 1970-01-01
      相关资源
      最近更新 更多