本文共 4050 字,大约阅读时间需要 13 分钟。
本文将详细介绍在 Windows 环境下手动运行 Hadoop WordCount Program 的过程,包括配置项目、创建HDFS文件、编写代码以及解决常见问题的方法。
将 Hadoop 集群的配置文件(如 core-site.xml、mapred-site.xml、yarn-site.xml 和 log4j.properties)复制到项目的 resource 目录下。请确保这些配置文件与您的集群配置保持一致。
下载 hadoop-common-2.6.5-bin-master.zip,提取码:x1z8。 将工具包中的 bin 目录中的所有文件复制到您的 Hadoop 2.6.5 安装目录下的 bin 目录内。
将 hadoop-common-2.6.5-bin-master 的 bin 文件夹中的 hadoop.dll複制到 C:\Windows\System32,然后重启电脑以确保 Java 能够正确加载 hadoop.dll。如果在运行程序时出现 UnsatisfiedLinkError 错误,说明 mest soir.monitorिज executed correctly.
通过 shell 命令在 HDFS 上创建输入目录:
hadoop fs -mkdir -p /input
然后赋予该目录权限:
hadoop fs -chmod 777 /input
将要处理的文件 test.txt 上传到 HDFS 引擎:
hadoop fs -put test.txt /input
赋予文件权限:
hadoop fs -chmod 777 /input/test.txt
以下是 wordcount 项目的主类代码,使用 Maven 运行:
package MapReduce;import java.io.IOException;import java.util.Iterator;import java.util.StringTokenizer;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.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;public class WordCount { public WordCount() {} public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length < 2) { System.err.println("Usage: wordcount [ ...] "); System.exit(2); } Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCount.TokenizerMapper.class); job.setCombinerClass(WordCount.IntSumReducer.class); job.setReducerClass(WordCount.IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); for (int i = 0; i < otherArgs.length - 1; ++i) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } public static class TokenizerMapper extends Mapper 在 IDEA 中,点击右上角的 Edit 图标,进入 Program Parameters,并修改以下内容:
Windows 作为工作环境。运行 WordCount Program 时,请注意:
conf/hdfs-core.xml 文件,将 dfs.permissions 的值设置为 false。hadoop fs -chmod 777 /user/root
运行成功后,你可以通过访问 Master节点的 Web 界面(master:50070)查看结果,进入 /user/root/output 查看输出文件的具体内容。
output 文件夹,不然可能导致片段化输出。通过以上步骤,你已成功在地窗口环境下手动运行 Hadoop WordCount Program。
转载地址:http://kxraz.baihongyu.com/