设为首页 收藏本站
查看: 826|回复: 0

[经验分享] hadoop sort 自定义排序(三个数比较写法)

[复制链接]

尚未签到

发表于 2016-12-10 10:51:45 | 显示全部楼层 |阅读模式
  0 目的:
  将文件,第一列相同时,第二列升序;第二列相同时,第三列升序
  3,3,3
3,2,4
3,2,0
2,2,1
2,1,4
1,1,0
  mapreduce中:

[size=89%]1.mapreduce阶段进行排序时,比较的是k2v2是不参与排序比较的。如果要想让v2也进行排序,需要把k2v2组装成新的类,作为k2,才能参与比较。


 

[size=89%]2.分组时也是按照k2进行比较的。


 


  1 代码: 核心就是将 hadoop map output的key自定义,里面写好比较写法

package sort;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
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 MyThreeSortApp {
// 0 定义操作地址
static final String FILE_ROOT = "hdfs://master:9000/";
static final String INPUT_PATH = "hdfs://master:9000/hello";
static final String OUT_PATH = "hdfs://master:9000/out";
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI(FILE_ROOT),conf);
Path outpath = new Path(OUT_PATH);
if(fileSystem.exists(outpath)){
fileSystem.delete(outpath, true);
}
// 0 定义干活的人
Job job = new Job(conf);
// 1.1 告诉干活的人 输入流位置     读取hdfs中的文件。每一行解析成一个<k,v>。每一个键值对调用一次map函数
FileInputFormat.setInputPaths(job, INPUT_PATH);
// 指定如何对输入文件进行格式化,把输入文件每一行解析成键值对
job.setInputFormatClass(TextInputFormat.class); //用户在启动MapReduce的时候需要指定一个InputFormat的implement
//1.2 指定自定义的map类
job.setMapperClass(MyMapper3.class);
job.setMapOutputKeyClass(NewKey3.class);
job.setMapOutputValueClass(NullWritable.class);

//1.3 分区
job.setNumReduceTasks(1);
//1.4 TODO 分组    目前按照默认方式执行
//1.5 TODO 规约
//2.2 指定自定义reduce类
job.setReducerClass(MyReducer3.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//2.3 指定写出到哪里
FileOutputFormat.setOutputPath(job, outpath);
job.setOutputFormatClass(TextOutputFormat.class);
// 让干活的人干活
job.waitForCompletion(true);
}
}
class MyMapper3 extends Mapper<LongWritable, Text, NewKey3, NullWritable>{

@Override
protected void map(LongWritable k1, Text v1, Context context)throws IOException, InterruptedException {
String lineStr = v1.toString();
System.out.println("map the line: " + lineStr);
String[] split = lineStr.split(",");
NewKey3 newKey3 = new NewKey3(Long.parseLong(split[0]),Long.parseLong(split[1]),Long.parseLong(split[2]));
context.write(newKey3, NullWritable.get());
}
}
class MyReducer3 extends Reducer<NewKey3, NullWritable, Text, NullWritable>{
protected void reduce(NewKey3 k2, Iterable<NullWritable> v2s, org.apache.hadoop.mapreduce.Reducer.Context context)
throws IOException, InterruptedException {
System.out.println("reduce the key is: " + k2.toString());
context.write(new Text(k2.toString()), NullWritable.get());
}

}

// 核心就是将 hadoop map output的key自定义,里面写好比较写法
class NewKey3 implements WritableComparable<NewKey3>{
private long first;
private long second;
private long third;
public NewKey3(){}
public NewKey3(long first,long second,long third){
this.first = first;
this.second = second;
this.third = third;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (first ^ (first >>> 32));
result = prime * result + (int) (second ^ (second >>> 32));
result = prime * result + (int) (third ^ (third >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NewKey3 other = (NewKey3) obj;
if (first != other.first)
return false;
if (second != other.second)
return false;
if (third != other.third)
return false;
return true;
}
@Override
public String toString() {
return first + " " + second + " " + third ;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.first);
out.writeLong(this.second);
out.writeLong(this.third);
}
@Override
public void readFields(DataInput in) throws IOException {
this.first = in.readLong();
this.second = in.readLong();
this.third = in.readLong();
}
@Override
public int compareTo(NewKey3 other) {
long result;
result = this.first - other.first;
if(result == 0){
result = this.second - other.second;
if(result == 0){
result = this.third - other.third;
}
}
return (int)result;
}
}

  2 运行结果:

[iyunv@master local]# hadoop fs -text /out/part-r-00000
Warning: $HADOOP_HOME is deprecated.
1 1 0
2 1 4
2 2 1
3 2 0
3 2 4
3 3 3

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-312305-1-1.html 上篇帖子: 数据分析不使用Hadoop的五大理由(转) 下篇帖子: hadoop启动时 localhost: Error: JAVA_HOME is not set.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表