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 pathpr.cpp
executable file
·78 lines (59 loc) · 1.85 KB
/
pr.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
#include <algorithm>
#include <cstdio>
#include <limits>
#include <string>
#include "stdint.h"
#include "Pipes.hh"
#include "TemplateFactory.hh"
#include "StringUtils.hh"
#include "utils.hh"
using namespace std;
class PageRankMapper : public HadoopPipes::Mapper {
public:
PageRankMapper( HadoopPipes::TaskContext& context ) {}
void map( HadoopPipes::MapContext& context ) {
string line = context.getInputValue();
vector< string > params =
HadoopUtils::splitString( line, "\t " );
string user = params[0];
double rank = HadoopUtils::toFloat(params[1]);
if ( params[2] != string("-") ) {
vector< string > followings =
HadoopUtils::splitString( params[2], "," );
double P = rank / followings.size();
for (unsigned i = 0; i < followings.size(); ++i)
if ( followings[i].length() > 0 ) {
context.emit( followings[i], doubleToString(P) );
}
}
context.emit( user, params[1] + " " + params[2] );
}
};
class PageRankReducer : public HadoopPipes::Reducer {
private:
static double D;
public:
PageRankReducer(HadoopPipes::TaskContext& context) {}
void reduce( HadoopPipes::ReduceContext& context ) {
double rank = 0.0;
string followings;
while ( context.nextValue() ) {
vector< string > line =
HadoopUtils::splitString( context.getInputValue(), " " );
if ( line.size() == 1 ) {
rank += HadoopUtils::toFloat(line[0]);
} else {
followings = line[1];
}
}
rank = D * rank + 1 - D;
context.emit(context.getInputKey(),
doubleToString(rank) + " " + followings );
}
};
double PageRankReducer::D = 0.85;
int main(int argc, char *argv[]) {
return HadoopPipes::runTask(HadoopPipes::TemplateFactory<
PageRankMapper,
PageRankReducer>() );
}