MapReduce排序

系统环境
Linux Ubuntu 16.04

jdk-7u75-linux-x64

hadoop-2.6.0-cdh5.4.5

hadoop-2.6.0-eclipse-cdh5.4.5.jar

eclipse-java-juno-SR2-linux-gtk-x86_64

任务内容
在电商网站上,当我们进入某电商页面里浏览商品时,就会产生用户对商品访问情况的数据 ,名为goods_visit1,goods_visit1中包含(商品id ,点击次数)两个字段,内容以“\t”分割,由于数据量很大,所以为了方便统计我们只截取它的一部分数据,内容如下:

view plain copy
商品id 点击次数
1010037 100
1010102 100
1010152 97
1010178 96
1010280 104
1010320 103
1010510 104
1010603 96
1010637 97
要求我们编写mapreduce程序来对商品点击次数有低到高进行排序。

实验结果数据如下:

view plain copy
点击次数 商品ID
96 1010603
96 1010178
97 1010637
97 1010152
100 1010102
100 1010037
103 1010320
104 1010510
104 1010280
任务步骤
1.切换到/apps/hadoop/sbin目录下,开启Hadoop。

view plain copy
cd /apps/hadoop/sbin
./start-all.sh
2.在Linux本地新建/data/mapreduce3目录。

view plain copy
mkdir -p /data/mapreduce3
3.在Linux中切换到/data/mapreduce3目录下,用wget命令从http://192.168.1.100:60000/allfiles/mapreduce3/goods_visit1网址上下载文本文件goods_visit1。

view plain copy
cd /data/mapreduce3
wget http://192.168.1.100:60000/allfiles/mapreduce3/goods_visit1
然后在当前目录下用wget命令从http://192.168.1.100:60000/allfiles/mapreduce3/hadoop2lib.tar.gz网址上下载项目用到的依赖包。

view plain copy
wget http://192.168.1.100:60000/allfiles/mapreduce3/hadoop2lib.tar.gz
将hadoop2lib.tar.gz解压到当前目录下。

view plain copy
tar zxvf hadoop2lib.tar.gz
4.首先在HDFS上新建/mymapreduce3/in目录,然后将Linux本地/data/mapreduce3目录下的goods_visit1文件导入到HDFS的/mymapreduce3/in目录中。

view plain copy
hadoop fs -mkdir -p /mymapreduce3/in
hadoop fs -put /data/mapreduce3/goods_visit1 /mymapreduce3/in
5.新建Java Project项目,项目名为mapreduce3。

在mapreduce3项目下新建包,包名为mapreduce。

在mapreduce包下新建类,类名为OneSort。

6.添加项目所需依赖的jar包,右键单击项目新建一个文件夹,名为hadoop2lib,用于存放项目所需的jar包。

将/data/mapreduce3目录下hadoop2lib文件夹中的所有jar包,拷贝到eclipse中mapreduce3项目的hadoop2lib目录下。

选中hadoop2lib目录下所有jar包,单击右键,选择Build Path→Add to Build Path。

7.编写Java代码,并描述其设计思路

在MapReduce过程中默认就有对数据的排序。它是按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce会按照数字大小对key排序,如果Key为封装String的Text类型,那么MapReduce将按照数据字典顺序对字符排序。在本例中我们用到第一种,key设置为IntWritable类型,其中MapReduce程序主要分为Map部分和Reduce部分。

Map部分代码

view plain copy
public static class Map extends Mapper<Object,Text,IntWritable,Text>{
private static Text goods=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line=value.toString();
String arr[]=line.split("\t");
num.set(Integer.parseInt(arr[1]));
goods.set(arr[0]);
context.write(num,goods);
}
}
在map端采用Hadoop默认的输入方式之后,将输入的value值用split()方法截取,把要排序的点击次数字段转化为IntWritable类型并设置为key,商品id字段设置为value,然后直接输出<key,value>。map输出的<key,value>先要经过shuffle过程把相同key值的所有value聚集起来形成<key,value-list>后交给reduce端。

Reduce部分代码

view plain copy
public static class Reduce extends Reducer<IntWritable,Text,IntWritable,Text>{
private static IntWritable result= new IntWritable();
//声明对象result
public void reduce(IntWritable key,Iterable values,Context context) throws IOException, InterruptedException{
for(Text val:values){
context.write(key,val);
}
}
}
reduce端接收到<key,value-list>之后,将输入的key直接复制给输出的key,用for循环遍历value-list并将里面的元素设置为输出的value,然后将<key,value>逐一输出,根据value-list中元素的个数决定输出的次数。

完整代码

view plain copy
package mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class OneSort {
public static class Map extends Mapper<Object , Text , IntWritable,Text >{
private static Text goods=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line=value.toString();
String arr[]=line.split("\t");
num.set(Integer.parseInt(arr[1]));
goods.set(arr[0]);
context.write(num,goods);
}
}
public static class Reduce extends Reducer< IntWritable, Text, IntWritable, Text>{
private static IntWritable result= new IntWritable();
public void reduce(IntWritable key,Iterable values,Context context) throws IOException, InterruptedException{
for(Text val:values){
context.write(key,val);
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf=new Configuration();
Job job =new Job(conf,“OneSort”);
job.setJarByClass(OneSort.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in=new Path(“hdfs://localhost:9000/mymapreduce3/in/goods_visit1”);
Path out=new Path(“hdfs://localhost:9000/mymapreduce3/out”);
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true) ? 0 : 1);

    }  
    }  

8.在OneSort类文件中,右键并点击=>Run As=>Run on Hadoop选项,将MapReduce任务提交到Hadoop中。

9.待执行完毕后,进入命令模式下,在HDFS上/mymapreduce3/out中查看实验结果。

view plain copy
hadoop fs -ls /mymapreduce3/out
hadoop fs -cat /mymapreduce3/out/part-r-00000

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值