您好,登錄后才能下訂單哦!
本篇內容介紹了“Hadoop中MapReduce獲取命令行參數的方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
package cmd; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import mapreduce.MyMapper; import mapreduce.MyReducer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; /** * 計算單詞 * @author Xr * */ public class WordCountApp extends Configured implements Tool{ public static String INPUT_PATH = ""; public static String OUTPUT_PATH = ""; @Override public int run(String[] args) throws Exception { INPUT_PATH = args[0]; OUTPUT_PATH = args[1]; Configuration conf = new Configuration(); //判處是否存在輸入目錄 existsFile(conf); Job job = new Job(conf,WordCountApp.class.getName()); //打成jar包 job.setJarByClass(WordCountApp.class); //1.1 從哪里讀取數據 FileInputFormat.setInputPaths(job, INPUT_PATH); //把輸入文本中的每一行解析成一個個鍵值對 job.setInputFormatClass(TextInputFormat.class); //1.2 設置自定義map函數 job.setMapperClass(MyMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); //1.3 分區 job.setPartitionerClass(HashPartitioner.class); job.setNumReduceTasks(1); //1.4 TODO 排序分組 //1.5 TODO 規約 //2.1 是框架做的,不需要程序員手工干預。 //2.2 自定義reducer函數 job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); //2.3 寫入到HDFS中 FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH)); //格式化類 job.setOutputFormatClass(TextOutputFormat.class); //提交給JobTracker去執行 job.waitForCompletion(true); return 0; } public static void main(String[] args)throws Exception { ToolRunner.run(new WordCountApp(), args); } private static void existsFile(Configuration conf) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI(INPUT_PATH), conf); if(fs.exists(new Path(OUTPUT_PATH))){ fs.delete(new Path(OUTPUT_PATH), true); } } } 運行:hadoop jar WordCount.jar hdfs://hadoop:9000/hello hdfs://hadoop:9000/h2 Name : Xr Date : 2014-03-02 21:47
“Hadoop中MapReduce獲取命令行參數的方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。