一、所需工具
JDK8
maven3.6.3
Hadoop3.2.2
IntelliJ IDEA 2022.3.3
下载链接:https://pan.baidu.com/s/1x5-hLZXUP6oawGy4h693eQ?pwd=mona
提取码:mona
二、安装JDK
(一)温馨提示,不安装在C盘,且目录万万别有空格,否则背面会报错,无法在Windows中调用hadoop命令。
(二)下载JDK8
我的版本为jdk-8u152-windows-x64,可根据自身需要到官网下载合适的版本
(三)安装JDK
1.在D盘创建Java目录,并在Java目录中分别创建子目录jdk和jre

2.安装
点击jdk-8u152-windows-x64.exe,右键以管理员身份运行,点击“下一步”后,更改安装目录到D:\Java\jdk后下一步。

等到jre安装提示出来后,更改安装目录到D:\Java\jre后下一步。

安装完成后点击关闭。

(四)设置环境变量
1.点击“我的电脑”,右键,点击“属性”,选择“高级系统设置”,选择“环境变量”。



2.在系统变量的栏位中选择“新建”

3.添加JAVA_HOME,值为JDK的安装目录

4.将JAVA_HOME添加到Path中
选中系统变量栏位中的“Path”,点击“编辑”,点击新建后输入“%JAVA_HOME%\bin”,并将此条值上移。上移是为了保证系统会优先匹配我们的安装的JDK。


5.验证环境变量是否乐成设置。Win+R,输入cmd,进入dos界面。依次输入java -version,java.exe和javac.exe,能乐成表现相关内容,即证明JDK安装乐成。



二、安装MAVEN
(一)下载Maven,我的版本为3.6.3
(二)安装。Maven属于绿色版软件,解压即安装。将其解压到D:\Program Files\apache-maven-3.6.3

(三)设置环境变量。与设置JDK雷同,在环境变量中添加MAVEN_HOME,并将bin目录添加到Path中


(四)验证是否安装乐成。Win+R,输入cmd,进入dos界面。输mvn,能乐成表现相关内容,即证明Maven安装乐成。

(五)本地堆栈设置
1.在D盘创建本地堆栈地点D:\maven\repository

2.将默认的堆栈地点改成D:\maven\repository。到D:\Program Files\apache-maven-3.6.3\conf中,找到settings.xml,用编辑器打开编辑,大概54行位置,添加 <localRepository>D:\maven\repository</localRepository>。如果不改,默认位置会在C盘的C:\Users\T480s\.m2\repository下,随着项目增多,C盘会爆。

(六)镜像堆栈设置
到D:\Program Files\apache-maven-3.6.3\conf中,找到settings.xml,用编辑器打开,到160行位置编辑。设置镜像主要是为了提高国内用户下载依赖的速度和稳定性,同时方便管理和维护。
添加以下内容:
<mirror>
<!-- 次镜像的唯一标识符,用来区分不同的mirror元素 -->
<id>nexus-aliyun</id>
<!--对哪种堆栈进行镜像,简单说就是替换哪个堆栈 -->
<mirrorOf>central</mirrorOf>
<!-- 镜像名称-->
<name>Nexus aliyun</name>
<!-- 镜像URL -->
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

三、本地安装Hadoop
(一)下载Hadoop,我的版本为3.2.2
(二)解压至D:\hadoop-3.2.2

(三)在bin目录中添加hadoop.dll和winutils.exe。温馨提示,这两个文件一定要和Hadoop的版本匹配,如果不是3.2.2的版本,背面用的时候会报错。


(四)在C:\Windows\System32中也添加hadoop.dll

(五)设置环境变量。与设置JDK雷同,在环境变量中添加HADOOP_HOME,并将bin目录添加到Path中


(六)设置hadoop-env.cmd
1.到D:\hadoop-3.2.2\etc\hadoop下找到hadoop-env.cmd

2.修改JAVA_HOME=D:\Java\jdk

(七)验证是否安装乐成。Win+R,输入cmd,进入dos界面。输hadoop version,能乐成表现相关内容,即证明Hadoop安装乐成。

四、安装IDEA
(一)下载安装包,我的是2022.3.3
(二)安装IDEA到D:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3,可下载激活工具激活。不要安装在C盘就行
五、wordcount体验
(一)打开IDEA
(二)新建maven项目
1.点击File——New——project

2.创建项目名为wordcount.mr,项目存放到D:\workspace(可根据自身情况确定目录),语言选择Java,构建系统选择Maven

3.选择我们自己的JDK版本





4.选择本地Maven


(三)编辑项目
1.编辑pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordcount</groupId>
<artifactId>wordcount-mr</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Hadoop dependencies -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.2.2</version>
</dependency>
<!-- 如果您的项目中确实需要hadoop-mapreduce-client-core,请保留并更新版本 -->
<!-- 但通常hadoop-client已经包含了所需的MapReduce依赖 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>3.2.2</version>
</dependency>
<!-- 其他依赖,如数据库连接器等 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version> <!-- 考虑使用更新的版本 -->
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass></mainClass> <!-- 替换为您的主类名 -->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.等候依赖下载

3.观察依赖是否下载乐成


4.创建mapper





添加如下代码:
package com.wordcount;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @description:
*/
public class WordCountMapper extends Mapper<LongWritable, Text,Text,LongWritable> {
//Mapper输出kv键值对 <单词,1>
private Text keyOut = new Text();
private final static LongWritable valueOut = new LongWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//将读取的一行内容根据分隔符进行切割
String[] words = value.toString().split("\\s+");
//遍历单词数组
for (String word : words) {
keyOut.set(word);
//输出单词,并标记1
context.write(new Text(word),valueOut);
}
}
}
5.创建reducer



添加以下代码
package com.wordcount;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @description:
*/
public class WordCountReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
private LongWritable result = new LongWritable();
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
//统计变量
long count = 0;
//遍历一组数据,取出该组全部的value
for (LongWritable value : values) {
//全部的value累加 就是该单词的总次数
count +=value.get();
}
result.set(count);
//输出最闭幕果<单词,总次数>
context.write(key,result);
}
}
6.创建driver



输入以下代码:
package com.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 WordCountDriver{
public static void main(String[] args) throws Exception {
//设置文件对象
Configuration conf = new Configuration();
// 创建作业实例
Job job = Job.getInstance(conf, WordCountDriver.class.getSimpleName());
// 设置作业驱动类
job.setJarByClass(WordCountDriver.class);
// 设置作业mapper reducer类
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
// 设置作业mapper阶段输出key value数据范例
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//设置作业reducer阶段输出key value数据范例 也就是步伐最终输出数据范例
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
// 设置作业的输入数据路径
FileInputFormat.addInputPath(job, new Path(args[0]));
// 设置作业的输出数据路径
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//判断输出路径是否存在 如果存在删除
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(args[1]))){
fs.delete(new Path(args[1]),true);
}
// 提交作业并等候实行完成
boolean resultFlag = job.waitForCompletion(true);
//步伐退出
System.exit(resultFlag ? 0 :1);
}
}
7.设置log4j.properties



输入以下代码:
log4j.rootLogger=info,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=mapreduce_test.log
log4j.appender.R.MaxFileSize=1MB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG
(四)在本地运行项目
1.添加路径参数
先运行WordCountDriver
会提示错误,因为我们没有设置路径参数
这样设置
2.在D:\wordcount\input中添加文档1.txt。注意,input目录需要自己先建好,output可以不建。
3.运行
4.查验运行乐成
(五)在集群运行项目
1.在pom.xml中添加主类
先在WordCountDriver中复制主类名
再在pom.xml中的相应位置添加
2.打包
3.查看jar包
4.集群测试
4.1启动集群
4.2创建数据目录
hdfs dfs -mkdir -p /wordcount-mr/input
4.3将1.txt上传到HDFS中的 /wordcount-mr/input
4.4将1.txt上传到主节点的/root
4.5将1.txt上传至 /wordcount-mr/input
4.6将jar包上传到主节点
4.7运行jar包(我的jar包当时的名字是wordcont-mr-1.0.jar,所以一定用自己的jar包的名字,不要搞错了)
hadoop jar wordcont-mr-1.0.jar /wordcount-mr/input /wordcount-mr/output
4.8到HDFS上查看运行结果
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |