1. 程式人生 > >自定義Counter使用 與

自定義Counter使用 與

自定義計數器的使用(記錄敏感單詞)
在這裡插入圖片描述

複製程式碼
1 package counter;
2
3 import java.net.URI;
4 import org.apache.hadoop.conf.Configuration;
5 import org.apache.hadoop.fs.FileSystem;
6 import org.apache.hadoop.fs.Path;
7 import org.apache.hadoop.io.LongWritable;
8 import org.apache.hadoop.io.Text;
9 import org.apache.hadoop.mapreduce.Counter;
10 import org.apache.hadoop.mapreduce.Job;
11 import org.apache.hadoop.mapreduce.Mapper;
12 import org.apache.hadoop.mapreduce.Reducer;
13 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14 import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
16 import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
17
18 public class WordCountApp {
19 static final String INPUT_PATH = “hdfs://chaoren:9000/hello”;
20 static final String OUT_PATH = “hdfs://chaoren:9000/out”;
21
22 public static void main(String[] args) throws Exception {
23 Configuration conf = new Configuration();
24 FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
25 Path outPath = new Path(OUT_PATH);
26 if (fileSystem.exists(outPath)) {
27 fileSystem.delete(outPath, true);
28 }
29
30 Job job = new Job(conf, WordCountApp.class.getSimpleName());
31
32 // 1.1指定讀取的檔案位於哪裡
33 FileInputFormat.setInputPaths(job, INPUT_PATH);
34 // 指定如何對輸入的檔案進行格式化,把輸入檔案每一行解析成鍵值對
35

//job.setInputFormatClass(TextInputFormat.class);
36
37 // 1.2指定自定義的map類
38 job.setMapperClass(MyMapper.class);
39 // map輸出的<k,v>型別。如果<k3,v3>的型別與<k2,v2>型別一致,則可以省略
40 //job.setOutputKeyClass(Text.class);
41 //job.setOutputValueClass(LongWritable.class);
42
43 // 1.3分割槽
44 //job.setPartitionerClass
(org.apache.hadoop.mapreduce.lib.partition.HashPartitioner.class);
45 // 有一個reduce任務執行
46 //job.setNumReduceTasks(1);
47
48 // 1.4排序、分組
49
50 // 1.5歸約
51
52 // 2.2指定自定義reduce類
53 job.setReducerClass(MyReducer.class);
54 // 指定reduce的輸出型別
55 job.setOutputKeyClass(Text.class);
56 job.setOutputValueClass(LongWritable.class);
57
58 // 2.3指定寫出到哪裡
59 FileOutputFormat.setOutputPath(job, outPath);
60 // 指定輸出檔案的格式化類
61
//job.setOutputFormatClass
(TextOutputFormat.class);
62
63 // 把job提交給jobtracker執行
64 job.waitForCompletion(true);
65 }
66
67 /**
68 *
69 * KEYIN 即K1 表示行的偏移量
70 * VALUEIN 即V1 表示行文字內容
71 * KEYOUT 即K2 表示行中出現的單詞
72 * VALUEOUT 即V2 表示行中出現的單詞的次數,固定值1
73 *
74 /
75 static class MyMapper extends
76 Mapper<LongWritable, Text, Text, LongWritable> {
77 protected void map(LongWritable k1, Text v1, Context context)
78 throws java.io.IOException, InterruptedException {
79 /
*
80 * 自定義計數器的使用
81 /
82 Counter counter = context.getCounter(“Sensitive Words”, “hello”);//自定義計數器名稱Sensitive Words
83 String line = v1.toString();
84 if(line.contains(“hello”)){
85 counter.increment(1L);//記錄敏感詞彙hello的出現次數
86 }
87 String[] splited = line.split("\t");
88 for (String word : splited) {
89 context.write(new Text(word), new LongWritable(1));
90 }
91 };
92 }
93
94 /
*
95 * KEYIN 即K2 表示行中出現的單詞
96 * VALUEIN 即V2 表示出現的單詞的次數
97 * KEYOUT 即K3 表示行中出現的不同單詞
98 * VALUEOUT 即V3 表示行中出現的不同單詞的總次數
99 */
100 static class MyReducer extends
101 Reducer<Text, LongWritable, Text, LongWritable> {
102 protected void reduce(Text k2, java.lang.Iterable v2s,
103 Context ctx) throws java.io.IOException,
104 InterruptedException {
105 long times = 0L;
106 for (LongWritable count : v2s) {
107 times += count.get();
108 }
109 ctx.write(k2, new LongWritable(times));
110 };
111 }
112 }

複製程式碼

在eclipse中執行後,可以在控制檯檢視到結果:
在這裡插入圖片描述

Combiner程式設計(1.5可選步驟,視情況而定!)
•每一個map可能會產生大量的輸出,combiner的作用就是在map端對輸出先做一次合併,以減少傳輸到reducer的資料量。
•combiner最基本是實現本地key的歸併,combiner具有類似本地的reduce功能。 如果不用combiner,那麼,所有的結果

都是reduce完成,效率會相對低下。使用combiner,先完成的map會在本地聚合,提升速度。
•注意:Combiner的輸出是Reducer的輸入,Combiner絕不能改變最終的計算結果。所以從我的想法來看,Combiner只應該

用於那種Reduce的輸入key/value與輸出key/value型別完全一致,且不影響最終結果的場景。比如累加,最大值等。

複製程式碼
1 package combine;
2
3 import java.net.URI;
4
5 import org.apache.hadoop.conf.Configuration;
6 import org.apache.hadoop.fs.FileSystem;
7 import org.apache.hadoop.fs.Path;
8 import org.apache.hadoop.io.LongWritable;
9 import org.apache.hadoop.io.Text;
10 import org.apache.hadoop.mapreduce.Job;
11 import org.apache.hadoop.mapreduce.Mapper;
12 import org.apache.hadoop.mapreduce.Reducer;
13 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
15
16 /**
17 * 問:為什麼使用Combiner?
18 * 答:Combiner發生在Map端,對資料進行規約處理,資料量變小了,傳送到reduce端的資料量變小了,傳輸時間變短,作業的整體時間變短。
19 *
20 * 問:為什麼Combiner不作為MR執行的標配,而是可選步驟呢?
21 * 答:因為不是所有的演算法都適合使用Combiner處理,例如求平均數。
22 *
23 * 問:Combiner本身已經執行了reduce操作,為什麼在Reducer階段還要執行reduce操作呢?
24 * 答:combiner操作發生在map端的,處理一個任務所接收的檔案中的資料,不能跨map任務執行;只有reduce可以接收多個map任務處理的資料。
25 *
26 /
27 public class WordCountApp {
28 static final String INPUT_PATH = “hdfs://chaoren:9000/hello”;
29 static final String OUT_PATH = “hdfs://chaoren:9000/out”;
30
31 public static void main(String[] args) throws Exception {
32 Configuration conf = new Configuration();
33 final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
34 final Path outPath = new Path(OUT_PATH);
35 if(fileSystem.exists(outPath)){
36 fileSystem.delete(outPath, true);
37 }
38
39 final Job job = new Job(conf , WordCountApp.class.getSimpleName());
40 //1.1指定讀取的檔案位於哪裡
41 FileInputFormat.setInputPaths(job, INPUT_PATH);
42 //指定如何對輸入檔案進行格式化,把輸入檔案每一行解析成鍵值對
43 //job.setInputFormatClass(TextInputFormat.class);
44
45 //1.2 指定自定義的map類
46 job.setMapperClass(MyMapper.class);
47 //map輸出的<k,v>型別。如果<k3,v3>的型別與<k2,v2>型別一致,則可以省略
48 //job.setMapOutputKeyClass(Text.class);
49 //job.setMapOutputValueClass(LongWritable.class);
50
51 //1.3 分割槽
52 //job.setPartitionerClass(HashPartitioner.class);
53 //有一個reduce任務執行
54 //job.setNumReduceTasks(1);
55
56 //1.4 TODO 排序、分組
57
58 //1.5 規約
59 job.setCombinerClass(MyCombiner.class);
60
61 //2.2 指定自定義reduce類
62 job.setReducerClass(MyReducer.class);
63 //指定reduce的輸出型別
64 job.setOutputKeyClass(Text.class);
65 job.setOutputValueClass(LongWritable.class);
66
67 //2.3 指定寫出到哪裡
68 FileOutputFormat.setOutputPath(job, outPath);
69 //指定輸出檔案的格式化類
70 //job.setOutputFormatClass(TextOutputFormat.class);
71
72 //把job提交給JobTracker執行
73 job.waitForCompletion(true);
74 }
75
76 /
*
77 * KEYIN 即k1 表示行的偏移量
78 * VALUEIN 即v1 表示行文字內容
79 * KEYOUT 即k2 表示行中出現的單詞
80 * VALUEOUT 即v2 表示行中出現的單詞的次數,固定值1
81 /
82 static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
83 protected void map(LongWritable k1, Text v1, Context context) throws java.io.IOException ,InterruptedException {
84 final String[] splited = v1.toString().split("\t");
85 for (String word : splited) {
86 context.write(new Text(word), new LongWritable(1));
87 System.out.println(“Mapper輸出<”+word+","+1+">");
88 }
89 };
90 }
91
92 /
*
93 * KEYIN 即k2 表示行中出現的單詞
94 * VALUEIN 即v2 表示行中出現的單詞的次數
95 * KEYOUT 即k3 表示文字中出現的不同單詞
96 * VALUEOUT 即v3 表示文字中出現的不同單詞的總次數
97 *
98 */
99 static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
100 protected void reduce(Text k2, java.lang.Iterable v2s, Context ctx) throws java.io.IOException ,InterruptedException {
101 //顯示次數表示redcue函式被呼叫了多少次,表示k2有多少個分組
102 System.out.println(“MyReducer輸入分組<”+k2.toString()+",…>");
103 long times = 0L;
104 for (LongWritable count : v2s) {
105 times += count.get();
106 //顯示次數表示輸入的k2,v2的鍵值對數量
107 System.out.println(“MyReducer輸入鍵值對<”+k2.toString()+","+count.get()+">");
108 }
109 ctx.write(k2, new LongWritable(times));
110 };
111 }
112
113
114 static class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable>{
115 protected void reduce(Text k2, java.lang.Iterable v2s, Context ctx) throws java.io.IOException ,InterruptedException {
116 //顯示次數表示redcue函式被呼叫了多少次,表示k2有多少個分組
117 System.out.println(“Combiner輸入分組<”+k2.toString()+",…>");
118 long times = 0L;
119 for (LongWritable count : v2s) {
120 times += count.get();
121 //顯示次數表示輸入的k2,v2的鍵值對數量
122 System.out.println(“Combiner輸入鍵值對<”+k2.toString()+","+count.get()+">");
123 }
124
125 ctx.write(k2, new LongWritable(times));
126 //顯示次數表示輸出的k2,v2的鍵值對數量
127 System.out.println(“Combiner輸出鍵值對<”+k2.toString()+","+times+">");
128 };
129 }
130 }

複製程式碼
在這裡插入圖片描述