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 数据过滤的更多相关文章

  1. Python Streaming实战2: Join的实现与数据过滤

    Hadoop Join 与 Not In的实现  (一)源数据与要实现的查询 1. 要实现的查询 select a.sid ,a.name ,b.course ,b.score from Studen ...

  2. ABP框架 - 数据过滤

    文档目录 本节内容: 简介 预定义过滤 ISoftDelete 何时可用? IMustHaveTenant 何时可用? IMayHaveTenant 何时可用? 禁用过滤 关于using声明 关于多租 ...

  3. C#实现通用数据过滤窗体

    最近一直在做WINFORM项目,所以经常有些新的想法或尝试与大家分享,之前与大家分享了通用窗体遮罩层.通用可附加数据绑定的DataGridView.窗体渐显,今天来分享一个大家在其它软件中常见的功能: ...

  4. php数据过滤函数与方法示例【转载】

    1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval( ...

  5. PHP数据过滤

    1.php提交数据过滤的基本原则  1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval ...

  6. SQL学习之高级数据过滤

    一.高级数据过滤之IN操作符 IN 操作符用来指定条件范围,范围中的每个条件都可以进行匹配.IN取一组由逗号分隔.括在圆括号中的合法值.代码如下: select ItemId,ItemName,Che ...

  7. FastReport 数据过滤

    FastReport 数据过滤 在DataBind 的 OnBeforePrint 设置条件   例:显示 大于0 的数据 procedure MasterData1OnBeforePrint(Sen ...

  8. .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤

    转:http://www.cnblogs.com/jaxu/archive/2011/08/04/2127365.html 我们见过Excel中的数据过滤功能,可以通过点击表头上的下拉列表来实现数据的 ...

  9. ABP文档笔记 - 数据过滤

    预定义的过滤 ISoftDelete 软删除过滤用来在查询数据库时,自动过滤(从结果中抽取)已删除的实体.如果一个实体可以被软删除,它必须实现ISoftDelete接口,该接口只定义了一个IsDele ...

随机推荐

  1. iis频繁奔溃,求大神帮忙分析dump

    直接上图了 上图三个错误最近频繁出现,出现一次iis就奔溃一次,抓取的dump分析后如下: Couldn't resolve error at 'ls' :> !analyze -v ***** ...

  2. Ubuntu 18.04安装 Sublime

    作为一款强大的源代码编辑器,当然需要一手流利的命令来安装它. 命令: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | s ...

  3. oracle 优化方案小记

    1. 目前状况 1.1 表空间未合理规划,导致所有的用户下的所有表都创建在默认的表空间下 oracle 使用过程中未针对特定数据表进行特定的表空间规划,导致目前实例中所有的数据库表都存储中默认的表空间 ...

  4. Project interpreter not specified(eclipse+pydev) (转)

    [小记] 最近因为想配置Android的开发环境,把原来的MyEclipse5.5删了,下载了最新的Eclipse3.7版本,因为之前在进行Python开 发,就下载了最新的Pydev2.4版本,安装 ...

  5. python spyder 今天突然打不开了【已解决】

    python spyder 我是设置开机启动的,先出现dos窗口,然后是蜘蛛网,后面就什么都没有了.然后百度了半天,在csdn看到一篇文章,试了一下,内牛满面! 方法:C:\Documents and ...

  6. Mac OS X 更新JAMF域控配置

    在终端执行以下命令即可更新jamf域控配置属性 sudo jamf mcx # 应用被管理的配置信息 sudo jamf policy -trigger # 检查触发器策略 sudo jamf rec ...

  7. RabbitMQ (十四) 普通集群

    上篇文章把单机集群搭建好了,可以开始验证普通集群的相关功能了. 我们首先在管理后台(15672,15673 都可以)添加一个用户,并用新用户登录,添加一个虚拟主机 由于是在一台机器上模拟集群,所以我们 ...

  8. oracle return code 2112

    SQL-02112 SELECT..INTO returns too many rows Cause: A SELECT...INTO statement returned more rows tha ...

  9. ACM的奇计淫巧_bitset优化

    什么是bitset bitset 是STL库中的二进制容器,根据C++ reference 的说法,bitset可以看作bool数组,但优化了空间复杂度和时间复杂度,并且可以像整形一样按位与或. 使用 ...

  10. JQuery总结+实例

    JQuery是什么? Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...