MR 数据过滤
MR:
package com.euphe.filter; import com.euphe.util.HUtils;
import com.euphe.util.Utils;
import com.euphe.util.standardUtil.Location;
import com.euphe.util.standardUtil.StringListTools;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import static com.euphe.util.standardUtil.Shufflter.shufflter; public class FilterJob extends Configured implements Tool {
public static class Map extends Mapper<Object, Text, Text, Text> {
private static Text text = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
List<String> FirstList = new ArrayList<String>();
FirstList = StringListTools.StringToList(line,"\t");
String time = FirstList.get(Location.DATE_TIME);
context.write(new Text(time), new Text(line));//时间作为key,一行作为value
}
} public static class Reduce extends Reducer<Text, Text, NullWritable, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
try{
String line = "";
String result = "";
boolean fflg = false;
List<String> resultList = new ArrayList<String>();
for(Text value : values){//对一个分区的每个数据进行处理
line = value.toString();
fflg = shufflter(line);//对这行进行过滤 if(fflg){
resultList.add(line);//满足条件添加进list
}
}
result = StringListTools.ListToString(resultList, "\n");
context.write(NullWritable.get(), new Text(result));
}catch (Exception e){
e.printStackTrace();
}
}
} @Override
public int run(String[] args) throws Exception {
Configuration conf = HUtils.getConf();
conf.set("mapreduce.job.jar", Utils.getRootPathBasedPath("WEB-INF/jars/filter.jar"));//打包运行
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();//解析命令行参数
if (otherArgs.length !=2) {//要求必须有输入和输出路径两个参数
System.err.println("Usage: com.euphe.filter.FilterJob <in> <out>");
System.exit(2);
}
Job job = Job.getInstance(conf,"Filter input :"+otherArgs[0]+" to "+otherArgs[1]);
job.setJarByClass(FilterJob.class);
job.setMapperClass(FilterJob.Map.class);
job.setReducerClass(FilterJob.Reduce.class);
job.setNumReduceTasks(1); //设置map输出的key value
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
//设置reducer输出的key,value类型
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
FileSystem.get(conf).delete(new Path(otherArgs[1]), true);//调用任务前先删除输出目录
return job.waitForCompletion(true) ? 0 : 1;
}
}
shufflter函数:
package com.euphe.util.standardUtil; import java.util.ArrayList;
import java.util.List; public class Shufflter {
public static boolean shufflter(String line){
boolean fflg = true;
List<String> tmpList = new ArrayList<String>();
tmpList = StringListTools.StringToList(line, "\t"); try{//这四个元素不全为空时返回bool值为true
String dk1 = tmpList.get(Location.DK1);
String of = tmpList.get(Location.osFamily);
String uf = tmpList.get(Location.uaFamily);
String ty = tmpList.get(Location.type);
if(dk1.equals("null")
&& of.equals("unknown")
&& uf.equals("unknown")
&& ty.equals("unknown"))
fflg = false; }catch (Exception e){
e.printStackTrace();
}
return fflg;
}
}
Location工具:
package com.euphe.util.standardUtil;
public class Location {
//原始数据中各属性的位置
public static final int DATE_TIME = 0;
//shufflter阶段各属性位置
public static final int DK1 = 5;
public static final int osFamily = 8;
public static final int uaFamily = 9;
public static final int type = 13;
}
StringList工具:
package com.euphe.util.standardUtil; import java.util.ArrayList;
import java.util.List; public class StringListTools {
public static List<String> StringToList(String str, String seperator){
//该函数读入日志文件的一行,根据分隔符将各个项保存到List中
if(str == null)
return null; List<String> strList = new ArrayList<String>();
String[] strArray = str.split(seperator);
for(String text : strArray)
strList.add(text);
return strList;
} public static String ListToString(List<String> tempList, String seperator) {
//该函数根据分隔符将List保存为String
if (tempList == null)
return null; String temp = new String();
for(int i = 0; i < tempList.size()-1; i++){
temp = temp + tempList.get(i) + seperator;
}
temp = temp + tempList.get(tempList.size()-1);
return temp;
}
}
MR 数据过滤的更多相关文章
- Python Streaming实战2: Join的实现与数据过滤
Hadoop Join 与 Not In的实现 (一)源数据与要实现的查询 1. 要实现的查询 select a.sid ,a.name ,b.course ,b.score from Studen ...
- ABP框架 - 数据过滤
文档目录 本节内容: 简介 预定义过滤 ISoftDelete 何时可用? IMustHaveTenant 何时可用? IMayHaveTenant 何时可用? 禁用过滤 关于using声明 关于多租 ...
- C#实现通用数据过滤窗体
最近一直在做WINFORM项目,所以经常有些新的想法或尝试与大家分享,之前与大家分享了通用窗体遮罩层.通用可附加数据绑定的DataGridView.窗体渐显,今天来分享一个大家在其它软件中常见的功能: ...
- php数据过滤函数与方法示例【转载】
1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval( ...
- PHP数据过滤
1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval ...
- SQL学习之高级数据过滤
一.高级数据过滤之IN操作符 IN 操作符用来指定条件范围,范围中的每个条件都可以进行匹配.IN取一组由逗号分隔.括在圆括号中的合法值.代码如下: select ItemId,ItemName,Che ...
- FastReport 数据过滤
FastReport 数据过滤 在DataBind 的 OnBeforePrint 设置条件 例:显示 大于0 的数据 procedure MasterData1OnBeforePrint(Sen ...
- .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤
转:http://www.cnblogs.com/jaxu/archive/2011/08/04/2127365.html 我们见过Excel中的数据过滤功能,可以通过点击表头上的下拉列表来实现数据的 ...
- ABP文档笔记 - 数据过滤
预定义的过滤 ISoftDelete 软删除过滤用来在查询数据库时,自动过滤(从结果中抽取)已删除的实体.如果一个实体可以被软删除,它必须实现ISoftDelete接口,该接口只定义了一个IsDele ...
随机推荐
- MVC - 13.验证
1.注解验证-Required-StringLength-Range-Regular. 1.1.验证方式 [Required], [StringLength], [Range], 和 [Regular ...
- Linux:hping高级主机扫描
https://www.aliyun.com/jiaocheng/167107.html https://blog.csdn.net/weixin_39762926/article/details/7 ...
- sublime text光标移入移出括号的快捷键设置
使用sublime text每次输入完一个函数或者标签,光标一般都是停留在括号中间,要跳出来要使用左右方向键或者end键 这俩键键区比较远,按起来麻烦,可以自己设置快捷键实现跳出的功能. 原来的快捷键 ...
- bzoj 1485 卡特兰数 + 分解因子
思路:打表可以看出是卡特兰数,但是模数不一定是素数,所以需要分解一下因数. #include<bits/stdc++.h> #define LL long long #define fi ...
- 【初探移动前端开发】jQuery Mobile 二
本文例子请使用手机查看 List列表 在移动设备平台下,由于移动设备屏幕比较小,我们又是用手在上面点击的触屏方式,传统的列表模式在手机上就不太友好了. 虽然HTML5与CSS3提供了强大的界面实现方案 ...
- CentOS 7.4 下面安装 jdk 10 的一点总结
CentOS 7.4 下面安装 jdk 10 的一点总结 一.前期工作 1.检验系统原版本 [root@zck ~]# java -version java version "1.7.0_& ...
- react native native module
React Native Native Modules,官方地址:https://facebook.github.io/react-native/docs/native-modules-android ...
- php获取农历、节日、节气
/* * 农历 节气 节日 * edit: www.jbxue.com */ header("Content-Type:text/html;charset=utf-8"); cla ...
- 【Java多线程】线程池学习
Java线程池学习 众所周知,Java不仅提供了线程,也提供了线程池库给我们使用,那么今天来学学线程池的具体使用以及线程池基本实现原理分析. ThreadPoolExecutor ThreadPool ...
- IN 运算符
在前面已经介绍了IN运算符的简单使用,使用IN运算符可以用来匹配一个固定集合中的某一项.比如下面的SQL语句检索在2001.2003和2005年出版的所有图书: SELECT * FROM T_Boo ...