网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
public class WordCountMain {
public WordCountMain(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration, “word_count”);
job.setJarByClass(WordCountMain.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.out.println(job.waitForCompletion(true) ? “运行成功” : “运行失败”);
}
public static void main(String[] args) {
try {
WordCountMain wordCountMain = new WordCountMain(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
map类:
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) {
String line = value.toString();
String[] words = line.split(" ");
for (String word : words) {
// 将单词作为key,将次数1作为value。
try {
context.write(new Text(word), new LongWritable(1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
reducer类:
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable values, Context context) {
int count = 0;
for (LongWritable value : values) {
count += value.get();
}
try {
context.write(key, new LongWritable(count));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3,在IntelliJ IDEA中直接导出可运行的Java的jar包(具体方法见文章 https://zhangphil.blog.csdn.net/article/details/99434450),注意,在Hadoop2.8.3,Windows 10,64位环境下,编译出的jar包里面有两个文件夹:META-INF和license,如果直接运行将导致失败。需要用解压缩工具打开这个jar包,删掉META-INF和license这两个文件夹,如图:
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!