MapReduce实现协同过滤中每个用户看过的项目集合
一、知识准备
hadoop自带的例子在
D:\HADOOP_HOME\hadoop-2.6.4\share\hadoop\mapreduce\sources\hadoop-mapreduce-examples 2.6.0-source.jar
我记得当年面试的时候就问中位数的问题不过是数据流下的中位数,一问便知是否搞过hadoop。
二、代码实现
2.1 Mapper
package cf; import java.io.IOException; import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class MovieMapper1 extends Mapper<LongWritable, Text, Text, Text> { public void map(LongWritable ikey, Text ivalue, Context context)
throws IOException, InterruptedException {
String[] values = ivalue.toString().split(",");
if (values.length!=2) {
return ;
}
String userID = values[0];
String itemID = values[1];
context.write(new Text(userID), new Text(itemID));
}
}
2.2 Reducer
package cf; import java.io.IOException; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class MovieReduce1 extends Reducer<Text, Text, Text, Text> { public void reduce(Text _key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
// process values
StringBuffer sb = new StringBuffer();
for (Text val : values) {
sb.append(val.toString());
sb.append(",");
}
//value不能直接用StringBuffer 必须转换为String
context.write(_key,new Text(sb.toString()));
} }
2.3 Main
package cf; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class UserItemSetMapReduce { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration();
Job job = new Job(conf, "CFItemSet");
job.setJarByClass(UserItemSetMapReduce.class);
job.setMapperClass(MovieMapper1.class);
//job.setCombinerClass(cls);
// job.setCombinerClass(MovieReduce1.class);
job.setReducerClass(MovieReduce1.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path("hdfs://192.168.58.180:8020/cf/userItem.txt"));
//InputPath(job, new Path(otherArgs[0]));
//直接写到cf会提示已存在cf,我写成uIO.ttx,以为内容会写入到txt,然没有,默认他是文件夹
FileOutputFormat.setOutputPath(job,new Path("hdfs://192.168.58.180:8020/cf/userItemOut.txt"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
三、结果分析
3.1 输入

3.2 输出

查看结果发现输出文件的分隔符默认是tab,‘\t’,同时相对于输入文件来说输出结果是逆着的,类似沾,莫非context就是这样的先进后出、
3.3日志分析
只列出了主要部分的日志
DEBUG - PrivilegedAction as:hxsyl (auth:SIMPLE) from:org.apache.hadoop.mapreduce.Job.getCounters(Job.java:765)
INFO - Counters: 38
File System Counters
FILE: Number of bytes read=538
FILE: Number of bytes written=509366
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=106
HDFS: Number of bytes written=37
HDFS: Number of read operations=13
HDFS: Number of large read operations=0
HDFS: Number of write operations=4
Map-Reduce Framework
Map input records=11
Map output records=11
Map output bytes=44
Map output materialized bytes=72
Input split bytes=107
Combine input records=0
Combine output records=0
Reduce input groups=5
Reduce shuffle bytes=72
Reduce input records=11
Reduce output records=5
Spilled Records=22
Shuffled Maps =1
Failed Shuffles=0
Merged Map outputs=1
GC time elapsed (ms)=3
CPU time spent (ms)=0
Physical memory (bytes) snapshot=0
Virtual memory (bytes) snapshot=0
Total committed heap usage (bytes)=462422016
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=53
File Output Format Counters
Bytes Written=37
DEBUG - PrivilegedAction as:hxsyl (auth:SIMPLE) from:org.apache.hadoop.mapreduce.Job.updateStatus(Job.java:323)
DEBUG - stopping client from cache: org.apache.hadoop.ipc.Client@37afeb11
DEBUG - removing client from cache: org.apache.hadoop.ipc.Client@37afeb11
DEBUG - stopping actual client because no more references remain: org.apache.hadoop.ipc.Client@37afeb11
DEBUG - Stopping client
DEBUG - IPC Client (521081105) connection to /192.168.58.180:8020 from hxsyl: closed
DEBUG - IPC Client (521081105) connection to /192.168.58.180:8020 from hxsyl: stopped, remaining connections 0
大神分析一下如何执行的,看着日志....Map如何输入的,执行几次等。
MapReduce实现协同过滤中每个用户看过的项目集合的更多相关文章
- 共轭梯度法求解协同过滤中的 ALS
协同过滤是一类基于用户行为数据的推荐方法,主要是利用已有用户群体过去的行为或意见来预测当前用户的偏好,进而为其产生推荐.能用于协同过滤的算法很多,大致可分为:基于最近邻推荐和基于模型的推荐.其中基于最 ...
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
一.Mahout推荐算法简介 Mahout算法框架自带的推荐器有下面这些: l GenericUserBasedRecommender:基于用户的推荐器,用户数量少时速度快: l GenericI ...
- 协同过滤中的Grey Sheep问题
寒神解释:某些用户的倾向性和品味没有一致性,比较散.因此在协同过滤这种算法里,没办法和某个group有很高的相似/一致度,推荐会失效. 我理解是寻找邻居时候计算得到的相似度和其他用户相似度都非常小,或 ...
- Music Recommendation System with User-based and Item-based Collaborative Filtering Technique(使用基于用户及基于物品的协同过滤技术的音乐推荐系统)【更新】
摘要: 大数据催生了互联网,电子商务,也导致了信息过载.信息过载的问题可以由推荐系统来解决.推荐系统可以提供选择新产品(电影,音乐等)的建议.这篇论文介绍了一个音乐推荐系统,它会根据用户的历史行为和口 ...
- Slope one—个性化推荐中最简洁的协同过滤算法
Slope One 是一系列应用于 协同过滤的算法的统称.由 Daniel Lemire和Anna Maclachlan于2005年发表的论文中提出. [1]有争议的是,该算法堪称基于项目评价的non ...
- 推荐系统-协同过滤在Spark中的实现
作者:vivo 互联网服务器团队-Tang Shutao 现如今推荐无处不在,例如抖音.淘宝.京东App均能见到推荐系统的身影,其背后涉及许多的技术.本文以经典的协同过滤为切入点,重点介绍了被工业界广 ...
- 基于用户的协同过滤电影推荐user-CF python
协同过滤包括基于物品的协同过滤和基于用户的协同过滤,本文基于电影评分数据做基于用户的推荐 主要做三个部分:1.读取数据:2.构建用户与用户的相似度矩阵:3.进行推荐: 查看数据u.data 主要用到前 ...
- MapRedcue的demo(协同过滤)
MapRedcue的演示(协同过滤) 做一个关于电影推荐.你于你好友之间的浏览电影以及电影评分的推荐的协同过滤. 百度百科: 协同过滤简单来说是利用某兴趣相投.拥有共同经验之群体的喜好来推荐用户感兴趣 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
随机推荐
- Java 中包装类wrapped type之间以及和primitive type的比较
注意, 包装类的实例之间比较, 是不能直接用 == 的 public static void main(String[] args) { // TODO Auto-generated method s ...
- 多个mapper location时, mybatis spring的自动扫描配置
1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...
- 丰富Easyui 的插件 - lookup
插件用途: 主要用于表单中,某字段的内容是用其他表里的记录ID.当然你可以使用combobox.combotree.combogrid等,但有时这些表现方式并不是很好,希望弹出个层,然后在去做一些查询 ...
- Java的注解(Annotation)
1.什么是注解 Annotation is code about the code, that is metadata about the program itself. Java注解,是Java5. ...
- 细细品味Storm_Storm简介及安装
Storm是由专业数据分析公司BackType开发的一个分布式实时数据处理软件,可以简单.高效.可靠地处理大量的数据流.Twitter在2011年7月收购该公司,并于2011年9月底正式将Storm项 ...
- 解决Kafka-1194问题
生产环境中使用Kafka作为日志处理的中间件,系统结构是这样的.自12月上线一个多月来,系统运行稳定. 用过kafka的都知道,Kafka产生的消息全部存储到硬盘文件中,并且在消息被消费后不会被立即删 ...
- swifttextfield代理方法
//MARK:textfield delegate //键盘的高度 func textFieldShouldBeginEditing(textField: UITextField) -> Boo ...
- DOM 概况
DOM(文档对象模型)是针对 HTML 和 XML 文档的一个API(应用程序编程接口).DOM 描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分. 层次节点 DOM可以将任何 H ...
- 开源--豆瓣小组UWP,已上架应用商店
1.前言 豆瓣小组是我和我老婆都比较喜欢的豆瓣家族里面的一款产品.平时加入了一些小组,偶尔打开看下新鲜的帖子,可以打发一下无聊的时间. 豆瓣小组UWP是我前几周在家里开发的一款windows 10应用 ...
- 网页上传图片 判断类型 检测大小 剪切图片 ASP.NET版本
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=56&extra=page%3D1 我们在网页上传图片的时候,特 ...