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 ...
随机推荐
- OpenAcc笔记——update
program main use omp_lib use openacc implicit none real,allocatable:: v1(:) integer length, idx call ...
- SGU 289. Challenging Tic-Tac-Toe
注意一个问题就是不合法状态的判定.一个是点数不对,一个是X赢了,但是0接着下了一个子,一个是0赢了,但X也接着下了子,判断一下就行了. 做法是直接搜索,然后调参数...比较难懂的说. #include ...
- C#读取RSS源,并利用Solr索引
折磨我几天的问题今天终于解决了,分享一下近期solr使用的一些经验. 本来是用nutch在爬取页面,可是客户需要爬取RSS,而且可以识别那些页面是通过RSS源抓取出来的.nutch虽然自带解析RSS的 ...
- JavaSwing 版本的简单扫雷游戏
JavaSwing 版本的简单扫雷游戏 一.扫雷游戏的基本规则 1.扫雷游戏分为初级.中级.高级和自定义四个级别. 单击游戏模式可以选择"初级"."中级".&q ...
- 转:智能模糊测试工具 Winafl 的使用与分析
本文为 椒图科技 授权嘶吼发布,如若转载,请注明来源于嘶吼: http://www.4hou.com/technology/2800.html 注意: 函数的偏移地址计算方式是以IDA中出现的Imag ...
- python No migrations to apply
错误显示: “No migrations to apply” 错误情况:python在通过model同步数据库时,提示 No migrations to apply 查看数据库,新表没有被创建,只是 ...
- 洛谷——P2009 跑步
P2009 跑步 题目背景 跑步是一项有意思的运动,尤其是可以开发人的脑筋.常神牛很喜欢跑步. 题目描述 常神牛跑步的场地是一个多边形(边数≤20,每个顶点用英文大写字母表示),并且在这个多边形内部, ...
- 【kubernetes】ubuntu14.04 64位 搭建kubernetes过程
背景: Kubernetes介绍:http://kubernetes.io/docs/getting-started-guides/ github地址:https://github.com/kuber ...
- 【BZOJ 1455】 1455: 罗马游戏 (可并堆-左偏树+并查集)
1455: 罗马游戏 Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那 ...
- 「2018山东一轮集训」 Tree
为什么出题人这么毒瘤啊??!!一个分块还要带log的题非要出成n<=2*1e5....... 为了卡过最后两个点我做了无数常数优化,包括但不限于:把所有线段树改成 存差分的树状数组:把树剖求LC ...