Hadoop是一个开源的分布式计算框架,它允许使用简单的编程模型在大量计算机集群上处理大规模数据集,Hadoop的核心组件包括HDFS(Hadoop Distributed File System)和MapReduce,以下是一个简单的Hadoop MapReduce程序示例,用于统计文本文件中单词的出现次数:
1、我们需要创建一个Java项目,并添加Hadoop相关的依赖库,在项目的pom.xml文件中添加以下依赖:
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoopcommon</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoopmapreduceclientcore</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoophdfs</artifactId> <version>3.2.1</version> </dependency> </dependencies>
2、编写一个Mapper类,用于读取输入文件并将每个单词作为键,值初始化为1。
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split("\s+"); for (String w : words) { word.set(w); context.write(word, one); } } }
3、编写一个Reducer类,用于将Mapper输出的键值对进行汇总,得到每个单词的总出现次数。
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable value : values) { sum += value.get(); } context.write(key, new IntWritable(sum)); } }
4、编写一个驱动程序,用于配置和运行MapReduce作业。
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: WordCount <input path> <output path>"); System.exit(1); } Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
5、编译并打包项目,将生成的jar文件上传到Hadoop集群,通过命令行运行以下命令来执行MapReduce作业:
hadoop jar wordcount.jar WordCount input_path output_path
input_path
是包含要统计的文本文件的HDFS路径,output_path
是结果输出的HDFS路径。
以上内容就是解答有关idea 源码 hadoop的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1113404.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复