两个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. linux指令札记

    1.有关文件压缩解压缩:Linux下自带了一个unzip的程序可以解压缩文件,解压命令是:unzip filename.zip 同样也提供了一个zip程序压缩zip文件,命令是 zip filenam ...

  2. 模拟器配置使用Fildder进行抓包,包含Https+证书

    1.首先,百度检索.参考别人的,大致上都是到安装证书就失败了.我后面只说几个关键点. 2.安装证书,必须设置屏幕密码.我最开始使用把cef拷贝到,手机结果出现bug,安装不了.后来采用了在手机内部访问 ...

  3. IDEA设置优化

    默认会开很多的功能,但是有些功能暂时用不到,于是想屏蔽掉. Duplicated Code冗余代码提示功能 先找到设置路径Settings -> Editor -> Inspections ...

  4. php使用file_get_contents请求微信接口失败

    windows下的php,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了.Linux下的PHP,就必须安装openssl模块,安装好了以后就 ...

  5. 使用Docker link搭建PHP开发环境

    一般我们会把nginx.php都安装在同一个容器,为了扩展方便,我们希望nginx和php分开.那么就可以使用docker link命令实现这一目的. 需要的镜像: nginx 1.12.2 php( ...

  6. 4、flask之分页插件的使用、添加后保留原url搜索条件、单例模式

    本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...

  7. zip-gzip-bzip2_压缩文件

    问:为什么要压缩文件? 答:方便传输,因为压缩的文件容量会比较小        存储所使用的空间也会比较小 ---> 备份   Windows里的压缩软件:WinRAR.Zip.好压.2345 ...

  8. Python常用数据结构之collections模块

    Python数据结构常用模块:collections.heapq.operator.itertools collections collections是日常工作中的重点.高频模块,常用类型由: 计数器 ...

  9. Python——文件操作详解

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  10. 《设计模式之禅》--设计模式大PK

    创建类模式包括工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式. 其中单例模式要保持在内存中只有一个对象,原型模式是要求通过复制的方式产生一个新的对象. [工厂方法(抽象工厂) VS 建造者 ...