hadoop 二次排序

                                               hadoop 二次排序

1       3
1       2
1       1
3       3
3       2
2       2
2       1
3       1

排序后:

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

代码为:

package com.hadoop.test.SecondSort;


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;


import org.apache.hadoop.io.WritableComparable;


public class SortKey implements WritableComparable<SortKey> {
private Long first;
private Long second;


public SortKey() {
}


public SortKey(Long first, Long second) {
super();
this.first = first;
this.second = second;
}


@Override
public void write(DataOutput out) throws IOException {
out.writeLong(this.first);
out.writeLong(this.second);


}


@Override
public void readFields(DataInput in) throws IOException {
this.first = in.readLong();
this.second = in.readLong();


}


@Override
public int compareTo(SortKey o) {
if (this.first != o.first) {
return (int) (this.first - o.first);
} else {
return (int) (this.second - o.second);
}


}


@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.first.hashCode()+this.second.hashCode();
}


@Override
public boolean equals(Object obj) {
if (!(obj instanceof SortKey)) {
return false;
} else {
SortKey key = (SortKey) obj;
return (this.first == key.first && this.second == key.second);
}
}


public Long getFirst() {
return first;
}


public void setFirst(Long first) {
this.first = first;
}


public Long getSecond() {
return second;
}


public void setSecond(Long second) {
this.second = second;
}





}



package com.hadoop.test.SecondSort;


import java.io.IOException;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;


public class SortMapper extends Mapper<LongWritable, Text, SortKey, LongWritable> {


@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] arr = line.split("\t");
SortKey temp = new SortKey(Long.parseLong(arr[0]), Long.parseLong(arr[1]));
if(arr.length==2){
context.write(temp, new LongWritable(Long.parseLong(arr[1])));
}
}


}



package com.hadoop.test.SecondSort;


import java.io.IOException;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;


public class SortReducer extends Reducer<SortKey, LongWritable, LongWritable, LongWritable> {


@Override
protected void reduce(SortKey k2, Iterable<LongWritable> values,Context context)
throws IOException, InterruptedException {
for (LongWritable value : values) {
context.write(new LongWritable(k2.getFirst()), new LongWritable(k2.getSecond()));
}
}


}


package com.hadoop.test.SecondSort;


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.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
 * 默认key排序 安装升序排序
 * @author 小明
 *
 */
public class JobMain {
public static void main(String[] args) throws Exception{
Configuration configuration = new Configuration();
Job job = new Job(configuration, "sort-sort");
job.setJarByClass(JobMain.class);

job.setMapperClass(SortMapper.class);
job.setMapOutputKeyClass(SortKey.class);
job.setMapOutputValueClass(LongWritable.class);

job.setReducerClass(SortReducer.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(LongWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));

Path outputDir= new Path(args[1]);
FileSystem sys = FileSystem.get(configuration);
if(sys.exists(outputDir)){
sys.delete(outputDir, true);
}
FileOutputFormat.setOutputPath(job, outputDir);
 
job.waitForCompletion(true);
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值