1

約 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}}
4

2 に答える 2

7

次のことを試してみてください。

    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();
    }    
于 2012-12-06T03:16:30.047 に答える
5

Kay の回答を既に受け入れており、これは無視される可能性がありますが、別の解決策を提案したいと思います。

Th MongoDB のドキュメントには、 Mongo で全文検索を実行する方法に関する記事があります。テキストベースのフィールドで個々の単語をすばやく検索できるようにするために、テキストフィールドを個々の単語の配列に分割してドキュメントを準備し、これらの配列を全文と共にドキュメントに保存し、これにインデックスを作成することを提案しています。配列。

その後、検索クエリで 1. インデックスを使用でき、2. 正規表現 (非常にコストがかかる可能性がある) を使用する必要がないため、特定の単語を含むすべてのドキュメントを非常に迅速に見つけることができます。

于 2012-12-06T11:15:17.603 に答える