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 ...
随机推荐
- Java学习笔记(二)——Java操作properties文件
[前面的话] 前段时间在学习和玩java web相关的东西,对于这些技术,一边学习,一边做东西,一边总结,希望可以一边成长和有所收获.有时总是思考太多反而成为了前进的阻力,所以对于生活还是简单一些,不 ...
- AC日记——[Hnoi2017]影魔 bzoj 4826
4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...
- logrotate日志轮转
1)基本介绍 适合应用服务日志,系统日志按天切割 如果没有日志轮转,日志文件会越来越大 将丢弃系统中最旧的日志文件,以节省空间 logrotate本身不是系统守护进程,它是通过计划任务crond每天执 ...
- react native 增加react-native-storage
现时需要使用react-native-storage本地存储 第一步:配置storage主文件 mystorage.js import { AsyncStorage } from 'react-nat ...
- Java介绍
Java简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推 ...
- Spring学习的切入点
spring是个顶级的框架,这话没毛病.很多人想把它征服,想去阅读它的源码,弄懂它的设计思想,从中学到精粹. 但是很多次打开后,看到庞大的体系结构,就懵逼了,不知从何入手. 我在这里总结下学习spri ...
- 北方大学 ACM 多校训练赛 第七场 C Castle(LCA)
[题意]给你N个点,N条不同的边,Q次询问,求出u,v之间的最短路. [分析]题意很简单,就是求最短路,但是Q次啊,暴力DIJ?当然不行,观察到这个题边的数目和点的数目是相同的,也就是说这个图是由一棵 ...
- Python开发基础-Day11内置函数补充、匿名函数、递归函数
内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: divmod(a, b) #a.b为数字,a为除数,b ...
- AC自动机及KMP练习
好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...
- 【树形dp】Rebuilding Roads
[POJ1947]Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11934 Accep ...