hadoop_wordcount_1027

MyMap

 1 package s26;
 2 
 3 import org.apache.hadoop.io.IntWritable;
 4 import org.apache.hadoop.io.LongWritable;
 5 import org.apache.hadoop.io.Text;
 6 import org.apache.hadoop.mapreduce.Mapper;
 7 
 8 import java.io.IOException;
 9 
10 
11 public class MyMap extends Mapper<LongWritable,Text,Text,IntWritable> {
12 
13     @Override
14     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
15         //super.map(key, value, context);
16         Text result = new Text();
17         IntWritable one = new IntWritable(1);
18 
19         String line = value.toString();
20         String[] words = line.split("\t");
21 
22         for (String w:words) {
23             result.set(w);
24             context.write(result,one);
25         }
26 
27     }
28 }

MyRed

 1 package s26;
 2 
 3 import org.apache.hadoop.io.IntWritable;
 4 import org.apache.hadoop.io.Text;
 5 import org.apache.hadoop.mapreduce.Reducer;
 6 
 7 import java.io.IOException;
 8 
 9 public class MyRed extends Reducer<Text,IntWritable,Text,IntWritable> {
10 
11     @Override
12     protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
13         //super.reduce(key, values, context);
14         int sum = 0;
15         for(IntWritable i:values){
16             sum += i.get();
17         }
18         context.write(key,new IntWritable(sum));
19 
20     }
21 }

MyJob

 1 package s26;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.fs.FileSystem;
 5 import org.apache.hadoop.fs.Path;
 6 import org.apache.hadoop.io.IntWritable;
 7 import org.apache.hadoop.io.Text;
 8 import org.apache.hadoop.mapreduce.Job;
 9 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
10 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
11 
12 import java.io.IOException;
13 
14 public class MyJob {
15 
16     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
17 
18         //1.conf
19         Configuration conf = new Configuration();
20         conf.set("fs.defaultFS","hdfs://master:9000");
21 
22         //2.job
23         Job job = Job.getInstance(conf);
24         // jar-package
25         job.setJarByClass(MyJob.class);
26         //
27         job.setMapperClass(MyMap.class);
28         //
29         job.setReducerClass(MyRed.class);
30         job.setOutputKeyClass(Text.class);
31         job.setOutputValueClass(Integer.class);
32 
33         job.setMapOutputKeyClass(Text.class);
34         job.setMapOutputValueClass(IntWritable.class);
35         job.setOutputKeyClass(Text.class);
36         job.setOutputValueClass(IntWritable.class);
37         //3.io
38         Path pin = new Path("/sjw");
39         Path pout = new Path("/out");
40         //
41         FileSystem fs = FileSystem.get(conf);
42         if(fs.exists(pout)){
43             fs.delete(pout,true);
44         }
45         FileInputFormat.setInputPaths(job,pin);
46         FileOutputFormat.setOutputPath(job,pout);
47 
48         //4.run
49         job.waitForCompletion(true);
50 
51 
52     }
53 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小石小石摩西摩西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值