传参关键代码:

//从配置文件获取参数,必须在作业创建的前面

conf.addResource("hadoop-bigdata.xml");
keepUrl=conf.get("KeepUrlString","");
filterUrl=conf.get("FilterUrlString","");
conf.set("FilterUrl", filterUrl);
conf.set("KeepUrl", keepUrl);
//获取参数
String fstr=context.getConfiguration().get("FilterUrl");
String kstr=context.getConfiguration().get("KeepUrl");
 
package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class FilterUrl { public static class FilterUrlMap extends Mapper<Object,Text,Text,Text>
{
private static Text word=new Text(); public void map(Object key,Text values,Context context) throws
IOException,InterruptedException
{
boolean fflag=false;
boolean kflag=false;
//获取参数
String fstr=context.getConfiguration().get("FilterUrl");
String kstr=context.getConfiguration().get("KeepUrl");
//循环的方式
// StringTokenizer fitr=new StringTokenizer(fstr,"|");
// StringTokenizer kitr=new StringTokenizer(kstr,"|"); //正则表达式,替换特殊字符
Pattern filter=Pattern.compile(fstr.replace(".","\\."));
Pattern keep=Pattern.compile(kstr.replace(".","\\.")); //有一大段的内容
StringTokenizer itr = new StringTokenizer(values.toString(),"\n");
String url="";
while(itr.hasMoreTokens())
{
url=itr.nextToken().toLowerCase(); //正则表达式的模式匹配
Matcher mkeep=keep.matcher(url);
if(mkeep.find())
{
kflag=true;
Matcher mfilter=filter.matcher(url);
if(mfilter.find())
fflag=true;
} //需要保留的URL
/**
//循环的模式匹配
while(kitr.hasMoreTokens())
{
if(url.indexOf(kitr.nextToken())>0)
{
kflag=true;
break;
}
}
//需要过滤掉的URL
while(kflag && fitr.hasMoreTokens())
{
if(url.indexOf(fitr.nextToken())>0)
{
fflag=true;
break;
}
}
*/
//是需要保留的并且不是需要过滤掉的URL
if(kflag && !fflag)
{
word.set(url);
context.write(word,new Text(""));
}
}
}
}
public static class FilterUrlReduce extends Reducer<Text,Text,Text,Text>
{
public void reduce(Text key,Iterable<Text> values,Context context) throws
IOException,InterruptedException
{
context.write(key, new Text(""));
}
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf=new Configuration();
String filterUrl=new String();
String keepUrl=new String();
if(args.length!=2)
{
System.err.println("please input two args:<in> <out>");
System.exit(2);
}
//从配置文件获取参数,必须在作业创建的前面
conf.addResource("hadoop-bigdata.xml");
keepUrl=conf.get("KeepUrlString","");
filterUrl=conf.get("FilterUrlString","");
conf.set("FilterUrl", filterUrl);
conf.set("KeepUrl", keepUrl); //这句必须在参数设置语句的后面,否则参数获取失败
Job job=new Job(conf,"filter url");
job.setJarByClass(FilterUrl.class);
job.setMapperClass(FilterUrlMap.class);
job.setReducerClass(FilterUrlReduce.class);
//job.setNumReduceTasks(0); //如果不要的话会有多个小的文件
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
System.exit(job.waitForCompletion(true)?0:1);
}
}

需要从配置文件获取的参数:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration>
<property>
<!--C net keep url string -->
<name>KeepUrlString</name>
<value>anjueke.com|soufun.com</value>
</property>
<property>
<!--filter url-->
<name>FilterUrlString</name>
<value>.js|.jpg|.jpeg|.gif|.png|.css|error.html</value>
</property>
</configuration>

hadoop参数传递的更多相关文章

  1. hadoop参数传递实例

    要求: 根据输入文件中的信息,计算出某几个字符串出现的个数 输入文件格式:xxx,xxx,xxx,xx,x,x,xxx,x,x,xx,x,x,x,x,x,x,x, 输出文件:xx    10 xx   ...

  2. [Hadoop in Action] 第7章 细则手册

    向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序   1.向任务传递作业定制的参数        在编写Mapper和Reducer时,通常会想让一些地方可以配 ...

  3. Ubuntu下eclipse开发hadoop应用程序环境配置

    第一步:下载eclipse-jee-kepler-SR2-linux-gtk-x86_64.tar.gz 注意:如果电脑是64位,就下载linux下的64位eclipse,不要下载32位的eclips ...

  4. Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  5. Hadoop第6周练习—在Eclipse中安装Hadoop插件及测试(Linux操作系统)

    1    运行环境说明 1.1     硬软件环境 1.2     机器网络环境 2    :安装Eclipse并测试 2.1     内容 2.2     实现过程 2.2.1   2.2.2   ...

  6. Hadoop学习笔记2---配置详解

    配置系统是复杂软件必不可少的一部分,而Hadoop配置信息处理是学习Hadoop源代码的一个很好的起点.现在就从Hadoop的配置文件谈起. 一.Hadoop配置格式 Hadoop配置文件格式如下所示 ...

  7. hadoop streaming 编程

    概况 Hadoop Streaming 是一个工具, 代替编写Java的实现类,而利用可执行程序来完成map-reduce过程.一个最简单的程序 $HADOOP_HOME/bin/hadoop jar ...

  8. Nutch+Hadoop集群搭建

    转载自:http://www.open-open.com/lib/view/open1328670771405.html 1.Apache Nutch    Apache Nutch是一个用于网络搜索 ...

  9. Hadoop MapReduce开发最佳实践(上篇)

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

随机推荐

  1. 无法定位序数 12384 于动态链接库 mfc90.dll

    场景: 运行 vs2008 编译生成的一个 exe 错误: 无法定位序数 12384 于动态链接库 mfc90.dll 原因: 缺少vc++ 2008 运行库. vcredist 全称Microsof ...

  2. HTML5实现摇一摇的功能(实测后)--转

    eviceMotionEvent(设备运动事件)返回设备有关于加速度和旋转的相关信息.加速度的数据将包含三个轴:x,y和z(示意如下图所 示,x轴横向贯穿手机屏幕或者笔记本键盘,y轴纵向贯穿手机屏幕或 ...

  3. Python之Python 安装环境搭建

    方法和步骤: python安装包的下载:http://111.13.109.34:83/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/www.python.org/ftp/python ...

  4. (源)VC助手VA破解使用指南

    一般情况下,你下载的破解版的VC助手,要么自带的有一个名为VA_X.dll的文件,要么是有一个可运行的破解程序,根据不同的情况进行如下操作,只要你下载的安装文件没有问题,就能正确打上破解补丁.网上有一 ...

  5. VMware虚拟CentOS 6.5在NAT模式下配置静态IP地址及Xshell远程控制配置

    VMware虚拟CentOS 6.5在NAT模式下配置静态IP地址及Xshell远程控制配置 标签: LinuxXshellCentOS 2016-10-15 04:58 127人阅读 评论(0) 收 ...

  6. 如何使用Matrix对bitmap的旋转与镜像水平垂直翻转

    Bitmap convert(Bitmap a, int width, int height){int w = a.getWidth();int h = a.getHeight();Bitmap ne ...

  7. SpringBoot学习:使用spring-boot-devtools进行热部署

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 pom.xml添加依赖: <!--支持热启动jar包--> <depe ...

  8. C# 将DateTime.Now.DayOfWeek的值转为中文星期

    1.  如果常规使用DateTime.Now.DayOfWeek则得到是英文,如果加ToString后再加上相关参数(“G”.“g”.“X”.“x”.“F”.“f”.“D”或“d”),则还会有所变化. ...

  9. Linux入门基础教程之Linux下软件安装

    Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc AP ...

  10. JSP——MVC模式+Servlet生命周期

    设计模式MVC模式 模型MODEL操作数据库的增删改查——javaBean 视图VIEW显示数据——JSP 控制器CONTROLLER响应用户的——servlet Model(模型),是程序的主体部分 ...