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本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
随机推荐
- SecureCRT工具配色方案
一.配色后的效果图 二.设置背景颜色 1.选项(Options)==>会话选项(Sessions options)==>终端(Terminal)==>仿真(Emulation) 2. ...
- JAVA:NIO初步了解
简介: Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式. Java NIO: Ch ...
- Python数据
读取文件中数据的最高分数 highest_score=0 result_f=open("results.txt") for line in result_f: (name,scor ...
- 原生JS--Ajax
原生Ajax: Ajax基础:--ajax:无刷新数据读取,读取服务器上的信息--HTTP请求方法: --GET:用于获取数据,如浏览帖子 --POST:用于上传数据,如用户注册 --GE ...
- spark调试
http://blog.csdn.net/shenlanzifa/article/details/42679577 http://alvinalexander.com/java/jwarehouse/ ...
- Android 对电话进行监听和挂断
1.添加权限 <!--拨打电话的权限--><uses-permission android:name="android.permission.PROCESS_OUTGOIN ...
- angular之控制器(0)
一.控制器的含义 在angularJS中,controlle是一个javascript函数/类,用于操作作用域中,各个对象的初始状态以及相应的行为 二.控制器的作用 1. 控制 AngularJS 应 ...
- 用流从一个指定的网址抓取html代码
package cn.bdqn.collect.test; import java.io.BufferedReader; import java.io.InputStream; import java ...
- bootstrap中table页面做省市区级联效果(级联库见前面级联编辑)(非select下拉框)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java精确计算小数
Java在计算浮点数的时候,由于二进制无法精确表示0.1的值(就好比十进制无法精确表示1/3一样),所以一般会对小数格式化处理. 但是如果涉及到金钱的项目,一点点误差都不能有,必须使用精确运算的时候, ...