博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hadoop参数传递
阅读量:7030 次
发布时间:2019-06-28

本文共 4998 字,大约阅读时间需要 16 分钟。

传参关键代码:

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

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
{ 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
{ public void reduce(Text key,Iterable
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:
"); 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); }}

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

KeepUrlString
anjueke.com|soufun.com
FilterUrlString
.js|.jpg|.jpeg|.gif|.png|.css|error.html

 

转载地址:http://pbrxl.baihongyu.com/

你可能感兴趣的文章
mpvue打包没有app.json等配置文件的解决方法
查看>>
树莓派配置swoole环境
查看>>
JavaScript 工作原理之十二-网络层探秘及如何提高其性能和安全性
查看>>
搭建基于react项目的心得
查看>>
react-native踩坑记录
查看>>
HTTP API 设计入坑指南(一)
查看>>
OkHttp源码分析
查看>>
【挖坑系列】跨域问题相关
查看>>
使用cronolog切割nginx访问日志,定时清理旧日志
查看>>
PHP最常用函数TOP100(翻译)
查看>>
大数据科学新发展展望:不得不知的四大趋势
查看>>
python多线程、锁、event事件机制的简单使用
查看>>
ES6系列之解构赋值
查看>>
goLang 文件操作之二
查看>>
7大维度看国外企业为啥选择gRPC打造高性能微服务?
查看>>
HTTP协议类
查看>>
建造者模式
查看>>
【redux篇】middleware 之 redux-thunk
查看>>
数据结构---图的相关总结
查看>>
Linux平台上部署Mongoose服务器的方法介绍
查看>>