前言:

一直不会用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全局排序的更多相关文章

  1. 三种方法实现Hadoop(MapReduce)全局排序(1)

    我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...

  2. 一起学Hadoop——TotalOrderPartitioner类实现全局排序

    Hadoop排序,从大的范围来说有两种排序,一种是按照key排序,一种是按照value排序.如果按照value排序,只需在map函数中将key和value对调,然后在reduce函数中在对调回去.从小 ...

  3. Hadoop对文本文件的快速全局排序

    一.背景 Hadoop中实现了用于全局排序的InputSampler类和TotalOrderPartitioner类,调用示例是org.apache.hadoop.examples.Sort. 但是当 ...

  4. Mapreduce的排序(全局排序、分区加排序、Combiner优化)

    一.MR排序的分类 1.部分排序:MR会根据自己输出记录的KV对数据进行排序,保证输出到每一个文件内存都是经过排序的: 2.全局排序: 3.辅助排序:再第一次排序后经过分区再排序一次: 4.二次排序: ...

  5. MapReduce TotalOrderPartitioner 全局排序

    我们知道Mapreduce框架在feed数据给reducer之前会对map output key排序,这种排序机制保证了每一个reducer局部有序,hadoop 默认的partitioner是Has ...

  6. 大数据mapreduce全局排序top-N之python实现

    a.txt.b.txt文件如下: a.txt hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop ...

  7. 一起学Hadoop——使用自定义Partition实现hadoop部分排序

    排序在很多业务场景都要用到,今天本文介绍如何借助于自定义Partition类实现hadoop部分排序.本文还是使用java和python实现排序代码. 1.部分排序. 部分排序就是在每个文件中都是有序 ...

  8. MapReduce怎么优雅地实现全局排序

    思考 想到全局排序,是否第一想到的是,从map端收集数据,shuffle到reduce来,设置一个reduce,再对reduce中的数据排序,显然这样和单机器并没有什么区别,要知道mapreduce框 ...

  9. JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!

    JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...

随机推荐

  1. BAT脚本打印空行的使用方法

    @echo off echo= echo, echo; echo+ echo/ echo[ echo] echo: echo. echo\ pause 这十种方法可以分为三组,每组的效率依次递减. 至 ...

  2. winform下的智能提示框

    winform下的智能提示框 最近在搞winform的程序,接触到有些可能以后还会用到的功能,所以写到博客园里去,第一可以加深自己的印象,第二可以在以后再遇到同样问题的时候忘记了可以马上回来看看,第三 ...

  3. Roll A Ball

    GameObject的添加就不细说了,地面,小球和碰撞小物体. 刚体组件(Rigidbody): 使物体能够模拟物理效果,比如重力,碰撞,推力等: 控制小球移动的脚本(Script,Ball的脚本): ...

  4. :input 匹配所有 input, textarea, select 和 button 元素

    描述: 查找所有的input元素,下面这些元素都会被匹配到. HTML 代码: <form> <input type="button" value="I ...

  5. CAN总线抓包

    马六: 由此可见, 马6的7e9跟7e8反馈的数据差不太多. 而标志508反馈的情况则不同, 波箱跟发动机反馈完全不同: 宝马3的数据如下: 证明宝马也有7e8跟7ec, 但是貌似7e8是主流啊... ...

  6. javascript,jQuery,trim()

    JavaScript trim() Syntax string.trim() The trim() method removes whitespace from both sides of a str ...

  7. android音乐播放器(Service+ContentProvider+Broadcast+Activity四大组件完成)

    1.获取音乐 1-1:获取手机中的音乐(用ContentProvider内容提供者来完成): package com.firefly.util; import java.util.ArrayList; ...

  8. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

  9. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  10. img的hover事件闪动

    今天给同学写一个相册照片鼠标浮动显示细节的效果,遇到了闪动的bug,也顺利解决,就写下来跟大家分享. 我使用的是'标签:hover + 标签'的形式,如果使用jquery的mouseover.mous ...