Я пишу функцию mapreduce, и сейчас у меня есть следующее:
function myMapper() {
//The mapper function is called with each document, which has the special name 'this'
//Emit a key-value pair:
var hashtag;
for (hashtag in this.entities.hashtags) {
//print(hashtag.text);
emit(hashtag.text, 1);
}
//emit(this.entities, 1);
}
function myReducer(key, values) {
//The reducer is called once for each key, and is passed an array
//containing all values corresponding to that key.
//Produce the desired result
return Array.sum( values );
}
db.tweets.mapReduce(myMapper, myReducer, { query: {}, out: "hashtags" })
db.hashtags.aggregate({$sort: {value: -1}})
Я вижу нулевой вывод из этого mapreduce, как показано ниже:
{ "_id" : null, "value" : 454540 }
Я знаю, что лица это поле, а хэштеги это массив, верно?Итак, почему этот код не работает?
{
"_id" : ObjectId("5aa58befc4214f42f4c7cc70"),
"created_at" : "Mon Apr 24 14:39:49 +0000 2017",
"id" : NumberLong("856518043605905409"),
"id_str" : "856518043605905409",
"text" : "RT @Sheena_Davis: Lots of activities for Safety week \nMay 1-6 dfghdfghd #SafeCityLondon #ldnont",
"source" : "<a href=\"bnmvnm" rel=\"nofollow\">Twitter Web Client</a>",
"truncated" : false,
"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" : {
"id" : 83649318,
"id_str" : "83649318",
"name" : "MLHealthUnit",
"screen_name" : "MLHealthUnit",
"location" : "London and Middlesex County",
"description" : "Official account of the Middlesex-London Health Unit │We promote and protect the health of our community. We’re HERE for YOU. #MLHU #LdnOnt",
"protected" : false,
"verified" : true,
"followers_count" : 9937,
"friends_count" : 3794,
"listed_count" : 257,
"favourites_count" : 6287,
"profile_link_color" : "008265",
"default_profile" : false,
"default_profile_image" : false,
"following" : null,
"follow_request_sent" : null,
"notifications" : null
},
"geo" : null,
"coordinates" : null,
"place" : null,
"contributors" : null,
"retweeted_status" : {
"created_at" : "Mon Apr 24 10:24:21 +0000 2017",
"id" : NumberLong("856453750915043328"),
"id_str" : "856453750915043328",
"text" : "Lots of activities for Safety week \nMay 1-6 ghjghj #SafeCityLondon #ldnont",
"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" : {
"id" : 144868126,
"id_str" : "144868126",
"name" : "Sheena Davis",
"screen_name" : "Sheena_Davis",
"location" : "London ON CA",
},
"entities" : {
"hashtags" : [
{
"text" : "SafeCityLondon",
"indices" : [
68,
83
]
},
{
"text" : "ldnont",
"indices" : [
84,
91
]
}
],
"urls" : [
{
"url" : "dfgdf.co",
"expanded_url" : "url.ckdkd",
"display_url" : "ghjghj",
"indices" : [
44,
67
]
}
],
"user_mentions" : [ ],
"symbols" : [ ]
},
"favorited" : false,
"retweeted" : false,
"possibly_sensitive" : false,
"filter_level" : "low",
"lang" : "en"
}
}
Я не самый опытный с javascript, но он довольно ограничен с точки зрения устранения неполадок mapreduce, кажется.
Обновление: я чувствую, что пыталсявсе на этом этапе.Честно говоря, это должен быть код сопоставления, но я не уверен, что это такое.
Вот некоторые из «отладочных» операций, которые я выполнял:
function myMapper() {
//The mapper function is called with each document, which has the special name 'this'
//Emit a key-value pair:
var hashtag;
/*for (hashtag in this.entities.hashtags[0]) {
//print(hashtag.text);
emit(hashtag[0][1], 1);
}*/
for (bop in this.entities) {
//print(hashtag.text);
//emit(bop, 1);
var myArr = JSON.parse(bop.hashtags);
for (ding in myArr) {
emit(ding, 1)
}
//emit(bop.hashtags[0], 1)
for (beep in bop.hashtags) {
//emit(beep, 1);
//emit(beep.hashtags, 1)
//emit(beep[0], 1)
/*for (thing in beep) {
emit(thing, 1);
}*/
}
}
//emit(this.entities, 1);
}