mapreduce实验:对txt文档实现排序和分区两个功能。要求按照年龄排序后自定义分区。
前言
随着大数据的不断发展,hadoop这门技术也越来越重要,很多人都开启了学习大数据,本文就如何在海量数据中进行分区排序提供了思路。在上一次实验,我们简单的完成了获取最值的程序设计。在本次实验中,我们需要完成分区的设计。
一、txt数据准备
1.python中有random和faker包(外部)给我们提供假的数据。我们使用python创建一个小型的txt文档,其中包括姓名,年龄,score(1分制)
2.本次实验要求:对txt文档实现排序和分区两个功能。要求按照年龄排序后自定义分区。
以下是创建的txt文档(按照\t分行):
rose 27 0.6270426084076096 lisa 27 0.7321873119280536 black 22 0.8129257516728405 jack 27 0.8328363834853498 rose 22 0.9685109182738815 black 30 0.7727688951446979 lisa 21 0.24477509026919908 black 25 0.8785838355517068 rose 30 0.8901461639299959 black 26 0.5460709396256507
二.
二、步骤
1.代码设计
- 创建StuBean对象继承WritableComparable<StuBean>并对其序列化。对象属性是age。
- mapper阶段对数据进行分割处理,context写入(StuBean,Text)的数据.
- partitioner阶段获取mapper阶段输出的(StuBean,Text),按照StuBean进行自定义分区
- reduce阶段对mapper阶段的数据进行处理,将context写入数据变为(Text,StuBean)
- Driver实现整个流程
2.实现代码
- Stubean对象序列化
package com.atguigu.mapreduce.scoreInfo; import org.apache.hadoop.io.WritableComparable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; //input--排序 public class StuBean implements WritableComparable<StuBean> { private long age; public long getAge() { return age; } public void setAge(long age) { this.age = age; } public StuBean() { } @Override public String toString() { return age+"\t"; } @Override public void write(DataOutput out) throws IOException { out.writeLong(age); } // 序列化读取 @Override public void readFields(DataInput in) throws IOException { this.age =in.readLong(); } @Override public int compareTo(StuBean o) { if (o.getAge() < this.getAge()){ return -1; }else { return 1; } } }
- mapper阶段
package com.atguigu.mapreduce.scoreInfo; import com.atguigu.mapreduce.Comparable.FlowBean; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; //根据input类型,输出Text的主键,stuBean的数据 public class StuMapper extends Mapper<LongWritable,Text,StuBean,Text> { private Text outV=new Text(); private StuBean outK=new StuBean(); @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, StuBean, Text>.Context context) throws IOException, InterruptedException { String s = value.toString(); String[] split = s.split("\t"); outK.setAge(Long.parseLong(split[1])); outV.set(split[0]); context.write(outK,outV); } }
- partitioner自定义分区
package com.atguigu.mapreduce.scoreInfo; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Partitioner; public class StuParticular extends Partitioner<StuBean,Text>{ @Override public int getPartition(StuBean stuBean, Text text, int numPartitions) { long prePhone = stuBean.getAge(); int partition; if (prePhone <18 && prePhone >0){ partition=0; } else if (prePhone >=20 && prePhone <23) { partition = 1; }else if (prePhone >=23 && prePhone <26) { partition = 2; }else if (prePhone >=26 && prePhone <28) { partition = 3; }else { partition=4; } return partition; } }
- reduce阶段
package com.atguigu.mapreduce.scoreInfo; import com.atguigu.mapreduce.writable.FlowBean; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; //将mapper阶段的数据转化成视图bean和int public class StuReducer extends Reducer<StuBean,Text,Text,StuBean> { @Override protected void reduce(StuBean key, Iterable<Text> values, Reducer<StuBean, Text, Text, StuBean>.Context context) throws IOException, InterruptedException { for (Text i :values) { context.write(i,key); } } }
- Driver阶段
package com.atguigu.mapreduce.scoreInfo;
import com.atguigu.mapreduce.particular.ProvinceParticular;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/*
只排序
*/
public class StuDriver {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
// 1 获取配置信息以及获取job对象
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2 关联本Driver程序的jar
job.setJarByClass(StuDriver.class);
// 3 关联Mapper和Reducer的jar
job.setMapperClass(StuMapper.class);
job.setReducerClass(StuReducer.class);
// 4 设置Mapper输出的kv类型
job.setMapOutputKeyClass(StuBean.class);
job.setMapOutputValueClass(Text.class);
// 5 设置最终输出kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(StuBean.class);
//设置分区--分区与输出是不一样的,输出!=分区
job.setPartitionerClass(StuParticular.class);
job.setNumReduceTasks(5);
// 6 设置输入和输出路径
FileInputFormat.setInputPaths(job, new Path("D:\\desk\\ac.txt"));
FileOutputFormat.setOutputPath(job, new Path("D:\\desk\\ac9"));
// 7 提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本次实验建立在排序的基础上,适合新手