MapReduce基础开发之四参数传递
生活随笔
收集整理的這篇文章主要介紹了
MapReduce基础开发之四参数传递
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Map和Reduce函數是在各節點進行,如果要在MapReduce數據加工中使用共同參數,要如何傳參呢?方法有二:
1、Configuration類的set和get的方法讀取xml/txt文件設置或自己配置,再通過Map和Reduce函數的Context來獲取;
2、基于org.apache.hadoop.io.DefaultStringifier類的Store函數和Load函數,通過Writable接口序列化和反序列化實現;
1、Configuration類的set和get的方法讀取xml/txt文件設置或自己配置,再通過Map和Reduce函數的Context來獲取;
2、基于org.apache.hadoop.io.DefaultStringifier類的Store函數和Load函數,通過Writable接口序列化和反序列化實現;
這里采用方法1,模擬讀取txt文件獲取參數并傳到Map函數。具體代碼如下:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; 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.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;public class AdslDPI {public static class DPIMapper extends Mapper<Object, Text, Text, Text>{private Text oKey=new Text();public void map(Object key, Text value, Context context)throws IOException, InterruptedException {String[] iValue=value.toString().split("\\|");//獲取行,并按照|分隔符提取if(iValue.length>10){String account=iValue[1];//源文件每行第2個字段為adsl賬號,作為輸出keyString url=iValue[7];//源文件每行第8個字段為urlString cookike=iValue[10];//源文件每行第11個字段為cookie//獲取正則表達式匹配,conf所set得值String regEx = context.getConfiguration().get("regEx");//String regEx=".*imei.*|.*meid.*|.*imsi.*|.*biz.*";//定義正則表達式Pattern patMatch = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE); Pattern patSplit= Pattern.compile("[?&]+"); //以多條件分割字符串 String[] strsUrl = patSplit.split(url); //解析URLfor (String strUrl:strsUrl){if(strUrl.contains("=")){//para=value參數及其值提取String[] paras=strUrl.toString().split("=");if(paras.length>1){Matcher matUrl = patMatch.matcher(paras[0]);//匹配參數中含正則表達式if(matUrl.find()){oKey.set(account+"|"+paras[0]+"|"+paras[1]);context.write(oKey,new Text(""));}}}}String[] strsCookie = patSplit.split(cookike); //解析cookiefor (String strCookie:strsCookie){if(strCookie.contains("=")){//para=value參數及其值提取String[] paras=strCookie.toString().split("=");if(paras.length>1){Matcher matCookie = patMatch.matcher(paras[0]);//匹配參數中含正則表達式if(matCookie.find()){oKey.set(account+"|"+paras[0]+"|"+paras[1]);context.write(oKey,new Text(""));}}}} }}} public static class DPIReducer extends Reducer<Text,Text,Text,Text> {private Text oKey=new Text();public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {//獲取正則表達式匹配,conf所set得值,這里只是將參數直接輸出,目的是驗證reduce函數也獲取到參數String regEx = context.getConfiguration().get("regEx");String outStr=key.toString()+"|"+regEx;oKey.set(outStr);context.write(oKey, new Text(""));}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();//讀取txt文件,提取正則表達式//String path = AdslDPI.class.getProtectionDomain().getCodeSource().getLocation().getFile(); //獲取jar包所在目錄 //path = java.net.URLDecoder.decode(path, "UTF-8");//path=path+System.getProperty("file.separator")+"para.txt";String path=System.getProperty("user.dir")+System.getProperty("file.separator")+"para.txt";//獲取當前用戶工作目錄String regEx=new String("");try{ FileReader fileRd=new FileReader(path);BufferedReader bufRd=new BufferedReader(fileRd);String line=new String("");while((line=bufRd.readLine())!=null){regEx=regEx+line+"|";} }catch(Exception e){ e.printStackTrace(); }regEx=regEx.substring(0, regEx.length()-1);//截取最后一個|字符conf.set("regEx", regEx);//傳遞參數//獲取輸入和輸出目錄String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: AdslDPI <in> <out>");System.exit(2);}//設置驅動函數和mapreduce函數Job job = new Job(conf, "parse url and cookies");job.setJarByClass(AdslDPI.class);job.setNumReduceTasks(1);//設置reduce輸入文件一個,方便查看結果job.setMapperClass(DPIMapper.class);job.setCombinerClass(DPIReducer.class);job.setReducerClass(DPIReducer.class);//設置輸出<key,value>數據類型job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//設置輸入輸出目錄FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);} }總結
以上是生活随笔為你收集整理的MapReduce基础开发之四参数传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapReduce基础开发之三字段处理并
- 下一篇: MapReduce基础开发之五分布式下载