public void build() {
init();
LazyPlume plume = new LazyPlume();
// Get input files
PCollection inputEvent2, inputLogFile;
try {
inputEvent2 = plume.readFile(inputPathEvent2, collectionOf(strings()));
inputLogFile = plume.readFile(inputPathLogFile, collectionOf(strings()));
// Add as inputs
addInput(inputEvent2);
addInput(inputLogFile);
} catch (IOException e) {
throw new RuntimeException();
}
/**
* We use flatten to aggregate one log file we have with a list of users that used one new event.
* The list of users is converted to the log format before flattening by adding a date and a event name.
*/
PCollection aggregateLog = plume.flatten(
inputLogFile,
inputEvent2.map(new DoFn<Text, Text>() {
@Override
public void process(Text v, EmitFn emitter) {
emitter.emit(new Text(new SimpleDateFormat("yyyy/MM/dd").format(new Date())+"\t"+"new_event"+"\t"+v.toString()));
}
}, collectionOf(strings())));
/**
* We use the aggregate log to calculate a map of [date, user] -> #clicks
*/
PCollection dateUserClicks = aggregateLog.map(new DoFn<Text, Pair>() {
@Override
public void process(Text v, EmitFn<Pair> emitter) {
String[] splittedLine = v.toString().split("\t");
Text dateUser = new Text(splittedLine[0]+"\t"+splittedLine[2]);
emitter.emit(Pair.create(dateUser, new IntWritable(1)));
}
}, tableOf(strings(), integers()))
.groupByKey()
.combine(countCombiner)
.map(countReduceToText, tableOf(strings(), strings()));
/**
* We use the aggregate log to calculate a map of [date] -> #clicks
*/
PCollection dateClicks = aggregateLog.map(new DoFn<Text, Pair>() {
@Override
public void process(Text v, EmitFn<Pair> emitter) {
String[] splittedLine = v.toString().split("\t");
emitter.emit(Pair.create(new Text(splittedLine[0]), new IntWritable(1)));
}
}, tableOf(strings(), integers()))
.groupByKey()
.combine(countCombiner)
.map(countReduceToText, tableOf(strings(), strings()));
/**
* We use the aggregate log to calculate a list of uniq users
*/
PCollection uniqUsers = aggregateLog.map(new DoFn<Text, Pair>() {
@Override
public void process(Text v, EmitFn<Pair> emitter) {
String[] splittedLine = v.toString().split("\t");
emitter.emit(Pair.create(new Text(splittedLine[2]), new Text("")));
}