約 25 万件のツイートをストリーミングして MongoDB に保存しました。ご覧のとおり、ツイートに含まれる単語またはキーワードに基づいて取得しています。
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 関数に渡して、各カテゴリのツイート数をカウントすることです。この例では、単純な数字であるため、彼はページ数を数えています。私は次のようなことをしたい:
"if (this.tweet.contains("kword1")) "+
"category = 'kword1 tweets'; " +
"else if (this.tweet.contains("kword2")) " +
"category = 'kword2 tweets';
次に、サンプル プログラムと同様に、reduce 関数を使用してカウントを取得します。
構文が正しくないことはわかっていますが、それが私がやりたいことのほとんどです。それを達成する方法はありますか?ありがとう!
PS: ああ、私は Java でコーディングしています。したがって、Java 構文は高く評価されます。ありがとうございました!
投稿されたコードの出力は次のようになります。
{ "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 ;)"}
もちろん、これは「自伝」という単語を含むすべてのツイートに適用されます。私が望むのは、これをマップ関数で使用し、それを「自伝ツイート」(および他のキーワードも) として分類してから、reduce 関数に送信してすべてをカウントし、単語が含まれるツイートの数を返すことです。それ。
何かのようなもの:
{"_id" : "Autobiography Tweets" , "value" : { "publicTweets" : 3.0}}
{"_id" : "Biography Tweets" , "value" : { "publicTweets" : 15.0}}