日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MapReduce基础开发之四参数传递

發(fā)布時間:2025/4/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MapReduce基础开发之四参数传递 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Map和Reduce函數(shù)是在各節(jié)點進行,如果要在MapReduce數(shù)據(jù)加工中使用共同參數(shù),要如何傳參呢?方法有二:

1、Configuration類的set和get的方法讀取xml/txt文件設(shè)置或自己配置,再通過Map和Reduce函數(shù)的Context來獲取;
2、基于org.apache.hadoop.io.DefaultStringifier類的Store函數(shù)和Load函數(shù),通過Writable接口序列化和反序列化實現(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個字段為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參數(shù)及其值提取String[] paras=strUrl.toString().split("=");if(paras.length>1){Matcher matUrl = patMatch.matcher(paras[0]);//匹配參數(shù)中含正則表達式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ù)中含正則表達式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得值,這里只是將參數(shù)直接輸出,目的是驗證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文件,提取正則表達式//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);//截取最后一個|字符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ū)動函數(shù)和mapreduce函數(shù)Job job = new Job(conf, "parse url and cookies");job.setJarByClass(AdslDPI.class);job.setNumReduceTasks(1);//設(shè)置reduce輸入文件一個,方便查看結(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基础开发之四参数传递的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。