Query in a MongoDB Map Reduce Function

后端 未结 2 822
北海茫月
北海茫月 2020-12-21 20:04

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.

Mon         


        
2条回答
  •  暖寄归人
    2020-12-21 20:15

    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();
        }    
    

提交回复
热议问题