hadoop参数传递
传参关键代码:
//从配置文件获取参数,必须在作业创建的前面
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参数传递的更多相关文章
- hadoop参数传递实例
要求: 根据输入文件中的信息,计算出某几个字符串出现的个数 输入文件格式:xxx,xxx,xxx,xx,x,x,xxx,x,x,xx,x,x,x,x,x,x,x, 输出文件:xx 10 xx ...
- [Hadoop in Action] 第7章 细则手册
向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序 1.向任务传递作业定制的参数 在编写Mapper和Reducer时,通常会想让一些地方可以配 ...
- Ubuntu下eclipse开发hadoop应用程序环境配置
第一步:下载eclipse-jee-kepler-SR2-linux-gtk-x86_64.tar.gz 注意:如果电脑是64位,就下载linux下的64位eclipse,不要下载32位的eclips ...
- Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- Hadoop第6周练习—在Eclipse中安装Hadoop插件及测试(Linux操作系统)
1 运行环境说明 1.1 硬软件环境 1.2 机器网络环境 2 :安装Eclipse并测试 2.1 内容 2.2 实现过程 2.2.1 2.2.2 ...
- Hadoop学习笔记2---配置详解
配置系统是复杂软件必不可少的一部分,而Hadoop配置信息处理是学习Hadoop源代码的一个很好的起点.现在就从Hadoop的配置文件谈起. 一.Hadoop配置格式 Hadoop配置文件格式如下所示 ...
- hadoop streaming 编程
概况 Hadoop Streaming 是一个工具, 代替编写Java的实现类,而利用可执行程序来完成map-reduce过程.一个最简单的程序 $HADOOP_HOME/bin/hadoop jar ...
- Nutch+Hadoop集群搭建
转载自:http://www.open-open.com/lib/view/open1328670771405.html 1.Apache Nutch Apache Nutch是一个用于网络搜索 ...
- Hadoop MapReduce开发最佳实践(上篇)
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
随机推荐
- Axiom3D:Ogre中Mesh文件格式分析(一)
在Axiom3D,或者说是Ogre的mesh的文件格式我们可能通过代码反推出相关格式,相关过程本来我是直接写的,后面发现相关流程写完后,我自己都看晕了,然后我就把一些过程用Execl整理出来,发现过程 ...
- 最近迷上了GUI
package windows; import java.awt.BorderLayout; import javax.swing.ButtonGroup; import javax.swing.JB ...
- Java数组列表反转
在Java中,如何反转数组列表中的元素? 以下示例使用Collections.reverse(ArrayList)方法反转数组列表中的元素. package com.yiibai; public cl ...
- VMware虚拟CentOS 6.5在NAT模式下配置静态IP地址及Xshell远程控制配置
VMware虚拟CentOS 6.5在NAT模式下配置静态IP地址及Xshell远程控制配置 标签: LinuxXshellCentOS 2016-10-15 04:58 127人阅读 评论(0) 收 ...
- 备份集中的数据库备份与现有的xx数据库不同”解决方法
搞定
- R语言ggplot2 简介
ggplot2是一个绘制可视化图形的R包,汲取了R语言基础绘图系统(graphics) 和l attice包的优点,摒弃了相关的缺点,创造出来的一套独立的绘图系统: ggplot2 有以下几个特点: ...
- 最好的Java和Android开发IDE---IntelliJ IDEA使用技巧
转载请注明网址:http//:www.cnblogs.com/JohnTsai 以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试 ...
- VS2008编译错误:error C2065: 'PMIB_TCPSTATS' : undeclared identifier c:\program files (x86)\microsoft sdks\windows\v7.0a\include\iphlpapi.h 411
安装了VS2008编译之前的程序,结果出现了编译错误,以为是VS2008的Sp1补丁没装好,重装补丁后还是不行,编译错误如下: 双击错误会定位在iphlpapi.h中, 一个可行的解决办法是:把iph ...
- Linux/Centos下/lib64/libc.so.6: version `GLIBC_2.14' not found问题
Centos的某个版本下编译了一个可执行程序,复制到另外一个Centos环境下去执行,结果出现了以下错误: /lib64/libc.so.6: version `GLIBC_2.14' not fou ...
- Bootstrap——导航居中
这是采用了栅格,设置缩进,使看起来居中,但是手机浏览会靠到最左边.另外center-block类好像也不管用. <div class="row"> <ul cla ...