现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1。

buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割,样本数据及格式如下:

买家id   商品id    收藏日期

10181   1000481   2010-04-04 16:54:31

20001   1001597   2010-04-07 15:07:52

20001   1001560   2010-04-07 15:08:27

20042   1001368   2010-04-08 08:20:30

20067   1002061   2010-04-08 16:45:33

20056   1003289   2010-04-12 10:50:55

20056   1003290   2010-04-12 11:57:35

20056   1003292   2010-04-12 12:05:29

20054   1002420   2010-04-14 15:24:12

20055   1001679   2010-04-14 19:46:04

20054   1010675   2010-04-14 15:23:53

20054   1002429   2010-04-14 17:52:45

20076   1002427   2010-04-14 19:35:39

20054   1003326   2010-04-20 12:54:44

20056   1002420   2010-04-15 11:24:49

20064   1002422   2010-04-15 11:35:54

20056   1003066   2010-04-15 11:43:01

20056   1003055   2010-04-15 11:43:06

20056   1010183   2010-04-15 11:45:24

20056   1002422   2010-04-15 11:45:49

20056   1003100   2010-04-15 11:45:54

20056   1003094   2010-04-15 11:45:57

20056   1003064   2010-04-15 11:46:04

20056   1010178   2010-04-15 16:15:20

20076   1003101   2010-04-15 16:37:27

20076   1003103   2010-04-15 16:37:05

20076   1003100   2010-04-15 16:37:18

20076   1003066   2010-04-15 16:37:31

20054   1003103   2010-04-15 16:40:14

20054   1003100   2010-04-15 16:40:16

要求编写MapReduce程序,统计每个买家收藏商品数量。

统计结果数据如下:

  1. 买家id 商品数量
  2. 10181   1
  3. 20001   2
  4. 20042   1
  5. 20054   6
  6. 20055   1
  7. 20056   12
  8. 20064   1
  9. 20067   1
  10. 20076   5
package mapreduce;
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance();
job.setJobName("WordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(doMapper.class);
job.setReducerClass(doReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
Path in = new Path("hdfs://localhost:9000/mymapreduce1/in/buyer_favourite9");
Path out = new Path("hdfs://localhost:9000/mymapreduce1/out");
FileInputFormat.addInputPath(job, in);
FileOutputFormat.setOutputPath(job, out);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
public static class doMapper extends Mapper<Object, Text, Text, IntWritable>{
public static final IntWritable one = new IntWritable(1);
public static Text word = new Text();
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer tokenizer = new StringTokenizer(value.toString(), "   ");
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
result.set(sum);
context.write(key, result);
}
}
}

实验截图:

大型数据库技术实验六 实验6:Mapreduce实例——WordCount的更多相关文章

  1. 实验6:Mapreduce实例——WordCount

          实验目的1.准确理解Mapreduce的设计原理2.熟练掌握WordCount程序代码编写3.学会自己编写WordCount程序进行词频统计实验原理MapReduce采用的是“分而治之”的 ...

  2. Mapreduce实例——WordCount

    实验步骤 切换目录到/apps/hadoop/sbin下,启动hadoop. cd /apps/hadoop/sbin ./start-all.sh 2.在linux上,创建一个目录/data/map ...

  3. 实验六 DIV+CSS的综合应用

    实验六 DIV+CSS的综合应用 [实验目的] 1.掌握DIV布局的方法: 2.利用CSS对DIV进行美化: 3.利用CSS对文本图像等网页元素进行美化 [实验环境] 连接互联网的PC ,Win7操作 ...

  4. VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存

                                                                          VMware vSphere服务器虚拟化实验六 vCente ...

  5. 实验六 CC2530平台上P2P通信的TinyOS编程

    实验六 CC2530平台上P2P通信的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步的掌握射频通信TinyOS编程方法 学生通过本实验应理解TinyOS中 ...

  6. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验六:数码管模块

    实验六:数码管模块 有关数码管的驱动,想必读者已经学烂了 ... 不过,作为学习的新仪式,再烂的东西也要温故知新,不然学习就会不健全.黑金开发板上的数码管资源,由始至终都没有改变过,笔者因此由身怀念. ...

  7. 实验 六:分析linux内核创建一个新进程的过程

    实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...

  8. Linux内核分析实验六

    Linux内核分析实验六 进程控制块PCB——task_struct(进程描述符) 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_s ...

  9. 20145314郑凯杰《网络对抗技术》可选实验 shellcode注入与Return-to-libc攻击实验

    20145314郑凯杰<网络对抗技术>可选实验 shellcode注入与Return-to-libc攻击实验 1.0 实践内容 Return-to-libc攻击是一种特殊的缓冲区溢出攻击, ...

随机推荐

  1. MySql数据库精简与绿色启动

    1.下载MYSQL的zip包,解压ZIP包 版本低的相对需要的空间少,最好能在mysql-5.6以下,我测试的最高5.6版本为mysql-5.6.46,主要是里面有my.ini文件,高于5.6的版本里 ...

  2. scons自动化构建工具

    方式一 可以官方下载,安装使用 方式二 使用 RT-Thread env工具,其中集成了scons工具 env工具配置 打开设置 添加到右键菜单 使用scons生成mdk5工程 > scons ...

  3. 1、SSH无密码访问

    1.在需要无密码登录远程服务器的机器上(如A→B服务器)生成密码对 A:服务器操作: ssh-keygen -t rsa :输出的内容直接一路回车即可(enter) 执行上面一步,会在~/.ssh目录 ...

  4. 使用 setTimeout 来模拟一个 setInterval

    setTimeout 超时调用:在多少时间 在执行: setinterval 每隔多少时间 就调用 例如: setTimeout这个的值是1000,也就是说在页面刷新后,1000毫秒之后才调用这个函数 ...

  5. nodejs爬虫--抓取CSDN某用户全部文章

    最近正在学习node.js,就像搞一些东西来玩玩,于是这个简单的爬虫就诞生了. 准备工作 node.js爬虫肯定要先安装node.js环境 创建一个文件夹 在该文件夹打开命令行,执行npm init初 ...

  6. idea git pull fatal: bad config line 1 in file /.gitconfig 问题处理

    在网上搜好多都是直接改username和useremail的,但是没有说明原理. 因为我的电脑是新入职接手上一家的电脑 后来在git bash 里面用$ git config user.name 原来 ...

  7. Java设计模式之Iterator

    public interface Aggregate { //调用iterator方法生成实现Iterator接口的类的实例 public abstract Iterator iterator(); ...

  8. MySQL第六课

    SELECT [DISTINCT]     * /{字段名1,字段名2,字段名3,.........} FROM 表名 [WHERE 条件表达式1] [GROUP BY 字段名[HAVING 条件表达 ...

  9. [HNOI2016]网络 [树链剖分,可删除堆]

    考虑在 |不在| 这条链上的所有点上放上一个 \(x\),删除也是,然后用可删除堆就随便草掉了. // powered by c++11 // by Isaunoya #pragma GCC opti ...

  10. Wilson's Theorem

    ProofsSuppose first that $p$ is composite. Then $p$ has a factor $d > 1$ that is less than or equ ...