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

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

买家id   商品id    收藏日期  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  
      -- ::  

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

源代码:

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 class MyMapper extends Mapper<Object,Text,Text,IntWritable>{
private final static IntWritable one = new IntWritable();
private static String word = new String();
public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()){ word=itr.nextToken();
System.out.println(word);
String id=word.substring(,word.indexOf("   "));
Text word2=new Text();
word2.set(id);
context.write(word2,one);
} }
} public static class MyReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,InterruptedException{
int sum = ;
for (IntWritable val : values)
{
sum += val.get();
}
result.set(sum); context.write(key,result);
}
} public static void main(String[] args) throws Exception{ Job job = Job.getInstance();
job.setJobName("WordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
Path in = new Path("hdfs://localhost:9000/mymapreduce1/in/buyer_favorite1") ;
Path out = new Path("hdfs://localhost:9000/mymapreduce1/out") ; FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true)?:);
} }

统计数据:


买家id    

mapreduce统计总数的更多相关文章

  1. MongoDb 用 mapreduce 统计留存率

    MongoDb 用 mapreduce 统计留存率(金庆的专栏)留存的定义采用的是新增账号第X日:某日新增的账号中,在新增日后第X日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...

  2. Hadoop基础-Map端链式编程之MapReduce统计TopN示例

    Hadoop基础-Map端链式编程之MapReduce统计TopN示例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.项目需求 对“temp.txt”中的数据进行分析,统计出各 ...

  3. bootstrap-table 页脚总计(自定义统计总数)

    •首先给table添加属性: showFooter: footer js代码如下: //初始化bootstrapTableinitBootstrapTable: function () { var o ...

  4. MySQL统计总数就用count(*),别花里胡哨的《死磕MySQL系列 十》

    有一个问题是这样的统计数据总数用count(*).count(主键ID).count(字段).count(1)那个效率高. 先说结论,不用那么花里胡哨遇到统计总数全部使用count(*). 但是有很多 ...

  5. 大家都在用MySQL count(*)统计总数,到底有什么问题?

    在日常开发工作中,我经常会遇到需要统计总数的场景,比如:统计订单总数.统计用户总数等.一般我们会使用MySQL 的count函数进行统计,但是随着数据量逐渐增大,统计耗时也越来越长,最后竟然出现慢查询 ...

  6. 针对微信的一篇推送附有的数据链接进行MapReduce统计

    原推送引用:https://mp.weixin.qq.com/s/3qQqN6qzQ3a8_Au2qfZnVg 版权归原作者所有,如有侵权请及时联系本人,见谅! 原文采用Excel进行统计数据,这里采 ...

  7. sql中奇怪的sum(1),sum(2),count(1),count(6),count(*):统计总数

    sql的统计函数 sql统计函数有 count 统计条数,配合group用 sum 累加指定字段数值 但注意sum(1)就特殊 sum(1)等同于count(*) sum(1)统计个数,功能和coun ...

  8. MapReduce -- 统计天气信息

    示例 数据: -- :: 34c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 45c -- :: 50c -- :: 33c -- :: 41c -- ...

  9. MySQL的统计总数count(*)与count(id)或count(字段)的之间的各自效率性能对比

    执行效果: 1.  count(1) and count(*) 当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了!  从执行计划来看,count(1)和cou ...

随机推荐

  1. R: 基本的数学运算

    ################################################### 问题:基本数学运算   18.4.30 R语言用于初等数学的计算,都怎么表示??加减乘除.余数. ...

  2. 196D The Next Good String

    传送门 题目大意 给定n和一个字符串,求一个新字符串使得这个字符串不存在长度大于等于n的回文子串且在字典序大于原串的情况下最小. 分析 我们知道如果有一个长度为n+2的回文串,那它一定由一个长度为n的 ...

  3. Ubuntu 切换到桌面 快捷键设置

    设置完以上步骤后,这接windows系统键+d,即可切换到桌面. ps:按Alt+Tab键,可以切换到自己想要的图标进程.

  4. WebGoat系列实验Injection Flaws

    WebGoat系列实验Injection Flaws Numeric SQL Injection 下列表单允许用户查看天气信息,尝试注入SQL语句显示所有天气信息. 选择一个位置的天气,如Columb ...

  5. 十三周作业—使用Metaspoit攻击MS08-067

    操作过程及结果如下:

  6. i++,++i,i+=1,i = i+1在C++中的区别

    其实这个问题可以从三个角度去分析:语言规范,编译器实现,CPU支持.首先从语言规范上来讲:前置++和后置++是不等价的,前置++在规范中明确指出 和+=组合操作符是等价的,但和E = E+1;这样的赋 ...

  7. PDG转PDF注定会文件膨胀、质量下降吗?

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2006.07.16更新:2006.07.20 事先声明: PDG文件是超星公司电子图书的专有格式,需要用超星公司的专用浏览器 ...

  8. 理解JavaScript普通函数以及箭头函数里使用的this

    this 普通函数的this 普通函数的this是由动态作用域决定,它总指向于它的直接调用者.具体可以分为以下四项: this总是指向它的直接调用者, 例如 obj.func() ,那么func()里 ...

  9. C++: I/O流详解(三)——串流

    一.串流 串流类是 ios 中的派生类 C++的串流对象可以连接string对象或字符串 串流提取数据时对字符串按变量类型解释:插入数据时把类型 数据转换成字符串 串流I/O具有格式化功能 例: #i ...

  10. poj3167(kmp)

    题目链接: http://poj.org/problem?id=3167 题意: 给出两串数字 s1, s2, 求主串 s1 中的 s2 匹配数并输出每个匹配的开头位置. 区间 [l, r] 是 s2 ...