static {
System.setProperty(“hadoop.home.dir”,“E:/x3/hadoop-2.9.2”);
}

//map
public static class MyMapper extends Mapper<LongWritable,Text,Text,Week2Data>{@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String lineValue = value.toString();String[] split = lineValue.split("\t");String year = split[0].substring(0, 4);Integer diff = null;Integer num1 = Integer.parseInt(split[1]);Integer num2 = Integer.parseInt(split[2]);diff = num1 - num2;context.write(new Text(year),new Week2Data(split[0],diff));}
}//自定义分区器
public static class MyPartitioner extends Partitioner<Text,Week2Data>{@Overridepublic int getPartition(Text key, Week2Data value, int i) {return key.hashCode() % 127 % i;}
}//自定义分组
public static class MyGroupComparble extends WritableComparator{protected MyGroupComparble() {super(Week2Data.class,true);}@Overridepublic int compare(WritableComparable a, WritableComparable b) {Week2Data data1 = (Week2Data) a;Week2Data data2 = (Week2Data) b;return data1.getYear().compareTo(data2.getYear());}
}//reduce
public static class MyReduce extends Reducer<Text,Week2Data,Text,Text>{@Overrideprotected void reduce(Text key, Iterable<Week2Data> values, Context context) throws IOException, InterruptedException {for (Week2Data value : values){context.write(new Text(value.getYear()+"   "+value.getBalance()),new Text(""));}}
}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {//jobConfiguration conf = new Configuration();Job job = Job.getInstance(conf, "week2-test2");//写入文件FileInputFormat.addInputPath(job,new Path(args[0]));//map计算job.setMapperClass(MyMapper.class);job.setMapOutputValueClass(Week2Data.class);job.setMapOutputKeyClass(Text.class);//shuffle流程//调用自定义分区器job.setPartitionerClass(MyPartitioner.class);//调用自定义分组// job.setGroupingComparatorClass(MyGroupComparble.class);//reduce计算//设置reduce分区个数job.setNumReduceTasks(2);job.setReducerClass(MyReduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//判断文件是否存在FileSystem fs = FileSystem.get(conf);if (fs.exists(new Path(args[1]))) {fs.delete(new Path(args[1]), true);}//写出文件FileOutputFormat.setOutputPath(job,new Path(args[1]));//提交作业boolean result = job.waitForCompletion(true);System.out.println(result);
}

实体类
public class Week2Data implements WritableComparable{

private String year;
private Integer balance;public Week2Data(String year, Integer balance) {this.year = year;this.balance = balance;
}public Week2Data() {}public String getYear() {return year;
}public void setYear(String year) {this.year = year;
}public Integer getBalance() {return balance;
}public void setBalance(Integer balance) {this.balance = balance;
}@Override
public int compareTo(Week2Data o) {return o.balance.compareTo(this.getBalance());
}@Override
public void write(DataOutput output) throws IOException {output.writeUTF(year);output.writeInt(balance);
}@Override
public void readFields(DataInput input) throws IOException {this.year = input.readUTF();this.balance = input.readInt();
}

}

MapReduce之week2 test 分区计算结余(练习)相关推荐

  1. 使用QGIS分区统计工具实现栅格分类数据的分区计算面积——GlobeLand30地表覆盖数据为例

    在栅格分析中,常常碰到使用分类后的栅格数据按照特定分区统计面积的需求,今天,我将使用QGIS的分区统计工具,演示地表覆盖数据按照地表分类分区域统计面积的过程,希望能给有这方面需求的朋友提供参考.  0 ...

  2. MapReduce既是编程模型又是计算框架

    learn from 从0开始学大数据(极客时间) MapReduce 编程模型 包含 Map 和 Reduce 两个过程 map 的主要输入是一对 <Key, Value> 值,输出一对 ...

  3. Spark vs. MapReduce 时间节约66%,计算节约40%

    本文转自http://www.csdn.net/article/2014-11-04/2822474,所有权力归原作者所有.虽然本文并没有讲什么实质的东西,但是可以拿来吹牛逼呀~ ⁽⁽ଘ( ˊᵕˋ ) ...

  4. 库存流水账计算结余数量

    原始数据 计算后数据 关键sql: sum(isnull(case when InoutFlag=1 then gios.FactReceiptQuan else null end, 0)-isnul ...

  5. MapReduce自定义排序、分区、分组案例

    一.题目 数据:由于数据量比较大,放入百度网盘中链接:      https://pan.baidu.com/s/13vHZ1v7Rw2Vbb5wZrWX0cA 提取码: 6qug  字段说明 班级  ...

  6. MapReduce二次排序分区,分组优化

    自定义分组 NameGroup package test;import org.apache.hadoop.io.RawComparator; import org.apache.hadoop.io. ...

  7. MapReduce编程系列 — 2:计算平均分

    1.项目名称: 2.程序代码: package com.averagescorecount;import java.io.IOException; import java.util.Iterator; ...

  8. MapReduce算法设计(三)----相对频率计算

    1.    相对频率的计算 在我们使用应用程序来分析文章时,一个重要的使用就是文章主题分类.就是依据文章所要表达的主题进行分类.而一般的程序化分类 (非人工分类)所使用的方法是TF-IDF.这种方法依 ...

  9. Twister: 迭代MapReduce计算框架

    摘要:MapReduce编程模型已经简化了许多数据并行应用的实现.编程模型的简化和MapReduce实现提供的服务质量在分布式计算社区上吸引了很多的热情.把MapReduce应用到多种科学应用的这么多 ...

最新文章

  1. Redis初学:9(Zset类型)
  2. Android Wear开发 - 数据通讯 - 第二节 : 数据的发送与接收
  3. 【指标统计】删除错误遥信
  4. VS2017的C++开发心得:头文件的路径问题与属性管理器
  5. boost::hana::make_type用法的测试程序
  6. 如何用ant给Java项目生成文档
  7. webServlet(/) 和 webServlet(/*) 的区别
  8. Chrome 100发布:全新图标,CPU、内存占用暴降!
  9. 2021 年中国开源优秀人物揭晓
  10. 位置采集[置顶] iPhone手机上的GPS位置信息采集与分享应用
  11. FFMpeg学习记录:X264编码器
  12. Ucinet6 + Netdraw 根据excel文件绘制网络拓扑图
  13. 笔记_python库jpype安装和使用,及如何打包java程序供Python调用
  14. 二叉查找树,平衡二叉树
  15. java软件工程师培训学_Java软件工程师学习路线
  16. 远程监控养猪监控系统
  17. HDU - 4489 The King’s Ups and Downs (排列组合+dp)
  18. 淘宝店铺运营经验分享,影响宝贝转化率的因素有哪些,如何提高转化
  19. react开发插件-ES7 React/Redux/GraphQL/React-Native snippets
  20. 猴子都能懂得Git(入门篇汇总版)持续更新中~~~

热门文章

  1. 运动模糊/拖影的原因分析
  2. 用实际例子理解回调函数(Calback)
  3. [论]【DSTGCN】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
  4. win7与VMware/VBox下linux共享文件夹方法
  5. rails consol reload!
  6. 爱因斯坦:三篇著名演讲
  7. maximo跟java_maximo遇到的错误问题
  8. python批量合并excel文件,后缀名为xls
  9. vue+ts的书写规范
  10. Visual Studio中输入英文会在字母之间自动增加空格