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本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
随机推荐
- confluence5.6安装
转自:http://ju.outofmemory.cn/entry/157013 说明:此文在confluence-wiki-5.6.5版本亲测通过附件:http://pan.baidu.com/s/ ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- windows安装TortoiseGit详细使用教程
标签:tortoisegit 环境:win8.1 64bit 安装准备: 首先你得安装windows下的git msysgit1.9.5 别下载错,不习惯英文的朋友,也可以下个语言包] 一.安装图解: ...
- 添加office权限时找不到ofice,com组件的方法
1.执行 mmc.exe,文件->添加/删除管理单元->可用的管理单元,中选择组件服务->添加->所选单元格 2. 执行dcomcnfg.exe -32,用于64的操作系统
- paper 97:异质人脸识别进展的资讯
高新波教授团队异质人脸图像识别研究取得新突破,有望大大降低刑侦过程人力耗费并提高办案效率 近日,西安电子科技大学高新波教授带领的研究团队,在异质人脸图像识别研究领域取得重要进展,其对香 ...
- C#使用二叉树算法设计一个无限分级的树表
效果图: 数据库: 操作树的示意图: 控制器代码: using Dw.Business; using Dw.Entity; using Dw.Utilities; using System; usin ...
- SQLServer 使用smb存放数据文件
安装smb: 服务器管理器->角色->文件服务 1.配置smb共享时,更改NTFS权限,需要将SQLServer启动域帐户加入,读.写.完全控制等权限 2.实例启动用户需要使用域帐户 3. ...
- ubuntu下gcc、g++和gfortran版本切换
第一步:用 which gcc.which g++和which gfortran查看位置,我的显示结果为:/usr/bin/gcc:/usr/bin/g++和/usr/bin/gfortran 第二部 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- [问题2014S15] 复旦高等代数II(13级)每周一题(第十五教学周)
[问题2014S15] 设 \(O\) 为 \(n\) 阶正交阵,\(A=\mathrm{diag}\{a_1,a_2,\cdots,a_n\}\) 为实对角阵, 证明: 方阵 \(OA\) 的特征 ...