现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品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. 204. Count Primes 统计<n的质数的个数

    [抄题]: Count the number of prime numbers less than a non-negative number, n. [暴力解法]: 时间分析: 空间分析: [优化后 ...

  2. R: 常用操作:

    ################################################### #清除所有变量: rm(list=ls()) #查看变量类型 getwd() setwd() i ...

  3. Linux shell实现阳历转农历

    闲来无事,想在Linux下用shell写一个阳历转农历的脚本,断断续续大概一个星期终于搞定.现在拿出来与大家分享. 缘由 本脚本实现原理是查表法(因为公式有误差):基于农历新年为基准,对农历新年前后两 ...

  4. Struts2 引入

    引入:   说:如果一个路径想访问一个类,需求怎么做? 第一种方法,用servlet 第二种方法,用过滤器 第三种方法如下图:把路径和方法都存到map里面,用反射可以执行类下的方法     第三中方法 ...

  5. jQuery 插件开发——countdown(倒计时)

    故事背景:按照惯例,先写下故事背景,其实也是再次汇报下最近开发情况的.做电商的都知道,这是个活动季啊,双十一.双十二.元旦....各种啊..其实说这么多就是想表达最近比较忙.呵呵^_^,正好今天抽点空 ...

  6. C# DLL(程序集)的生成和调用

    日期:2018年11月24日 环境:Window 10,VS2015 一.利用VS2015自带的工具生成DLL 步骤: 1.利用C#准备一个.cs文件: using System; public cl ...

  7. UIViewController函数调用顺序

    /*********** 0 执行1次而已 ******************/ + (void)load { NSLog(@" 0:%s", __func__); } /*** ...

  8. UIViewController+MJPopupViewController

    1.MJPopupBackgroundView 1.1 MJPopupBackgroundView.h // // MJPopupBackgroundView.h // watched // // C ...

  9. json解析(自动判断是jsonArray和jsonObject)

    因为想做一个接口自动化框架,已经实现了接口的访问和连接及获取接口返回的json数据,但json数据的解析是个麻烦的事情,所以写一个简单的版本记录一下.后续会进行优化,实现方法分离以及自动识别循环解析返 ...

  10. [SinGuLaRiTy] 二分图&匈牙利算法

    [SinGuLaRiTY-1019] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二分图 二分图是图论中一种特殊的图形.顾名思义,二分图G ...