传参关键代码:

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

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. VMware Fusion 5 正式版序列号

    HV4KJ-2X10K-VZ768-DRAGP-8CU2F MY63N-D0HE2-0ZXC1-HV954-937JL

  2. Mac 下 Homebrew(类似CentOS下的yum)简介及安装

    Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...

  3. 如何找回Ucenter创始人密码,账号无需修改

    UCenter 创始人的密码非常重要,忘记或丢失后,就不能进入 UCenter 进行用户和数据的管理,也会对站点造成安全隐患.由于 UCenter 的密码是采用两次 md5 加一个随机数的形式加密的, ...

  4. [lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)

    -- 演示将多条记录数据组合成一条sql插入语句(for mysql) function getTpl0(tname) -- 获取表各个字段 local t = { tpl_pack = {" ...

  5. RTC教程

    Tutorial: Get started with Rational Team Concert Getting Started with Jazz Source Control RTC入门教程及冲突 ...

  6. java String.intern();

    0.引言 什么都先不说,先看下面这个引入的例子: String str1 = new String("SEU")+ new String("Calvin"); ...

  7. spark shell学习笔记

    http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html

  8. ios模拟器已安装但xcode无法显示

    最近把系统抹盘重装了, 然后用Time Machine恢复到原始状态, 一切安好, 但是使用xcode的时候发现一个模拟器都没有了: 各种折腾, 重装SDK啊, 重装xcode啊,最后发现, 如果你的 ...

  9. yum和apt-get用法及区别

    https://www.cnblogs.com/garinzhang/p/diff_between_yum_apt-get_in_linux.html

  10. anaconda-ks.cfg详解

    https://blog.csdn.net/whyhonest/article/details/7555229