两个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(两个输入文件)的更多相关文章

  1. Java操作Hadoop、Map、Reduce合成

    原始数据: Map阶段 1.每次读一行数据, 2.拆分每行数据, 3.每个单词碰到一次写个1 <0, "hello tom"> <10, "hello ...

  2. Java 将两个Map对象合并为一个Map对象

    实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式. publi ...

  3. 一个 Map 函数、一个 Reduce 函数和一个 main 函数

    MapReduce 最简单的 MapReduce应用程序至少包含 3 个部分:一个 Map 函数.一个 Reduce 函数和一个 main 函数.main 函数将作业控制和文件输入/输出结合起来.在这 ...

  4. Map集合的两种遍历方式

    Map集合:即 接口Map<K,V> map集合的两种取出方式:    1.Set<k> keyset: 将map中所有的键存入到set集合(即将所有的key值存入到set中) ...

  5. scala - 从合并两个Map说开去 - foldLeft 和 foldRight 还有模式匹配

    开发中遇到需求:合并两个Map集合对象(将两个对应KEY的值累加) 先说解决方案: ( map1 )) ) } 这特么什么鬼  (╯‵□′)╯""┻━┻☆))>○<)  ...

  6. 从合并两个Map说开去 - foldLeft 和 foldRight 还有模式匹配

    开发中遇到需求:合并两个Map集合对象(将两个对应Key的值累加) 先说解决方案: ( map1 /: map2 ) { )) ) } 首先: Scala中现有的合并集合操作不能满足这个需求 . 注意 ...

  7. 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 ...

  8. js字符串长度计算(一个汉字==两个字符)和字符串截取

    js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...

  9. 性能优化(一个)Hibernate 使用缓存(一个、两、查询)提高系统性能

    在hibernate有三种类型的高速缓存,我们使用最频繁.分别缓存.缓存和查询缓存.下面我们使用这三个缓存中的项目和分析的优点和缺点. 缓存它的作用在于提高性能系统性能,介于应用系统与数据库之间而存在 ...

随机推荐

  1. POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]

    传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...

  2. asp.net core 中 sql server 2017 数据库连接测试

    使用sql server 2017 进行连接: 配置appsettings.json文件 { "ConnectionStrings": { "DefaultConnect ...

  3. 读书共享 Primer Plus C-part 12

    第十四章 结构和其他数据形式 1.关于上struct与union 的区别 #include<stdio.h> typedef union Book_u { int pags; int mo ...

  4. Wpf DataGridCheckBoxColumn 问题

    使用DataGridCheckBoxColumn  binding一个布尔属性时,发现无法checkbox无法勾选, 并且HeaderTemplate中的checkbox无法获取到viewmodel的 ...

  5. 微信小程序内嵌网页 网站生成小程序

    1. 进入小程序后台 - 设置 - 开发设置 2. 添加业务域名(小程序只支持https) 3. 小程序代码 <web-view src="网址"></web-v ...

  6. jq模仿雨滴下落的动画

    效果如图: 实现思路:定时器每隔x秒生成宽高.下落速度(即动画执行时间).left随机的div. 1.CSS: body{ overflow: hidden;/*这是为了防止出现滚动条*/ } .co ...

  7. intellij idea maven 工程生成可执行的jar

    新建maven 工程 写hello world 修改pom.xml 文件 <build> <pluginManagement> <plugins> <plug ...

  8. 【Unity3D技术文档翻译】第1.8篇 AssetBundles 问题及解决方法

    上一章:[Unity3D技术文档翻译]第1.7篇 AssetBundles 补丁更新 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Deve ...

  9. 市面上有没有靠谱的PM2.5检测仪?如何自己动手制作PM2.5检测仪

     市面上能买到的11中常见的pm2.5检测仪 网上大佬实测并不是很准,我这里没测过(全买下来有点贵,贫穷限制了我的想象力) 这些检测仪多数是复合式.多功能的空气质量检测仪.具体就不一一介绍了.这篇文章 ...

  10. 老男孩Python全栈开发(92天全)视频教程 自学笔记05

    day5课程内容: 集成开发环境(IDE) VIM #经典的Linux下的文本编辑器 Eclipse #Java IDE Visual Studio #微软开发的IDE notepad++ subli ...