This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfin.cpp
executable file
·81 lines (63 loc) · 1.95 KB
/
fin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <limits>
#include <string>
#include <utility>
#include "stdint.h"
#include "Pipes.hh"
#include "TemplateFactory.hh"
#include "StringUtils.hh"
#include "utils.hh"
typedef pair< double, int > sortvalue;
class FinalMapper : public HadoopPipes::Mapper {
public:
FinalMapper( HadoopPipes::TaskContext& context ) {}
void map( HadoopPipes::MapContext& context ) {
string line = context.getInputValue();
vector< string > params =
HadoopUtils::splitString( line, "\t " );
context.emit( "0", params[0] + " " + params[1] );
}
};
class FinalReducer : public HadoopPipes::Reducer {
public:
FinalReducer( HadoopPipes::TaskContext& context ) {}
void reduce( HadoopPipes::ReduceContext& context ) {
vector< sortvalue > values;
while ( context.nextValue() ) {
vector< string > value =
HadoopUtils::splitString( context.getInputValue(), " " );
int user = HadoopUtils::toInt(value[0]);
double rank = HadoopUtils::toFloat(value[1]);
insert( values, make_pair( -rank, user ) );
}
sort( values.begin(), values.end(), greater< sortvalue >() );
for (unsigned i = 0, lim = 5; i < values.size() && lim > 0; ++i, --lim) {
emit( context, values[i] );
}
}
void insert( vector< sortvalue >& v, sortvalue val ) {
if (!v.size()) {
v.push_back(val);
return;
}
vector< sortvalue >::iterator it =
lower_bound(v.begin(), v.end(), val);
v.insert(it, val);
if (v.size() > 5) {
v.pop_back();
}
}
void emit( HadoopPipes::ReduceContext& context,
sortvalue value ) {
context.emit( HadoopUtils::toString( value.second ),
doubleToString( -value.first ) );
}
};
int main(int argc, char *argv[]) {
return HadoopPipes::runTask(HadoopPipes::TemplateFactory<
FinalMapper,
FinalReducer>() );
}