標籤:
public class WordCountApp { // 可以指定目錄,目錄下如果有二級目錄的話,是不會執行的,只會執行一級目錄. private static final String INPUT_PATH = "hdfs://hadoop1:9000/abd";// 輸入路徑 private static final String OUT_PATH = "hdfs://hadoop1:9000/out";// 輸出路徑,reduce作業輸出的結果是一個目錄 // _SUCCESS:在linux中,帶底線的這些檔案一般都是被忽略不去處理的.表示作業執行成功. // _logs:產生的記錄檔. // part-r-00000:產生的是我們的輸出的檔案.開始以part開始.r:reduce輸出的結果,map輸出的結果是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:作業名稱 job.setJarByClass(WordCountApp.class); FileInputFormat.setInputPaths(job, INPUT_PATH);// 指定資料的輸入 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);// 設定Reduce輸出key的類型 job.setOutputValueClass(LongWritable.class);// 設定Reduce輸出的value類型 FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));// Reduce輸出完之後,就會產生一個最終的輸出,指定最終輸出的位置 job.waitForCompletion(true);// 提交給jobTracker並等待結束 } catch (Exception e) { e.printStackTrace(); } } /** * 輸入的key標示位移量:這一行開始的位元組. 輸入的value:當前的行文本的內容. MapReduce執行過程: * 在這裡邊,我們的資料輸入來自於原始檔案,資料輸出寫出到hdfs, 中間的一堆都是map輸出產生的臨時結果.存放在map啟動並執行linux磁碟上的, * 當經過shuffle時,reduce就會通過http把map端的對應資料給取過來. * mapred-default.xml中mapredcue.jobtracker * .root.dir,mapred.tmp.dir儲存map產生的結果. 作業運行時產生這個目錄,作業運行完之後它會刪除目錄. */ public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> { // 源檔案有兩行記錄,解析源檔案會產生兩個索引值對.分別是<0,hello you>,<10,hello me>,所以map函數會被調用兩次. // 在電腦儲存的時候,是一維的結構. @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 為什麼要把hadoop類型轉換為java類型? String line = value.toString(); String[] splited = line.split("\t"); // 使用hashMap寫出去的優勢:減少索引值對出現的個數. Map<String, Integer> hashMap = new HashMap<String, Integer>(); for (String word : splited) { // 在for迴圈體內,臨時變數word出現的此時是常量1 context.write(new Text(word), new LongWritable(1));// 把每個單詞出現的次數1寫出去. } } } // map函數執行結束後,map輸出的<k,v>一共有4個.<hello,1>,<you,1>,<hello,1>,<me,1> // map把資料處理完之後,就會進入reduce. // 在進入shuffle之前,資料需要先進行分區.預設只有一個區. // 對每個不同分區中的資料進行排序,分組. // 排序後的結果:<hello,1>,<hello,1>,<me,1>,<you,1> // 分組後的結果(相同key的value放在一個集合中):<hello,{1,1}>,<me,{1}>,<you,{1}> // 規約(可選) // map中的資料分發到reduce的過程稱作shuffle public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> { // 每一組調用一次reduce函數,一共調用了三次 @Override protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { // count標示單詞key在整個檔案出現的次數 // 分組的數量與reduce函數調用次數是相等的. // reduce函數調用次數與產生的<k,v>的數量拋開業務,沒有任何關係! long count = 0L; for (LongWritable times : values) { count += times.get(); } context.write(key, new LongWritable(count)); } }}
單詞計數WordCountApp.class