现有一个某电商网站的数据文件,名为buyer_favorite1,记录了用户收藏的商品以及收藏的日期,文件buyer_favorite1中包含(用户id,商品id,收藏日期)三个字段,数据内容以“\t”分割,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下:

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

要求用Java编写MapReduce程序,根据商品id进行去重,统计用户收藏商品中都有哪些商品被收藏。

源代码:

package mapreduce;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import mapreduce.WordCount.MyMapper;
import mapreduce.WordCount.MyReducer; public class Filter {
public static class Map extends Mapper<Object, Text, Text, NullWritable> {
private static Text newKey = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) {
String line = itr.nextToken();
String arr = line.substring(, line.indexOf("   "));
newKey.set(arr);
System.out.println(arr);
context.write(newKey, NullWritable.get()); }
} } public static class Reduce extends Reducer<Text, NullWritable, Text, NullWritable> {
public void reduce(Text key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException { context.write(key, NullWritable.get());
}
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
System.out.println("start");
Job job = new Job(conf, "filter");
job.setJarByClass(Filter.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in = new Path("hdfs://localhost:9000/mymapreduce2/in/buyer_favorite1");
Path out = new Path("hdfs://localhost:9000/mymapreduce2/out"); FileInputFormat.addInputPath(job, in);
FileOutputFormat.setOutputPath(job, out);
System.exit(job.waitForCompletion(true) ? : );
}
}

统计数据:


买家id

遇到的问题:

1.这次代码和上次代码很相似,所以这次代码石油上次代码复制粘贴过来改了一下。但是忘了该main函数中"job.setJarByClass(Filter.class);job.setMapperClass(Map.class);job.setReducerClass(Reduce.class);"。所以一直运行的是上次写的代码。

后来改了过来。

mapreduce去重的更多相关文章

  1. mapreduce学习指导及疑难解惑汇总

    原文链接http://www.aboutyun.com/thread-7091-1-1.html 1.思想起源: 我们在学习mapreduce,首先我们从思想上来认识.其实任何的奇思妙想,抽象的,好的 ...

  2. 零基础学习hadoop到上手工作线路指导初级篇:hive及mapreduce

      此篇是在零基础学习hadoop到上手工作线路指导(初级篇)的基础,一个继续总结.五一假期:在写点内容,也算是总结.上面我们会了基本的编程,我们需要对hadoop有一个更深的理解:hadoop分为h ...

  3. Hadoop 入门

    我看过的比较全的文章.赞一下 原文链接:http://www.aboutyun.com/thread-8329-1-1.html 问题导读: 1.hadoop编程需要哪些基础?2.hadoop编程需要 ...

  4. 零基础学习hadoop到上手工作线路指导(编程篇)

    问题导读: 1.hadoop编程需要哪些基础? 2.hadoop编程需要注意哪些问题? 3.如何创建mapreduce程序及其包含几部分? 4.如何远程连接eclipse,可能会遇到什么问题? 5.如 ...

  5. 零基础学习hadoop到上手工作线路指导(中级篇)

    此篇是在零基础学习hadoop到上手工作线路指导(初级篇)的基础,一个继续总结. 五一假期:在写点内容,也算是总结.上面我们会了基本的编程,我们需要对hadoop有一个更深的理解: hadoop分为h ...

  6. hadoop1.0.3学习笔记

    回 到 目 录 最近要从网上抓取数据下来,然后hadoop来做存储和分析. 呆毛王赛高 月子酱赛高 小唯酱赛高 目录 安装hadoop1.0.3 HDFS wordcount mapreduce去重 ...

  7. [Hadoop]-从数据去重认识MapReduce

    这学期刚好开了一门大数据的课,就是完完全全简简单单的介绍的那种,然后就接触到这里面最被人熟知的Hadoop了.看了官网的教程[吐槽一下,果然英语还是很重要!],嗯啊,一知半解地搭建了本地和伪分布式的, ...

  8. Hadoop阅读笔记(二)——利用MapReduce求平均数和去重

    前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...

  9. MapReduce应用案例--简单的数据去重

    1. 设计思路 去重,重点就是无论某个数据在文件中出现多少次,最后只是输出一次就可以. 根据这一点,我们联想到在reduce阶段数据输入形式是 <key, value list>,只要是k ...

随机推荐

  1. Windows系统 安装 CMake

    Windows系统 安装 CMake 我们的电脑系统:Windows 10 64位 安装的CMake 版本:cmake-3.6.1-win64-x64(目前最新) 下载 在CMake官网下载:cmak ...

  2. 547D Mike and Fish

    传送门 分析 见正睿10.3笔记 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...

  3. 关于UI Automation框架

    微软提供的UI Automation框架给开发windows平台的自动化测试带来了很大的便利,这里就总结一下相关的代码. 首先,直接使用UI Automation框架,完成一个NotePad的abou ...

  4. 别了,DjVu!

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2010.05.21 目录一.DjVu技术二.掌握DjVu技术的人三.玩DjVu的人四.小结跋:我与DjVu 谨以此文纪念与D ...

  5. UWP&WP8.1 基础控件——Image

    Image是UWP和WP8.1中系统自带的图片展示器. 具有较强的性能,使用也是非常的简单. 使用方式分为在XAML中,在C#代码中. XAML中: 在XAML中使用方式非常简单. 常用XAML So ...

  6. Django之博客系统搭建一

    前面已经介绍了django的各种用法,从这一章开始,将实际搭建一个blog系统. 首先我们需要设计blog的模型,在models.py中添加如下内容 # -*- coding: utf-8 -*- f ...

  7. position用法

    fixed的用法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. Codeforces Round #538 (Div. 2)D(区间DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){    int n ...

  9. [SinGuaRiTy] 2017-07-24 NOIP2015 模拟赛

    [SinGuLaRiTy-1030] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...

  10. Windows 下 Swoole开发环境配置

    一直停留在windows,入了 jetbrains 的全家桶.准备入门 Swoole,不可能每做一点修改就 git push 运行一下.因此要在 windows 上配置 swoole 运行环境.对比了 ...