【问题标题】:Get Retweets Count from user timeline从用户时间线获取转推数
【发布时间】:2016-07-29 12:24:13
【问题描述】:

我使用 twitter api 'statuses/user_timeline' 将我自己的 twitter tweets 集合存储在 mongodb 中。我正在尝试获取我使用 MongoDb MapReduce 方法在我发布的推文中获得的 转推 计数,但无法获得它。谁能帮帮我。

示例数据:这是存储在 mongodb 中的文档格式

{
    "_id" : ObjectId("570664d7a9c29761168b4587"),
    "created_at" : "Thu Sep 17 01:17:28 +0000 2015",
    "id" : NumberLong("644319222886039556"),
    "id_str" : "644319222886039556",
    "text" : "Be silent or let your words be worth more than you silence.",
    "entities" : {
        "hashtags" : [ ],
        "symbols" : [ ],
        "user_mentions" : [ ],
        "urls" : [ ]
    },
    "truncated" : false,
    "source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "in_reply_to_screen_name" : null,
    "user" : {
        // Here is the user information who tweeted
        "id" : NumberLong(xxxxxxxxxxxxxxxxx),
        "id_str" : "xxxxxxxxx",
        "name" : "Haridarshan Gorana",
        "screen_name" : "haridarshan2901"
    },
    "geo" : null,
    "coordinates" : null,
    "place" : null,
    "contributors" : null,
    "is_quote_status" : false,
    "retweet_count" : NumberLong(1),
    "favorite_count" : NumberLong(0),
    "favorited" : false,
    "retweeted" : false,
    "lang" : "en"
}

代码:

$map = new \MongoCode("function() { emit(this.id_str, this.retweet_count); }");
$out = "retweets";
$reduce = new \MongoCode('function(key, values) {
    var retweets = 0;
    for(i=0;i<values.length;i++){

        if( values[i].retweet_count > 0 ){
            retweets += values[i].retweet_count;
        }

    }
    return retweets;
}');
$verbose = true;
$cmd = array(
    "map" => $map,
    "reduce" => $reduce,
    "query" => $query,
    "out" => "retweets",
    "verbose" => true
);

$result = $db->command($cmd);

print_r($result);

这给了我这个错误

致命错误:在 null 上调用成员函数 command()

我尝试在 mongo 客户端上运行相同的代码

var mapFunction1 = function() {
    emit(this.id_str, this.retweet_count);
}

var reduceFunction1 = function(id, values) { 
    var retweet = 0; 
    for(i=0;i<values.length;i++){ 
        if(values[i].retweet_count > 0) { 
            retweet += values[i].retweet_count;
        } 
    } 
    return retweet;  
}

db.tweets.mapReduce(
    mapFunction1, 
    reduceFunction1, 
    {
        query: { 
            user: { id: xxxxxxxxx }
        }, 
        out: "retweets", 
        verbose: true
    }
)

控制台输出

{
    "result" : "retweets",
    "timeMillis" : 12,
    "timing" : {
        "mapTime" : 0,
        "emitLoop" : 8,
        "reduceTime" : 0,
        "mode" : "mixed",
        "total" : 12
    },
    "counts" : {
        "input" : 0,
        "emit" : 0,
        "reduce" : 0,
        "output" : 0
    },
    "ok" : 1
}

【问题讨论】:

  • 您能否在edit 您的帖子中包含一些示例文档和您的预期结果,以显示您希望通过 MapReduce 操作实现的目标?我有一种强烈的感觉,您的问题可以使用聚合框架轻松解决,并具有一些出色的性能增强。

标签: javascript mongodb mapreduce mongodb-query aggregation-framework


【解决方案1】:

你的 reducer 正试图调用一个属性 retweet_count,而此时只有一个“值”而没有其他属性。您已经在映射器中引用了它。

其实你的 reduce 可以很简单:

function(key,values) {
    return Array.sum(values)
}

但您最好使用.aggregate() 来实现这一点。它不仅更简单,而且运行速度更快:

db.tweets.aggregate([
  { "$group": {
    "_id": "$user.id_str",
    "retweets": { "$sum": "$retweet_count" }
  }}
])

或者对于 PHP

$collection->aggregate(
    array(
        '$group' => array(
           '_id' => '$user.id_str',
           'retweets' => array( '$sum' => '$retweet_count' )
        )
    )
)

如果您想向其中添加“查询”,请在开头添加 $match 管道阶段。即

$collection->aggregate(
    array(
        '$match' => array(
            'user.id_str' => 'xxxxxxxxx'
        )
    ),    
    array(
        '$group' => array(
           '_id' => '$user.id_str',
           'retweets' => array( '$sum' => '$retweet_count' )
        )
    )
)

你真的应该只在结构实际需要 JavaScript 控件进行处理时才使用mapReduce

【讨论】:

  • 感谢您的帮助。我以前从未与 Mongo 合作过,所以你能告诉我如果我收集了多个用户的推文,并且想在只有一个用户的推文上使用这种聚合方法,我将如何使用这种方法。在用户对象的示例推文中,我确实有 user idtwitter screen_name
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 2012-11-16
  • 2012-12-18
  • 2018-11-23
相关资源
最近更新 更多