两个map一个reduce(两个输入文件)
两个map,一个map读取一个hdfs文件,map完之后进入一个reduce进行逻辑处理。
package com.zhongxin.mr; import org.apache.commons.lang.StringUtils;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; /**
* Created by DingYS on 2017/12/7.
* 用户回款计划统计(详情)
*/
public class UserPlanAmount { public static class StatisticsMap extends Mapper<LongWritable,Text,Text,Text> {
private Text outKey = new Text();
private Text outValue = new Text();
private Pattern pattern = Pattern.compile(","); //statistics文件处理
public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{
String strs[] = pattern.split(String.valueOf(value));
String bidNo = strs[2];
String userId = strs[3];
String totalOnInvestedShare = strs[8];
String addShare = strs[17];
String addyield = strs[16];
String outv = bidNo + pattern +"statstics" + pattern + userId + pattern + totalOnInvestedShare + pattern + addShare + pattern + addyield;
outKey.set(bidNo);
outValue.set(outv);
context.write(outKey,outValue);
}
} public static class PlanMap extends Mapper<LongWritable,Text,Text,Text> {
private Text outKey = new Text();
private Text outValue = new Text();
private Pattern pattern = Pattern.compile(","); // plan统计表(该文件在sqoop导入时就进行了数据计算及合并)
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
String strs[] = pattern.split(String.valueOf(value));
String bidNo = strs[0];
String interestTime = strs[1];
String status = strs[2];
String planStatus = strs[3];
String yield = strs[4];
String endDate = strs[6];
String cycle = strs[7];
String financedAmount = strs[8];
String interestType = strs[9];
String penaltyAmount = strs[10];
String days = strs[11];
if("INIT".equals(status)){
String ouv = bidNo + pattern + "plan" + pattern + interestTime + pattern + planStatus + pattern + yield + pattern +
cycle + pattern + financedAmount + pattern + interestType + pattern + penaltyAmount + pattern + days + pattern + endDate;
outKey.set(bidNo);
outValue.set(ouv);
context.write(outKey,outValue);
}
} } public static class Reduce extends Reducer<Text,Text,Text,Text>{ private Text outValue = new Text();
private Pattern pattern = Pattern.compile(","); public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException{ Map<String,List<String>> planMap = new HashMap<String,List<String>>(); List<String> statisticsLst = new ArrayList<String>();
for(Text value : values){
String strs[] = pattern.split(String.valueOf(value));
String pbidNo = strs[0];
if("plan".equals(strs[1])){
if(planMap.containsKey(pbidNo)){
planMap.get(pbidNo).add(String.valueOf(value));
}else{
List<String> planLst = new ArrayList<String>();
planLst.add(String.valueOf(value));
planMap.put(pbidNo,planLst);
}
}else{
statisticsLst.add(String.valueOf(value));
}
} for(String value : statisticsLst){
String strs[] = pattern.split(String.valueOf(value));
String bidNo = strs[0];
String userId = strs[2];
String totalOnInvestedShare = strs[3];
String addShare = strs[4];
String addyield = strs[5];
if(null == planMap.get(bidNo) || 0 >= planMap.get(bidNo).size()){
continue;
}
String planBid = planMap.get(bidNo).get(0);
if(StringUtils.isBlank(planBid)){
continue;
}
String interestType = pattern.split(planBid)[7];
if("A1".equals(interestType)){
// 到期还本付息
for(String v : planMap.get(bidNo)){
String strp[] = pattern.split(v);
String interestTime = strp[2];
String yield = strp[4];
String cycle = strp[5];
BigDecimal interest = new BigDecimal(totalOnInvestedShare).multiply(new BigDecimal(yield)).divide(new BigDecimal(100),4);
BigDecimal addInterest = new BigDecimal(0);
if(StringUtils.isNotBlank(addShare) && StringUtils.isNotBlank(addyield)){
addInterest = new BigDecimal(addShare).multiply(new BigDecimal(addyield)).divide(new BigDecimal(100),4);
}
BigDecimal totalInterest = interest.add(addInterest).multiply(new BigDecimal(cycle)).divide(new BigDecimal(365),2);
String outv = userId + pattern + bidNo + pattern + interestTime + pattern + totalInterest + 0.00 + 0.00;
outValue.set(outv);
context.write(key,outValue);
}
}else{
// 按月付息,按季付息
for(String v : planMap.get(bidNo)){
String strp[] = pattern.split(v);
String interestTime = strp[2];
String yield = strp[4];
String days = strp[9];
String endDate = strp[10];
String penaltyTotalAmount = strp[8];
String financeAmount = strp[6];
BigDecimal interest = new BigDecimal(totalOnInvestedShare).multiply(new BigDecimal(yield)).divide(new BigDecimal(100),4);
BigDecimal addInterest = new BigDecimal(0);
if("null".equals(addShare) && "null".equals(addyield)){
addInterest = new BigDecimal(addShare).multiply(new BigDecimal(addyield)).divide(new BigDecimal(100),4);
}
BigDecimal totalInterest = interest.add(addInterest).multiply(new BigDecimal(days)).divide(new BigDecimal(365),2);
String planSttus = strp[3];
BigDecimal penalty = new BigDecimal(0);
BigDecimal capital = new BigDecimal(0);
if("ADVANCE".equals(planSttus)){
// 提前还款
penalty = new BigDecimal(penaltyTotalAmount).divide(new BigDecimal(financeAmount),2).multiply(new BigDecimal(totalOnInvestedShare));
totalInterest = totalInterest.add(penalty);
capital = new BigDecimal(totalOnInvestedShare);
}
/**
* 最后一次派息capital记成totalOnInvestedShare
*/
if(interestTime.equals(endDate)){
capital = new BigDecimal(totalOnInvestedShare);
}
String outv = userId + pattern + bidNo + pattern + interestTime + pattern + totalInterest +pattern + capital + pattern + penalty;
outValue.set(outv);
context.write(key,outValue);
}
}
} } } public static void main(String[] args) throws Exception{
Configuration config = new Configuration();
Job job = Job.getInstance(config);
job.setJobName("userPlanAmount");
job.setJarByClass(UserPlanAmount.class);
MultipleInputs.addInputPath(job,new Path(args[0]), TextInputFormat.class,StatisticsMap.class);
MultipleInputs.addInputPath(job,new Path(args[1]),TextInputFormat.class,PlanMap.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setOutputFormatClass(TextOutputFormat.class); FileOutputFormat.setOutputPath(job, new Path(args[2])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
两个map一个reduce(两个输入文件)的更多相关文章
- Java操作Hadoop、Map、Reduce合成
原始数据: Map阶段 1.每次读一行数据, 2.拆分每行数据, 3.每个单词碰到一次写个1 <0, "hello tom"> <10, "hello ...
- Java 将两个Map对象合并为一个Map对象
实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式. publi ...
- 一个 Map 函数、一个 Reduce 函数和一个 main 函数
MapReduce 最简单的 MapReduce应用程序至少包含 3 个部分:一个 Map 函数.一个 Reduce 函数和一个 main 函数.main 函数将作业控制和文件输入/输出结合起来.在这 ...
- Map集合的两种遍历方式
Map集合:即 接口Map<K,V> map集合的两种取出方式: 1.Set<k> keyset: 将map中所有的键存入到set集合(即将所有的key值存入到set中) ...
- scala - 从合并两个Map说开去 - foldLeft 和 foldRight 还有模式匹配
开发中遇到需求:合并两个Map集合对象(将两个对应KEY的值累加) 先说解决方案: ( map1 )) ) } 这特么什么鬼 (╯‵□′)╯""┻━┻☆))>○<) ...
- 从合并两个Map说开去 - foldLeft 和 foldRight 还有模式匹配
开发中遇到需求:合并两个Map集合对象(将两个对应Key的值累加) 先说解决方案: ( map1 /: map2 ) { )) ) } 首先: Scala中现有的合并集合操作不能满足这个需求 . 注意 ...
- scala 两个map合并,key相同时value相加/相减都可
scala 两个map合并,key相同时value相加 1.map自带的合并操作 2.map函数 2.1示例 2.2合并两个map 3.用foldLeft 3.1 语法 3.2 合并两个map 1.m ...
- js字符串长度计算(一个汉字==两个字符)和字符串截取
js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...
- 性能优化(一个)Hibernate 使用缓存(一个、两、查询)提高系统性能
在hibernate有三种类型的高速缓存,我们使用最频繁.分别缓存.缓存和查询缓存.下面我们使用这三个缓存中的项目和分析的优点和缺点. 缓存它的作用在于提高性能系统性能,介于应用系统与数据库之间而存在 ...
随机推荐
- BZOJ 2754: [SCOI2012]喵星球上的点名 [后缀数组+暴力]
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1906 Solved: 839[Submit][St ...
- c++项目范例
#include<iostream> #include<string.h> #include<stdlib.h> using namespace std; clas ...
- Nginx日志分析及脚本编写
在我们日常的运维中,当Nginx服务器正常运行后,我们会经常密切关注Nginx访问日志的相关情况,发现有异常的日志信息需要进行及时处理. 那今天我将跟大家一起来研究和分析Nginx日志,nginx默认 ...
- [Python Study Notes]内存信息
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- 用最简单的例子实现jQuery图片即时上传
[http://www.cnblogs.com/Zjmainstay/archive/2012/08/09/jQuery_upload_image.html] 最近看了一些jQuery即时上传的插件, ...
- iterm2 快捷键大全
Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iterm2 摆在那,基本上也就没有多少出场机会了 I ...
- Windows 桌面和文件夹的右键->打开命令行窗口
Windows 桌面和文件夹的右键->打开命令行窗口 1.先按下shift,再点鼠标右键运行CMD,(不是管理员权限) 上图是我已经加了右键的,并且 系统设置了 ps代替cmd,所以是“在此处 ...
- 拥抱.NET Core系列:MemoryCache 初识
Cache是一个绝大多数项目会用到的一个技术,说起到缓存可能就联想到 Set.Add.Get.Remove.Clear 这几个方法.那么在.NET Core中微软给我们带来了什么样的缓存体验呢?今天我 ...
- Python常用数据结构之collections模块
Python数据结构常用模块:collections.heapq.operator.itertools collections collections是日常工作中的重点.高频模块,常用类型由: 计数器 ...
- bzoj 1188 [HNOI2007]分裂游戏 SG函数 SG定理
[HNOI2007]分裂游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1394 Solved: 847[Submit][Status][Dis ...