现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品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. p2114 起床困难综合症

    传送门 分析 orz zwj 最好想到的方法是我们枚举每一位是0还是1,然后暴力求出经过n个操作之后的结果来决定这一位是0还是1 然后我们发现这种暴力的做法居然能a 但是还有更好的方法 我们只考虑开始 ...

  2. SDUT 3376 数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...

  3. Java 子类初始化过程

    //父类 class Animal{ private String name; private int age; Animal(String name, int age) {//创建父类构造器 sup ...

  4. [转]MySQL时间与字符串相互转换

    转至:https://www.cnblogs.com/wangyongwen/p/6265126.html 时间.字符串.时间戳之间的互相转换很常用,但是几乎每次使用时候都喜欢去搜索一下用法:本文整理 ...

  5. c# XML增删改查

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. SqlServer压缩数据库日志

    )--数据库名称 )--数据库日志文件名称 --替换成自己的文件名称 select @dbName='dbname' select @dbNamelog='dbname_log' ) set @sql ...

  7. Git配置代理命令

    针对***的代理配置 设置代理 git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.p ...

  8. 生成全局唯一ID

    在实际业务处理中,有时需要生成全局唯一ID来区别同类型的不同事物,介绍一下几种方式及其C++实现 //获取全局唯一ID //server_id为服务的id,因当同一个服务部署在多个服务器上时,需要区别 ...

  9. See Elevator Run Floors

    “在我短暂的电梯作业中我发现,架构的优化能力是有限的.越是工于优化算法…越是会被自己的架构所制约….想要更好的优化,唯有超越架构………" 零.基础 优化建立在架构之上,这句话莫得问题,也莫得 ...

  10. 咕咕(数位dp+AC自动机)

    咕咕(数位dp+AC自动机) 若一个字符串的字符集合是0~m-1,那么称它为m进制字符串.给出n个m进制字符串\(s_i\),每个字符串的权值为\(v_i\).对于另一个m进制字符串\(S\),设\( ...