传参关键代码:

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

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. Data source rejected establishment of connection, message from server: "Too many connections"

    详细错误信息: Caused by: com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source ...

  2. e792. 建立一个包括所有数据的SpinnerListModel

    By default, if the user is browsing the values in a SpinnerListModel, the iteration stops when eithe ...

  3. doctest初次体验

    测试代码放在两个地方才有效果,一个是模块开头,一个是函数声明语句的下一行 doctest 的概念模型 在python的官方文档中,对doctest是这样介绍的: doctest模块会搜索那些看起来像是 ...

  4. Visual Studio主题与配色方案

    有个性的开发人员总是喜欢使用属于的主题和配色方案,它们可以看出开发者的个性,更改它们可以缓解审美疲劳,总之选择一个适合自己的解决方案可能极大的增加自己的编码舒适度. 配色方案的选择和使用 手动修改Vi ...

  5. om.ibm.ws.jsp.translator.JspTranslationException:JSPG0227E解决办法

    原文链接:http://blog.163.com/rambosir@126/blog/static/124116406201212834417/ 今天在完成功能后,将jsp页面部署至websphere ...

  6. 配置防盗链 访问控制Directory 访问控制FilesMatch

  7. Unity3d游戏开发中使用可空类型(Nullable Types)

    你怎么确定一个Vector3,int,或float变量是否被分配了一个值?一个方便的方式就是使用可空类型! 有时变量携带重要信息,但仅仅有在特定的游戏事件发生时触发.比如:一个角色在你的游戏可能闲置, ...

  8. js pjax 和window.history.pushState,replaceState

    原文:http://blog.linjunhalida.com/blog/pjax/ github:https://github.com/defunkt/jquery-pjax 什么是pjax? 现在 ...

  9. WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files

    webjars网站https://www.webjars.org/,这里将很多的东西都打包成了jar包,想要用什么只需要导入相关的依赖就可以了. 比如springboot会用到jquery,webja ...

  10. 7 Django系列之关于bootstrap-table插件的简单使用

    preface 我们在前端html页面的时候做表格最常用的就是jinja2渲染,这样的好处是无须引用外部插件,直接使用就行,方便.但是不好的就是前端CSS样式以及其他表格排序筛选功能需要自己写,增加了 ...