Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:26:20+00:00 2026-06-15T17:26:20+00:00

I have streamed and saved about 250k tweets into MongoDB and here, I am

  • 0

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

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'; 

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: 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 ;)"}

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.

Something like:

{"_id" : "Autobiography Tweets" , "value" : { "publicTweets" : 3.0}}
{"_id" : "Biography Tweets" , "value" : { "publicTweets" : 15.0}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:26:21+00:00Added an answer on June 15, 2026 at 5:26 pm

    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();
        }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have all my data on a server be streamed into
I am doing a streamed musicplayer for android. I have tried with MediaPlayer mp.setDataSource(http://www.myServerPage.com/songUrl.mp3);
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an Android application that plays streamed audio from the Internet using the
I have configured a WCF service to transfer data on a streamed transfer mode.
I have many media elements in my application that are streamed via web. Sometimes
I have a stream of images to make a .flv video. I can make
I have some questions about wav files (located in virtual directory in IIS) and
My iPhone app plays streamed audio from the Internet and I have an UISlider
I have been researching on whether we can create an app which will play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.