MapReduce基础开发之四参数传递
生活随笔
收集整理的這篇文章主要介紹了
MapReduce基础开发之四参数传递
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Map和Reduce函數(shù)是在各節(jié)點(diǎn)進(jìn)行,如果要在MapReduce數(shù)據(jù)加工中使用共同參數(shù),要如何傳參呢?方法有二:
1、Configuration類的set和get的方法讀取xml/txt文件設(shè)置或自己配置,再通過Map和Reduce函數(shù)的Context來(lái)獲取;
2、基于org.apache.hadoop.io.DefaultStringifier類的Store函數(shù)和Load函數(shù),通過Writable接口序列化和反序列化實(shí)現(xiàn);
1、Configuration類的set和get的方法讀取xml/txt文件設(shè)置或自己配置,再通過Map和Reduce函數(shù)的Context來(lái)獲取;
2、基于org.apache.hadoop.io.DefaultStringifier類的Store函數(shù)和Load函數(shù),通過Writable接口序列化和反序列化實(shí)現(xiàn);
這里采用方法1,模擬讀取txt文件獲取參數(shù)并傳到Map函數(shù)。具體代碼如下:
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個(gè)字段為adsl賬號(hào),作為輸出keyString url=iValue[7];//源文件每行第8個(gè)字段為urlString cookike=iValue[10];//源文件每行第11個(gè)字段為cookie//獲取正則表達(dá)式匹配,conf所set得值String regEx = context.getConfiguration().get("regEx");//String regEx=".*imei.*|.*meid.*|.*imsi.*|.*biz.*";//定義正則表達(dá)式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參數(shù)及其值提取String[] paras=strUrl.toString().split("=");if(paras.length>1){Matcher matUrl = patMatch.matcher(paras[0]);//匹配參數(shù)中含正則表達(dá)式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參數(shù)及其值提取String[] paras=strCookie.toString().split("=");if(paras.length>1){Matcher matCookie = patMatch.matcher(paras[0]);//匹配參數(shù)中含正則表達(dá)式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 {//獲取正則表達(dá)式匹配,conf所set得值,這里只是將參數(shù)直接輸出,目的是驗(yàn)證reduce函數(shù)也獲取到參數(shù)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文件,提取正則表達(dá)式//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";//獲取當(dāng)前用戶工作目錄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);//截取最后一個(gè)|字符conf.set("regEx", regEx);//傳遞參數(shù)//獲取輸入和輸出目錄String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: AdslDPI <in> <out>");System.exit(2);}//設(shè)置驅(qū)動(dòng)函數(shù)和mapreduce函數(shù)Job job = new Job(conf, "parse url and cookies");job.setJarByClass(AdslDPI.class);job.setNumReduceTasks(1);//設(shè)置reduce輸入文件一個(gè),方便查看結(jié)果job.setMapperClass(DPIMapper.class);job.setCombinerClass(DPIReducer.class);job.setReducerClass(DPIReducer.class);//設(shè)置輸出<key,value>數(shù)據(jù)類型job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//設(shè)置輸入輸出目錄FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);} }總結(jié)
以上是生活随笔為你收集整理的MapReduce基础开发之四参数传递的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapReduce基础开发之三字段处理并
- 下一篇: MapReduce基础开发之五分布式下载