单词计数WordCountApp.class
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                单词计数WordCountApp.class
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            public class WordCountApp {// 可以指定目錄,目錄下如果有二級目錄的話,是不會執(zhí)行的,只會執(zhí)行一級目錄.private static final String INPUT_PATH = "hdfs://hadoop1:9000/abd";// 輸入路徑private static final String OUT_PATH = "hdfs://hadoop1:9000/out";// 輸出路徑,reduce作業(yè)輸出的結(jié)果是一個目錄// _SUCCESS:在linux中,帶下劃線的這些文件一般都是被忽略不去處理的.表示作業(yè)執(zhí)行成功.// _logs:產(chǎn)生的日志文件.// part-r-00000:產(chǎn)生的是我們的輸出的文件.開始以part開始.r:reduce輸出的結(jié)果,map輸出的結(jié)果是m,00000是序號public static void main(String[] args) {Configuration conf = new Configuration();// 配置對象try {FileSystem fileSystem = FileSystem.get(new URI(OUT_PATH), conf);fileSystem.delete(new Path(OUT_PATH), true);Job job = new Job(conf, WordCountApp.class.getSimpleName());// jobName:作業(yè)名稱job.setJarByClass(WordCountApp.class);FileInputFormat.setInputPaths(job, INPUT_PATH);// 指定數(shù)據(jù)的輸入job.setMapperClass(MyMapper.class);// 指定自定義map類job.setMapOutputKeyClass(Text.class);// 指定map輸出key的類型job.setMapOutputValueClass(LongWritable.class);// 指定map輸出value的類型job.setReducerClass(MyReducer.class);// 指定自定義Reduce類job.setOutputKeyClass(Text.class);// 設(shè)置Reduce輸出key的類型job.setOutputValueClass(LongWritable.class);// 設(shè)置Reduce輸出的value類型FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));// Reduce輸出完之后,就會產(chǎn)生一個最終的輸出,指定最終輸出的位置job.waitForCompletion(true);// 提交給jobTracker并等待結(jié)束} catch (Exception e) {e.printStackTrace();}}/*** 輸入的key標示偏移量:這一行開始的字節(jié). 輸入的value:當前的行文本的內(nèi)容. MapReduce執(zhí)行過程:* 在這里邊,我們的數(shù)據(jù)輸入來自于原始文件,數(shù)據(jù)輸出寫出到hdfs, 中間的一堆都是map輸出產(chǎn)生的臨時結(jié)果.存放在map運行的linux磁盤上的,* 當經(jīng)過shuffle時,reduce就會通過http把map端的對應數(shù)據(jù)給取過來.* mapred-default.xml中mapredcue.jobtracker* .root.dir,mapred.tmp.dir存儲map產(chǎn)生的結(jié)果. 作業(yè)運行時產(chǎn)生這個目錄,作業(yè)運行完之后它會刪除目錄.*/public static class MyMapper extendsMapper<LongWritable, Text, Text, LongWritable> {// 源文件有兩行記錄,解析源文件會產(chǎn)生兩個鍵值對.分別是<0,hello you>,<10,hello me>,所以map函數(shù)會被調(diào)用兩次.// 在計算機存儲的時候,是一維的結(jié)構(gòu).
        @Overrideprotected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {// 為什么要把hadoop類型轉(zhuǎn)換為java類型?String line = value.toString();String[] splited = line.split("\t");// 使用hashMap寫出去的優(yōu)勢:減少鍵值對出現(xiàn)的個數(shù).Map<String, Integer> hashMap = new HashMap<String, Integer>();for (String word : splited) {// 在for循環(huán)體內(nèi),臨時變量word出現(xiàn)的此時是常量1context.write(new Text(word), new LongWritable(1));// 把每個單詞出現(xiàn)的次數(shù)1寫出去.
            }}}// map函數(shù)執(zhí)行結(jié)束后,map輸出的<k,v>一共有4個.<hello,1>,<you,1>,<hello,1>,<me,1>// map把數(shù)據(jù)處理完之后,就會進入reduce.// 在進入shuffle之前,數(shù)據(jù)需要先進行分區(qū).默認只有一個區(qū).// 對每個不同分區(qū)中的數(shù)據(jù)進行排序,分組.// 排序后的結(jié)果:<hello,1>,<hello,1>,<me,1>,<you,1>// 分組后的結(jié)果(相同key的value放在一個集合中):<hello,{1,1}>,<me,{1}>,<you,{1}>// 規(guī)約(可選)// map中的數(shù)據(jù)分發(fā)到reduce的過程稱作shufflepublic static class MyReducer extendsReducer<Text, LongWritable, Text, LongWritable> {// 每一組調(diào)用一次reduce函數(shù),一共調(diào)用了三次
        @Overrideprotected void reduce(Text key, Iterable<LongWritable> values,Context context) throws IOException, InterruptedException {// count標示單詞key在整個文件出現(xiàn)的次數(shù)// 分組的數(shù)量與reduce函數(shù)調(diào)用次數(shù)是相等的.// reduce函數(shù)調(diào)用次數(shù)與產(chǎn)生的<k,v>的數(shù)量拋開業(yè)務(wù),沒有任何關(guān)系!long count = 0L;for (LongWritable times : values) {count += times.get();}context.write(key, new LongWritable(count));}}
}  
                        
                        
                        ?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaolong1032/p/4443148.html
總結(jié)
以上是生活随笔為你收集整理的单词计数WordCountApp.class的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 蓝牙Bluetooth技术手册规范下载【
- 下一篇: android键盘弹出,聊天背景不变形
