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 ...
随机推荐
- Go语言标准库之log包
用来作日志log输出的, 比较易懂. 今天周六啊,在公司加班学习一下呀. package main import ( "log" ) func init() { log.SetPr ...
- Team Service 编译项目并生成项目
第一步:生成GitHub帐号连接 在Service中选择Github 在弹出的GitHub连接中点击授权,即会弹出另一个窗口,输入Github的用户名及口令,即可授权. 第二步:创建Build定义 解 ...
- CSU 2151 集训难度【多标记线段树】
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2151 Input 第一行三个数n,m,v0 表示有n名萌新和m次调整,初始时全部萌新的集训难度 ...
- 1.L 查询关键字 HEXLOC
Take the compile listing for the program that has either OFFSET or LIST option selected. Use the OFF ...
- matlab学习之求解函数的根和极小值
只是用来求解函数的部分一个根,具体算法没查询,只是调用自带的函数 代码如下 % 求函数零点和极小值 xiszero=func(0) x=-1:0.1:1.5; y=func(x); plot(x,y) ...
- noip 1999 回文数
题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...
- [NOI2015]小园丁与老司机(DP+上下界最小流)
由于每行点的个数不超过1000,所以行内DP可以使用$O(n^2)$算法. 先找到每个点所能直接到达的所有点(x,y,x+y或x-y相同),用排序实现. 第一问:以行为阶段,对于每行,暴力枚举最有路径 ...
- xtuoj 1235 CQRXLB(博弈论)
CQRXLB Accepted : 19 Submit : 40 Time Limit : 1000 MS Memory Limit : 65536 KB CQRXLB Problem Des ...
- CodeForces - 995B Suit and Tie
题面在这里! 明明可以出成n<=1e5但是因为拒绝写数据结构而只出到n<=100,,,出题人真的很棒棒.. 一个显然的贪心就是,把和当前序列最左端的数匹配的数移到它的右边,这样迭代下去总是 ...
- 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ...