一起学Hadoop——使用自定义Partition实现hadoop部分排序
public class PartSortMap extends Mapper<LongWritable,Text,Text,Text> {
public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
String line = value.toString();//读取一行数据,数据格式为“Apple 201701 30”
String str[] = line.split(" ");//
//年月当做key值,因为要根据key值设置分区,而Apple+“_”+销量当做value
context.write(new Text(str[1]),new Text(str[0] + "_" + str[2]));
}
}
public class PartParttition extends Partitioner<Text, Text> {
public int getPartition(Text arg0, Text arg1, int arg2) {
String key = arg0.toString();
int month = Integer.parseInt(key.substring(4, key.length()));
if (month == 1) {
return 1 % arg2;
} else if (month == 2) {
return 2 % arg2;
} else if (month == 3) {
return 3 % arg2;
}else if (month == 4) {
return 4 % arg2;
}else if (month == 5) {
return 5 % arg2;
}else if (month == 6) {
return 6 % arg2;
}else if (month == 7) {
return 7 % arg2;
}else if (month == 8) {
return 8 % arg2;
}else if (month == 9) {
return 9 % arg2;
}else if (month == 10) {
return 10 % arg2;
}else if (month == 11) {
return 11 % arg2;
}else if (month == 12) {
return 12 % arg2;
}
return 0;
}
}
public class PartSortReduce extends Reducer<Text,Text,Text,Text> {
class FruitSales implements Comparable<FruitSales>{
private String name;//水果名字
private double sales;//水果销量
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSales(double sales){
this.sales = sales;
}
public double getSales() {
return this.sales;
}
@Override
public int compareTo(FruitSales o) {
if(this.getSales() > o.getSales()){
return -1;
}else if(this.getSales() == o.getSales()){
return 0;
}else {
return 1;
}
}
}
public void reduce(Text key, Iterable<Text> values,Context context)throws IOException,InterruptedException{
List<FruitSales> fruitList = new ArrayList<FruitSales>();
for(Text value: values) {
String[] str = value.toString().split("_");
FruitSales f = new FruitSales();
f.setName(str[0]);
f.setSales(Double.parseDouble(str[1]));
fruitList.add(f);
}
Collections.sort(fruitList);
for(FruitSales f : fruitList){
context.write(new Text(f.getName()),new Text(String.valueOf(f.getSales())));
}
}
}
public class PartSortMain {
public static void main(String[] args)throws Exception{
Configuration conf = new Configuration();
//获取运行时输入的参数,一般是通过shell脚本文件传进来。
String [] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length < 2){
System.err.println("必须输入读取文件路径和输出路径");
System.exit(2);
}
Job job = new Job();
job.setJarByClass(PartSortMain.class);
job.setJobName("PartSort app");
//设置读取文件的路径,都是从HDFS中读取。读取文件路径从脚本文件中传进来
FileInputFormat.addInputPath(job,new Path(args[0]));
//设置mapreduce程序的输出路径,MapReduce的结果都是输入到文件中
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.setPartitionerClass(PartParttition.class);//设置自定义partition类
job.setNumReduceTasks(12);//设置为partiton数量
//设置实现了map函数的类
job.setMapperClass(PartSortMap.class);
//设置实现了reduce函数的类
job.setReducerClass(PartSortReduce.class);
//设置reduce函数的key值
job.setOutputKeyClass(Text.class);
//设置reduce函数的value值
job.setOutputValueClass(Text.class);
System.exit(job.waitForCompletion(true) ? 0 :1);
}
}



#!/usr/bin/python
import sys
base_numer = 99999
for line in sys.stdin:
ss = line.strip().split(' ')
fruit = ss[0]
yearmm = ss[1]
sales = ss[2]
new_key = base_number - int(sales)
mm = yearmm[4:6]
print "%s\t%s\t%s" % (int(mm), int(new_key), fruit)
#!/usr/bin/python
import sys
base_number = 99999
for line in sys.stdin:
idx_id, sales, fruit = line.strip().split('\t')
new_key = base_number - int(sales)
print '\t'.join([val, str(new_key)])
set -e -x
HADOOP_CMD="/usr/local/src/hadoop-2.6.1/bin/hadoop"
STREAM_JAR_PATH="/usr/local/src/hadoop-2.6.1/share/hadoop/tools/lib/hadoop-streaming-2.6.1.jar"
INPUT_FILE_PATH_A="/data/fruit.txt"
OUTPUT_SORT_PATH="/output_sort"
$HADOOP_CMD fs -rmr -skipTrash $OUTPUT_SORT_PATH
$HADOOP_CMD jar $STREAM_JAR_PATH \
-input $INPUT_FILE_PATH_A\
-output $OUTPUT_SORT_PATH \
-mapper "python map_sort.py" \
-reducer "python reduce_sort.py" \
-file ./map_sort.py \
-file ./red_sort.py \
-jobconf mapred.reduce.tasks= \
-jobconf stream.num.map.output.key.fields= \
-jobconf num.key.fields.for.partition= \
-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner
一起学Hadoop——使用自定义Partition实现hadoop部分排序的更多相关文章
- 2 weekend110的hadoop的自定义排序实现 + mr程序中自定义分组的实现
我想得到按流量来排序,而且还是倒序,怎么达到实现呢? 达到下面这种效果, 默认是根据key来排, 我想根据value里的某个排, 解决思路:将value里的某个,放到key里去,然后来排 下面,开始w ...
- Hadoop mapreduce自定义分区HashPartitioner
本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...
- hadoop的自定义数据类型和与关系型数据库交互
最近有一个需求就是在建模的时候,有少部分数据是postgres的,只能读取postgres里面的数据到hadoop里面进行建模测试,而不能导出数据到hdfs上去. 读取postgres里面的数据库有两 ...
- commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现
commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现. Common Crawl 提供一个示例程序 BasicArcFileReaderSample.java ...
- Hadoop mapreduce自定义分组RawComparator
本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...
- 大数据学习笔记之Hadoop(一):Hadoop入门
文章目录 大数据概论 一.大数据概念 二.大数据的特点 三.大数据能干啥? 四.大数据发展前景 五.企业数据部的业务流程分析 六.企业数据部的一般组织结构 Hadoop(入门) 一 从Hadoop框架 ...
- 《Hadoop》对于高级编程Hadoop实现构建企业级安全解决方案
本章小结 ● 理解企业级应用的安全顾虑 ● 理解Hadoop尚未为企业级应用提供的安全机制 ● 考察用于构建企业级安全解决方式的方法 第10章讨论了Hadoop安全性以及Hadoop ...
- [BigData - Hadoop - YARN] YARN:下一代 Hadoop 计算平台
Apache Hadoop 是最流行的大数据处理工具之一.它多年来被许多公司成功部署在生产中.尽管 Hadoop 被视为可靠的.可扩展的.富有成本效益的解决方案,但大型开发人员社区仍在不断改进它.最终 ...
- hadoop分布式存储(2)-hadoop的安装(毕业设计)
总共分三步:1.准备linux环境 租用"云主机",阿里云,unitedStack等,云主机不受本机性能影响(或者直接安转linux操作系统或者虚拟机也行): PuTTy Conf ...
随机推荐
- SIFT+BOW 实现图像检索
原文地址:https://blog.csdn.net/silence2015/article/details/77374910 本文概述 图像检索是图像研究领域中一个重要的话题,广泛应用于医学,电子商 ...
- 训练报告 (2014-2015) 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest
Solved A Gym 100488A Yet Another Goat in the Garden B Gym 100488B Impossible to Guess Solved C Gym ...
- tomcat session Memcache 共享
背景 这次做的这个项目并发还真是挺高,单表一天产生百万条记录不在话下.结果导致运行过程中经常丢失数据,卡.慢等.开来终于要搞一次负载均衡了,之前实验学习了不少,但是没有在项目中实际用过,因为并发量不大 ...
- 【原创】大数据基础之Spark(9)spark部署方式yarn/mesos
1 下载解压 https://spark.apache.org/downloads.html $ wget http://mirrors.shu.edu.cn/apache/spark/spark-2 ...
- sublime text3 golang插件(golang build)
1 前言 先前条件: sublime text3:下载地址:http://www.sublimetext.com/3 golang:下载地址:https://golang.google.cn/dl/ ...
- Java数据结构与算法解析(十二)——散列表
散列表概述 散列表就是一种以 键-值(key-indexed) 存储数据的结构,我们只要输入待查找的值即key,即可查找到其对应的值. 散列表的思路很简单,如果所有的键都是整数,那么就可以使用一个简单 ...
- js实现两种排序算法——冒泡、快速排序
* 一:冒牌排序1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置要实现上述规则需要用到两层for循环,外层从第一个数到倒数第二个数,内层从 ...
- html5中如何去掉input type date默认
html5中如何去掉input type date默认样式 2.对日期时间控件的样式进行修改目前WebKit下有如下9个伪元素可以改变日期控件的UI:::-webkit-datetime-edit – ...
- HTML之Position用法
在此,先做声明,本篇仅是摘录自互联网,个人认为这篇文章讲的很不错,附于此地与大家共同欣赏. position的四个属性值: 1.relative2.absolute3.fixed4.static下面分 ...
- python 知识梳理
1.数据类型:字符串,列表,元组,字典,集合.处理每种数据类型的函数 2.判断与循环部分 3.高级函数:lambda,map,reduce,filter 4.自定义模块以及第三方模块 5.函数式编程 ...