博客
关于我
(十一)MapReduce案例之WordCount(2)
阅读量:628 次
发布时间:2019-03-13

本文共 4001 字,大约阅读时间需要 13 分钟。

在 Windows 环境下手动运行 Hadoop WordCount Program

本文将详细介绍在 Windows 环境下手动运行 Hadoop WordCount Program 的过程,包括配置项目、创建HDFS文件、编写代码以及解决常见问题的方法。


1.配置项目

1.1 复制Hadoop配置文件

将 Hadoop 集群的配置文件(如 core-site.xmlmapred-site.xmlyarn-site.xmllog4j.properties)复制到项目的 resource 目录下。请确保这些配置文件与您的集群配置保持一致。

1.2 下载并解压工具包

下载 hadoop-common-2.6.5-bin-master.zip,提取码:x1z8。将工具包中的 bin 目录中的所有文件复制到您的 Hadoop 2.6.5 安装目录下的 bin 目录内。

1.3 设置Hadoop路径

hadoop-common-2.6.5-bin-masterbin 文件夹中的 hadoop.dll複制到 C:\Windows\System32,然后重启电脑以确保 Java 能够正确加载 hadoop.dll。如果在运行程序时出现 UnsatisfiedLinkError 错误,说明 mest soir.monitorिज executed correctly.


2.在HDFS文件中创建文件

2.1 创建输入目录

通过 shell 命令在 HDFS 上创建输入目录:

hadoop fs -mkdir -p /input

然后赋予该目录权限:

hadoop fs -chmod 777 /input

2.2 将文件上传到HDFS

将要处理的文件 test.txt 上传到 HDFS 引擎:

hadoop fs -put test.txt /input

赋予文件权限:

hadoop fs -chmod 777 /input/test.txt

3.编写代码

以下是 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
{ private Text word = new Text(); private final IntWritable one = new IntWritable(1); public void map(Object key, Text value, Mapper
.Context context) throws IOException, InterruptedException { StringTokenizer tokenizer = new StringTokenizer(value.toString()); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer
{ private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable
values, Reducer
.Context context) throws IOException, InterruptedException { int sum = 0; for (Iterator
iterator = values.iterator(); iterator.hasNext(); sum += iterator.next().get()) {} result.set(sum); context.write(key, result); } }}

4.设置运行参数

在 IDEA 中,点击右上角的 Edit 图标,进入 Program Parameters,并修改以下内容:

  • Program arguments: 输入 HDFS 的输入路径和输出路径。
  • Working directon: 这里选择 Windows 作为工作环境。

5.运行MR

运行 WordCount Program 时,请注意:

  • 如果没有赋予权限会出现错误。解决方法:
  • 对非 CDH 用户,修改 conf/hdfs-core.xml 文件,将 dfs.permissions 的值设置为 false
  • 更改文件权限:
    hadoop fs -chmod 777 /user/root

运行成功后,你可以通过访问 Master节点的 Web 界面(master:50070)查看结果,进入 /user/root/output 查看输出文件的具体内容。


6.注意事项

  • 在 IDEA 中运行 WordCount 后,下一次运行时需要手动清理 output 文件夹,不然可能导致片段化输出。
  • 如果在 Windows 环境下运行,确保所有 Hadoop 目录的路径正确配置,并且 permissions 设置正确。

通过以上步骤,你已成功在地窗口环境下手动运行 Hadoop WordCount Program。

转载地址:http://kxraz.baihongyu.com/

你可能感兴趣的文章
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
NR,NF,FNR
查看>>
nrf开发笔记一开发软件
查看>>
NSDateFormatter的替代方法
查看>>
NSOperation基本操作
查看>>
NSSet集合 无序的 不能重复的
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
Nuget~管理自己的包包
查看>>
nullnullHuge Pages
查看>>
NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
查看>>
numpy 用法
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>