/
TransactionRecencyScore.java
144 lines (123 loc) · 5.31 KB
/
TransactionRecencyScore.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* visitante: Web analytic using Hadoop Map Reduce
* Author: Pranab Ghosh
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, softwarSessionSummarizere
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.visitante.customer;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.chombo.stats.ExponentialDistribution;
import org.chombo.stats.ProbabilityDistribution;
import org.chombo.stats.StandardNormalDistribution;
import org.chombo.util.Utility;
/**
* Converts transaction recency i.e, time since last transaction to a score based
* on normal distribution of time gap between transaction
* @author pranab
*
*/
public class TransactionRecencyScore extends Configured implements Tool {
private static String configDelim = ",";
@Override
public int run(String[] args) throws Exception {
Job job = new Job(getConf());
String jobName = "Transaction recency score";
job.setJobName(jobName);
job.setJarByClass(TransactionRecencyScore.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
Utility.setConfiguration(job.getConfiguration(), "visitante");
job.setMapperClass(TransactionRecencyScore.RecencyScoreMapper.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(0);
int status = job.waitForCompletion(true) ? 0 : 1;
return status;
}
/**
* @author pranab
*
*/
public static class RecencyScoreMapper extends Mapper<LongWritable, Text, NullWritable, Text> {
private Text outVal = new Text();
private String fieldDelimRegex;
private String[] items;
private double recency;
private double recencyScore;
private double globalAvTimeGap = -1.0;
private double globalStdDevTimeGap = -1.0;
private ProbabilityDistribution transGapDistr;
private static final int RECENCY_FIELD = 2;
/* (non-Javadoc)
* @see org.apache.hadoop.mapreduce.Mapper#setup(org.apache.hadoop.mapreduce.Mapper.Context)
*/
protected void setup(Context context) throws IOException, InterruptedException {
Configuration config = context.getConfiguration();
fieldDelimRegex = Utility.getFieldDelimiter(config, "trs.field.delim.regex", "field.delim.regex", ",");
//trans stats
List<String[]> transStats = Utility.parseFileLines(config, "trs.xaction.stats.file.path", "=");
for (String[] items : transStats) {
if (items[0].equals("globalAvTimeGap")) {
globalAvTimeGap = Double.parseDouble(items[1]);
} else if (items[0].equals("globalStdDevTimeGap")) {
globalStdDevTimeGap = Double.parseDouble(items[1]);
}
}
if (globalAvTimeGap < 0 && globalStdDevTimeGap < 0) {
throw new IllegalStateException("transaction time gap global stats not found");
}
//trans gap prob distr
String distrType = config.get("trs.trans.gap.prob.distr", "normal");
if (distrType.equals("normal")) {
transGapDistr = new StandardNormalDistribution(globalAvTimeGap, globalStdDevTimeGap);
} else if (distrType.equals("exponential")) {
transGapDistr = new ExponentialDistribution(globalAvTimeGap);
} else {
throw new IllegalArgumentException("invalid probability distribution");
}
}
/* (non-Javadoc)
* @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN, org.apache.hadoop.mapreduce.Mapper.Context)
*/
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
items = value.toString().split(fieldDelimRegex);
recency = Double.parseDouble(items[RECENCY_FIELD]);
recencyScore = 1.0 - transGapDistr.getDistr(recency);
items[RECENCY_FIELD] = Utility.formatDouble(recencyScore, 3);
outVal.set(Utility.join(items));
context.write(NullWritable.get(), outVal);
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new TransactionRecencyScore(), args);
System.exit(exitCode);
}
}