查询MongoDB Map Reduce函数 [英] Query in a MongoDB Map Reduce Function

查看:153
本文介绍了查询MongoDB Map Reduce函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经流式传输并将大约25万条推文保存到MongoDB中,在这里,我正在检索它,正如您所看到的那样,基于推文中出现的单词或关键字。

I have streamed and saved about 250k tweets into MongoDB and here, I am retrieving it, as you can see, based on a word, or keyword, present in the tweet.

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("TwitterData");
DBCollection collection = db.getCollection("publicTweets");
BasicDBObject fields = new BasicDBObject().append("tweet", 1).append("_id", 0);
BasicDBObject query = new BasicDBObject("tweet", new BasicDBObject("$regex", "autobiography"));
DBCursor cur=collection.find(query,fields);

我想要做的是使用Map-Reduce并根据关键字对其进行分类和将它传递给reduce函数来计算每个类别下的推文数量,有点像你可以看到的此处。在这个例子中,他正在计算页数,因为它是一个简单的数字。我想做类似的事情:

What I would like to do is to use Map-Reduce and based on the keyword, categorize it and pass it to the reduce function to count the number of tweets under each category, kinda like what you can see here. In the example, he's counting the number of pages as it is a simple number. I wanna do something like:

"if (this.tweet.contains("kword1")) "+
"category = 'kword1 tweets'; " + 
"else if (this.tweet.contains("kword2")) " + 
"category = 'kword2 tweets'; 

然后使用reduce函数来获取计数,就像在示例程序中一样。

and then use the reduce function to get the count, just like in the sample program.

我知道语法不正确,但这就是我想做的事情。有没有办法实现呢?谢谢!

I know that the syntax is incorrect, but that's pretty much what I would like to do. Is there any way of achieving it? Thanks!

PS :哦,我用Java编写代码。所以Java语法将受到高度赞赏。谢谢!

PS: Oh, and I'm coding in Java. So the Java syntax would be highly appreciated. Thank you!

所发布代码的输出如下:

The output of the code posted is something like this:

{ "tweet" : "An autobiography is a book that reveals nothing bad about its writer except his memory."}
{ "tweet" : "I refuse to read anything that's not real the only thing I've read since biff books is Jordan's autobiography #lol"}
{ "tweet" : "well we've had the 2012 publication of Ashley's Good Books, I predict 2013 will be seeing an autobiography ;)"}

这当然,适用于所有带有自传一词的推文。我想要的是在map函数中使用它,将其归类为自传推文(以及其他关键字),然后将其发送到reduce函数以计算所有内容并返回带有单词的推文数量它。

This of course, is for all tweets with the word "autobiography". What I'd like is to use this in the map function, categorize it as a "autobiography tweet" (and other keywords too), and then send it to the reduce function to count everything and return the number of tweets with the word in it.

类似于:

{"_id" : "Autobiography Tweets" , "value" : { "publicTweets" : 3.0}}
{"_id" : "Biography Tweets" , "value" : { "publicTweets" : 15.0}}


推荐答案

您可能需要尝试以下操作:

You might want to try the following:

    String map = "function() { " +
                 "    var regex1 = new RegExp('autobiography', 'i'); " +
                 "    var regex2 = new RegExp('book', 'i'); " +
                 "    if (regex1.test(this.tweet) ) " +
                 "         emit('Autobiography Tweet', 1); " +
                 "    else if (regex2.test(this.tweet) ) " +
                 "         emit('Book Tweet', 1); " +
                 "    else " +
                 "       emit('Uncategorized Tweet', 1); " +
                 "}";

    String reduce = "function(key, values) { " +
                    "    return Array.sum(values); " +
                    "}";

    MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce,
             null, MapReduceCommand.OutputType.INLINE, null);
    MapReduceOutput out = collection.mapReduce(cmd);

    try {
        for (DBObject o : out.results()) {

            System.out.println(o.toString());

       }
    } catch (Exception e) {
        e.printStackTrace();
    }    

这篇关于查询MongoDB Map Reduce函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆