博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Hadoop 2.6.0运行数字排序的计算
阅读量:7212 次
发布时间:2019-06-29

本文共 4054 字,大约阅读时间需要 13 分钟。

  上个博客写了Hadoop2.6.0的环境部署,下面写一个简单的基于数字排序的小程序,真正实现分布式的计算,原理就是对多个文件中的数字进行排序,每个文件中每个数字占一行,排序原理是按行读取后分块进行排序,最后对块进行合并,通俗来说就是首先对小于100的数据范围进行排序,然后对100-1000之间的数据进行排序,最后对大于1000的数据进行排序,最终这3块合成之后也一定是按顺序排列的,代码如下:

import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.Partitioner;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;public class Sort {    public static class Map extends            Mapper
{ private static IntWritable data = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); data.set(Integer.parseInt(line)); context.write(data, new IntWritable(1)); } } public static class Reduce extends Reducer
{ private static IntWritable linenum = new IntWritable(1); public void reduce(IntWritable key, Iterable
values, Context context) throws IOException, InterruptedException { for (IntWritable val : values) { context.write(linenum, key); linenum = new IntWritable(linenum.get() + 1); } } } public static class Partition extends Partitioner
{ @Override public int getPartition(IntWritable key, IntWritable value, int numPartitions) { int MaxNumber = 65223; int bound = MaxNumber / numPartitions + 1; int keynumber = key.get(); for (int i = 0; i < numPartitions; i++) { if (keynumber < bound * i && keynumber >= bound * (i - 1)) return i - 1; } return 0; } } /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args) .getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage WordCount
"); System.exit(2); } Job job = new Job(conf, "Sort"); job.setJarByClass(Sort.class); job.setMapperClass(Map.class); job.setPartitionerClass(Partition.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }}

 

  将源文件上传到服务器后,进行编译,hadoop2.6.0的编译方式和之前的hadoop1.2.1不太一样,这次需要引入3个jar文件分别是:

  share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.0.jar

  share/hadoop/common/hadoop-common-2.6.0.jar

  share/hadoop/common/lib/commons-cli-1.2.jar

  编译命令这里为:

javac -classpath ../share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.0.jar:../share/hadoop/common/hadoop-common-2.6.0.jar:../share/hadoop/common/lib/commons-cli-1.2.jar Sort.java

  如果忽略要警告可以添加-Xlint:deprecation参数进行编译:

javac -classpath ../share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.0.jar:../share/hadoop/common/hadoop-common-2.6.0.jar:../share/hadoop/common/lib/commons-cli-1.2.jar -Xlint:deprecation Sort.java

  编译成功之后打包操作:

jar -cvf sort.jar *.class

  打成sort.jar之后建立几个文件,格式就如下图所示:

  

  然后上传到HDFS文件系统之后,可以用hadoop来跑一下:

hadoop jar sort.jar Sort /sort /sortoutput

  注意:输出目录,不能使用原来的,如果原来存在一个目录,不管是空的还是非空的,那么hadoop都会报错,所以应该指定一个不存在的目录,让hadoop去新建他

  等运行完毕,然后查看输出就行了:

hdfs dfs -cat /sortoutput/*

  

  这样就简单的使用hadoop平台以分布式的方式运行了java应用

 

  

 

转载地址:http://fqrum.baihongyu.com/

你可能感兴趣的文章
Kaggle冠军经验分享丨如何用15个月冲到排行榜的首位
查看>>
Stream流与Lambda表达式(一) 杂谈
查看>>
独家揭秘!阿里大规模数据中心的性能分析
查看>>
Valid
查看>>
大数据驱动的运营创新和探索
查看>>
你属于程序员中的哪种人?
查看>>
基于Mixin Network的PHP比特币开发教程 之一:创建机器人
查看>>
时序数据库连载系列: 时序数据库一哥InfluxDB之存储机制解析
查看>>
sorl实现商品快速搜索
查看>>
Webpack4 学习笔记 - 01:webpack的安装和简单配置
查看>>
236. Lowest Common Ancestor of a Binary Tree
查看>>
300. Longest Increasing Subsequence
查看>>
GO基础编程-自定义函数
查看>>
你真的懂switch吗?聊聊switch语句中的块级作用域
查看>>
从0到1,了解NLP中的文本相似度
查看>>
HTML5新特性总结
查看>>
超越时代的天才——图灵
查看>>
使用 ale.js 制作一个小而美的表格编辑器(2)
查看>>
mybatis常用标签和动态查询
查看>>
以太坊交易源码分析
查看>>