一、知识准备

  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实现协同过滤中每个用户看过的项目集合的更多相关文章

  1. 共轭梯度法求解协同过滤中的 ALS

    协同过滤是一类基于用户行为数据的推荐方法,主要是利用已有用户群体过去的行为或意见来预测当前用户的偏好,进而为其产生推荐.能用于协同过滤的算法很多,大致可分为:基于最近邻推荐和基于模型的推荐.其中基于最 ...

  2. 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐

    一.Mahout推荐算法简介 Mahout算法框架自带的推荐器有下面这些: l  GenericUserBasedRecommender:基于用户的推荐器,用户数量少时速度快: l  GenericI ...

  3. 协同过滤中的Grey Sheep问题

    寒神解释:某些用户的倾向性和品味没有一致性,比较散.因此在协同过滤这种算法里,没办法和某个group有很高的相似/一致度,推荐会失效. 我理解是寻找邻居时候计算得到的相似度和其他用户相似度都非常小,或 ...

  4. Music Recommendation System with User-based and Item-based Collaborative Filtering Technique(使用基于用户及基于物品的协同过滤技术的音乐推荐系统)【更新】

    摘要: 大数据催生了互联网,电子商务,也导致了信息过载.信息过载的问题可以由推荐系统来解决.推荐系统可以提供选择新产品(电影,音乐等)的建议.这篇论文介绍了一个音乐推荐系统,它会根据用户的历史行为和口 ...

  5. Slope one—个性化推荐中最简洁的协同过滤算法

    Slope One 是一系列应用于 协同过滤的算法的统称.由 Daniel Lemire和Anna Maclachlan于2005年发表的论文中提出. [1]有争议的是,该算法堪称基于项目评价的non ...

  6. 推荐系统-协同过滤在Spark中的实现

    作者:vivo 互联网服务器团队-Tang Shutao 现如今推荐无处不在,例如抖音.淘宝.京东App均能见到推荐系统的身影,其背后涉及许多的技术.本文以经典的协同过滤为切入点,重点介绍了被工业界广 ...

  7. 基于用户的协同过滤电影推荐user-CF python

    协同过滤包括基于物品的协同过滤和基于用户的协同过滤,本文基于电影评分数据做基于用户的推荐 主要做三个部分:1.读取数据:2.构建用户与用户的相似度矩阵:3.进行推荐: 查看数据u.data 主要用到前 ...

  8. MapRedcue的demo(协同过滤)

    MapRedcue的演示(协同过滤) 做一个关于电影推荐.你于你好友之间的浏览电影以及电影评分的推荐的协同过滤. 百度百科: 协同过滤简单来说是利用某兴趣相投.拥有共同经验之群体的喜好来推荐用户感兴趣 ...

  9. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

随机推荐

  1. @OBJC 和 DYNAMIC

    原文转载自:@OBJC 和 DYNAMIC 虽然说 Swift 语言的初衷是希望能摆脱 Objective-C 的沉重的历史包袱和约束,但是不可否认的是经过了二十多年的洗礼,Cocoa 框架早就烙上了 ...

  2. .net core API 统一拦截错误

    public override void OnActionExecuted(ActionExecutedContext context) { if (context.Exception != null ...

  3. Ultra-QuickSort

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  4. 【转】CSS Sprites教程大全(使用方法、工具介绍)

    什么是CSS Sprite CSS Sprite 又叫CSS精灵,是目前大型网站中经常运用的图片处理方式.它的原理很简单,将网站上零散的小图片(或图标)整合在一张大图上,再用CSS中“backgrou ...

  5. [MetaHook] Event Hook

    #include <metahook.h> struct event_hook_t { event_hook_t *next; char *name; void (*pfnEvent)(e ...

  6. BASE64 编码和解码

    依赖jar: import org.apache.commons.codec.binary.Base64; BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据,其目的是为了简化存储或传输 ...

  7. Theano2.1.6-基础知识之在thenao中的求导

    来自:http://deeplearning.net/software/theano/tutorial/gradients.html Derivatives in Theano 一.计算梯度 现在,让 ...

  8. ServiceStack 概念参考文摘

    摘自:http://www.cnblogs.com/woxpp/p/5010881.html ServiceStack 用于服务开发,可以为各种形式的网站.软件.APP等提供数据服务,可以提供REST ...

  9. 网络功能虚拟化(NFV)

    你造什么是网络功能虚拟化(NFV)吗? NFV将网络功能整合到行业标准的服务器.交换机和存储硬件上,提供了优化的虚拟化数据平面,NFV通过服务器上运行的软件让管理员取代传统物理网络设备,并降低成本.能 ...

  10. WinForm 程序加管理员权限

    在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表 ...