所谓购物篮分析主要是挖掘出用户感兴趣的数据组合,应用于电商,大型超市。比如京东推荐,购买了此用户的产品同时购买了XX产品, 浏览了此商品的用户同时也浏览了XX商品, 对于大型超市来说道理也一样,这样就可以把产品组合打包卖给有兴趣的人。 购物篮分析为推荐做好后台数据组合的工作,推荐系统根据这些组合来做推荐。购物篮分析当然远远不止于此,更多信息可以查看相关资料。
购物篮主要目标是形成对应的排列组合,比如我购买了A,B,C,D 4个产品,另外一个人购买了B,D,E,F, 那么如果按照两两组合,我买的产品有A,B,AC,AD,BC,BD,CD, 另外一个人购买了,BD,BE,BF,DE,DF,EF。 组合之后计算出现的频率,结果如下:
AB 1
AC 1
AD 1
BD 2
CD 1
BE 1
BF 1
DE 1
DF 1
EF 1
通过上面的结果,我们知道,B和D这2种产品的组合要比其他组合多,在做推荐的时候,我们就可以考虑推荐B和D一起卖给用户。
为了实现购物篮分析,先要完成排列组合,通过递归把每个购物清单两两组合在一起,然后发送给reduce,再做统计,就能得到我们要的结果。 因此通过 Mapreduce来完成购物篮分析,难点不在于map和reduce,而在于排列组合。
排列组合代码如下:
package com.isesol.mapreduce;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class findSortMBA {
public static List<List<String>> findsort(Collection<String> elements, int n) {
List result = new ArrayList<List>();
if(n == 0) {
result.add(new ArrayList());
return result;
}
List<List<String>> combinations = findsort(elements, n - 1);
for(List<String> combination : combinations) {
for(String element : elements) {
if(combination.contains(element)) {
continue;
}
List<String> list = new ArrayList<String>();
list.addAll(combination);
if(list.contains(element)) {
continue;
}
list.add(element);
Collections.sort(list);
if(result.contains(list)){
continue;
}
result.add(list);
}
}
return result;
}
}
Mapreduce代码统计如下:
package com.isesol.mapreduce;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapred.join.TupleWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import com.isesol.mapreduce.findSortMBA;
public class MarketBasketAnalysis {
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private Text data = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] val = value.toString().split(",");
List<String> list = convertToList(val);
List<List<String>> result = findSortMBA.findsort(list, 2);
// Tuple tuple2 = Tuple.<String, Integer>of("hello", 1);
for (List<String> tuple : result) {
context.write(new Text(tuple.toString()), new IntWritable(1));
}
}
}
public static List<String> convertToList(String[] val) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < val.length; i++) {
list.add(val[i]);
}
return list;
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable values : value) {
sum += 1;
}
context.write(new Text(key), new IntWritable(sum));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "MarketBasketAnalysis");
job.setJarByClass(MarketBasketAnalysis.class);
job.setMapperClass(TokenizerMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(1);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
测试数据如下:
a,b,c b,d,e a,d,e b,e,f c,d a,f结果:
[a, b] 1 [a, c] 1 [a, d] 1 [a, e] 1 [a, f] 1 [b, c] 1 [b, d] 1 [b, e] 2 [b, f] 1 [c, d] 1 [d, e] 2 [e, f] 1