java写hadoop全局排序
前言:
一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序。但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生语言来写map-reduce;
开发环境eclipse,windows,把hadoop相关的jar附加到程序中,打包后放回linux虚机执行;
输入数据
1 haha 10
2 haha 9
3 haha 100
4 haha 1
5 haha 1
6 haha 2
7 haha 3
8 haha 1000
9 haha 1000
10 haha 999
11 haha 888
12 haha 10000
输出数据 cat part*-*>o.txt
1 haha 1
2 haha 1
3 haha 2
4 haha 3
5 haha 9
6 haha 10
7 haha 100
8 haha 888
9 haha 999
10 haha 1000
11 haha 1000
12 haha 10000
代码 MyMapper
package com.globalsort;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text> { @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String temp=value.toString();
String[] segs = temp.split("\t");
if (segs.length!=2)
{
return;
}
int newval = Integer.parseInt(segs[1]);
context.write(new LongWritable(newval),
new Text(segs[0])); } }
重写reducer
package com.globalsort; import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.util.Iterator; public class MyReducer extends Reducer<LongWritable, Text,Text,LongWritable > { @Override protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException { Iterator<Text> it = values.iterator();
while (it.hasNext())
{
String data = it.next().toString();
context.write(new Text(data),key); }
} }
重写patitioner
package com.globalsort;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class MyPartitioner extends Partitioner<LongWritable, Text> { @Override public int getPartition(LongWritable key, Text value, int numPartitions) {
long tmp = key.get();
if (tmp <= 100) {
return 0 % numPartitions; } else if (tmp <= 1000) {
return 1 % numPartitions; } else {
return 2 % numPartitions; } } }
runer
package com.globalsort; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import java.io.IOException; public class GlobalSortMain implements Tool { private Configuration conf; @Override
public Configuration getConf() {
return conf;
} @Override
public void setConf (Configuration conf){
this.conf=conf;
}
@Override
public int run(String[] args) throws Exception {
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 3) {
System.err.println("Usage: must contain <in> <out>");
}
Job job = configureJob(otherArgs);
return (job.waitForCompletion(true) ? 0 : 1);
} private Job configureJob(String[] args) throws IOException { conf.set("mapred.job.priority", "VERY_HIGH");
// conf.setBoolean("mapred.compress.map.output", true);
//conf.setClass("mapred.map.output.compression.codec", GzipCodec.class, CompressionCodec.class);
// conf.setBoolean("mapred.compress.reduce.output", true);
//conf.setClass("mapred.reduce.output.compression.codec", GzipCodec.class, CompressionCodec.class);
Job job = new Job(conf, "global sort liuyu");
job.setJarByClass(GlobalSortMain.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setPartitionerClass(MyPartitioner.class);
job.setNumReduceTasks(3);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
return job;
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
ToolRunner.run(conf, new GlobalSortMain(), args);
} }
java写hadoop全局排序的更多相关文章
- 三种方法实现Hadoop(MapReduce)全局排序(1)
我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...
- 一起学Hadoop——TotalOrderPartitioner类实现全局排序
Hadoop排序,从大的范围来说有两种排序,一种是按照key排序,一种是按照value排序.如果按照value排序,只需在map函数中将key和value对调,然后在reduce函数中在对调回去.从小 ...
- Hadoop对文本文件的快速全局排序
一.背景 Hadoop中实现了用于全局排序的InputSampler类和TotalOrderPartitioner类,调用示例是org.apache.hadoop.examples.Sort. 但是当 ...
- Mapreduce的排序(全局排序、分区加排序、Combiner优化)
一.MR排序的分类 1.部分排序:MR会根据自己输出记录的KV对数据进行排序,保证输出到每一个文件内存都是经过排序的: 2.全局排序: 3.辅助排序:再第一次排序后经过分区再排序一次: 4.二次排序: ...
- MapReduce TotalOrderPartitioner 全局排序
我们知道Mapreduce框架在feed数据给reducer之前会对map output key排序,这种排序机制保证了每一个reducer局部有序,hadoop 默认的partitioner是Has ...
- 大数据mapreduce全局排序top-N之python实现
a.txt.b.txt文件如下: a.txt hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop ...
- 一起学Hadoop——使用自定义Partition实现hadoop部分排序
排序在很多业务场景都要用到,今天本文介绍如何借助于自定义Partition类实现hadoop部分排序.本文还是使用java和python实现排序代码. 1.部分排序. 部分排序就是在每个文件中都是有序 ...
- MapReduce怎么优雅地实现全局排序
思考 想到全局排序,是否第一想到的是,从map端收集数据,shuffle到reduce来,设置一个reduce,再对reduce中的数据排序,显然这样和单机器并没有什么区别,要知道mapreduce框 ...
- JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!
JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
随机推荐
- [Android Tips] 17. Check APK Sign Info
$ jarsigner -verify -verbose -certs <apk file path> 查看 keystore $ keytool -list -keystore debu ...
- 使用plsql执行计划进行sql调优(转载)
一段SQL代码写好以后,可以通过查看SQL的执行计划,初步预测该SQL在运行时的性能好坏,尤其是在发现某个SQL语句的效率较差时,我们可以通过查看执行计划,分析出该SQL代码的问题所在. 那么,作为开 ...
- svn提交时强制添加注释
1,找到Repositories 文件夹 2,在Repositories 中找到对应的工程, 3,在工程下找到hooks文件夹 4.在hooks文件夹下添加 :pre-commit.bat文件;文件内 ...
- iphone和ipad各控件大小
ipad和iphone是一样的:
- TextBox
一.聚焦: private void FrmOnlineChargeMoney_Paint(object sender, PaintEventArgs e) { edtAuthCode.SelectA ...
- SQL学习指南 ——笔记
前言:每章的练习题很实用,跟着练了一遍.答案附录有 1.流行的商业级关系数据库:
- MVC5 + EF6 完整入门教程三
期待已久的EF终于来了. 学完本篇文章,你将会掌握基于EF数据模型的完整开发流程. 本次将会完成EF数据模型的搭建和使用. 基于这个模型,将之前的示例添加数据库查询验证功能. 文章提纲 概述 & ...
- LeetCode----202. Happy Number(Java)
package isHappy202; /* * Write an algorithm to determine if a number is "happy". A happy n ...
- Volley获取网络图片使用总结
参考资料 http://www.3fwork.com/b600/001956MYM002697/ //缓存 int cacheSize= 10 * 1024 * 1024; //声明一个新的Reque ...
- HDU 5685:2016"百度之星" - 资格赛 Problem A
原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others) ...