【问题标题】:MongoDB's reduce-phase is not working as expectedMongoDB 的缩减阶段未按预期工作
【发布时间】:2014-03-12 13:49:38
【问题描述】:

我在 MongoDB 中使用了一个用于 mapReduce-Programming 的 java 教程,并最终得到了以下代码:

package mapReduceExample;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;

public class MapReduceExampleMain {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Mongo mongo;

        try {
            mongo = new Mongo("localhost", 27017);
            DB db = mongo.getDB("library");

            DBCollection books = db.getCollection("books");

            BasicDBObject book = new BasicDBObject();
            book.put("name", "Understanding JAVA");
            book.put("pages", 100);
            books.insert(book);

            book = new BasicDBObject();
            book.put("name", "Understanding JSON");
            book.put("pages", 200);
            books.insert(book);

            book = new BasicDBObject();
            book.put("name", "Understanding XML");
            book.put("pages", 300);
            books.insert(book);

            book = new BasicDBObject();
            book.put("name", "Understanding Web Services");
            book.put("pages", 400);
            books.insert(book);

            book = new BasicDBObject();
            book.put("name", "Understanding Axis2");
            book.put("pages", 150);
            books.insert(book);

            String map = "function()"
                    + "{ "
                        + "var category; "
                        + "if ( this.pages > 100 ) category = 'Big Books'; "
                        + "else category = 'Small Books'; "
                        + "emit(category, {name: this.name});"
                    + "}";

            String reduce = "function(key, values)"
                    + "{"
                        + "return {books: values.length};"
                    + "} ";

            MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
                    null, MapReduceCommand.OutputType.INLINE, null);

            MapReduceOutput out = books.mapReduce(cmd);

            for (DBObject o : out.results()) {
                System.out.println(o.toString());
            }

            //aufräumen
            db.dropDatabase();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
}

这是一个非常简单的减少阶段,但它不是我想要的:(

输出是:

{ "_id" : "Big Books" , "value" : { "books" : 4.0}}
{ "_id" : "Small Books" , "value" : { "name" : "Understanding JAVA"}}

我希望这样:

{ "_id" : "Big Books" , "value" : { "books" : 4.0}}
{ "_id" : "Small Books" , "value" : { "books" : 1.0}}

为什么reduce-Phase在小书的情况下不返回values.length?

你好,安德烈

【问题讨论】:

    标签: java mongodb mapreduce


    【解决方案1】:

    因为如果只有一个结果,reduce 永远不会运行。把它改成finalize函数什么的。

    【讨论】:

      【解决方案2】:

      对 mapReduce 工作原理的基本理解


      让我们介绍mapReduce的概念

      • ma​​pper - 这是发出要馈送到reduce 阶段的数据的阶段。它需要发送一个 key 和一个 value。如果您想在映射器中发射多次,但要求保持不变。

      • reducer - 当给定键的多个值 来处理已发出的值列表时,将调用 reducer 为那个键。


      也就是说,由于 ma​​pper 只发出 一个 键值,因此您的 reducer 没有被调用。

      您可以在 finalise 中清理它,但 ma​​pper 中的 emit 的行为是按照标准设计的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-14
        • 2020-03-15
        • 1970-01-01
        • 2021-03-10
        • 2018-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多