VSCode:VSCode+Maven+Hadoop开发环境搭建
在Maven插件的帮助下,VSCode写Java其实非常方便。这一讲我们介绍如何借助maven用VScode搭建Hadoop开发环境。
1.Java环境安装
首先我们需要搭建好Java开发环境。我们需要从网站 https://www.oracle.com/java/technologies/downloads/ 下载指定版本的Java压缩包或安装包。压缩包需要解压到机器的指定目录,安装包直接傻瓜式安装即可。我这里下载的是Java17的MacOS安装包,运行后它默认给我安装在了/Library/Java/JavaVirtualMachines/temurin-17.jdk/
目录下。
然后配置环境变量,Mac用户在~/.zshrc
中添加:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
然后source ~/.zshrc
即可(Linux用户我没记错的话应该是~/.bashrc
文件)。
可以运行java -version
命令查看当前机器的Java版本。
(base) lonelyprince7@lonelyprince7deMacBook-Pro ~ % java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode)
然后读者需要在VSCode中安装以下Java插件:
其中需要注意,Maven for Java插件是用来构建Java大型项目的(也就是说不只是使用JRE内部的包,而且使用外包的JDK包。内部的包用java
命令编译的时候就会自动帮我们导入,但外部的包要稍微复杂一些,最简单的方式就是使用maven工具了)。
安装Maven
如果你是Mac系统且安装了homebrew,可以直接下列命令运行安装
brew install maven
你也可以前往Maven官网 https://maven.apache.org/download.cgi 下载并解压到自定义目录。
安装完后记得配置环境变量:
export M2_HOME=/opt/homebrew/Cellar/maven/3.8.2/libexec
export PATH=${PATH}:${M2_HOME}/bin
此处/opt/homebrew/Cellar/maven/3.8.2/libexec
是我的maven安装位置,修改成你实际的自定义目录即可。
然后source ~/.zshrc
即可(同样,Linux用户应该是~/.bashrc
文件)。
运行mvn -version
可以查看maven版本。
(base) orion-orion@MacBook-Pro libexec % mvn -version
Apache Maven 3.8.2 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /opt/homebrew/Cellar/maven/3.8.2/libexec
Java version: temurin-17, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "12.3.1", arch: "aarch64", family: "mac"
3.新建Maven项目
首先,用VSCode打开一个存放用于项目的目录(在我电脑上是 "/Users/lonelyprince7/Documents/LocalCode/Hadoop-MapReduce"
),然后在右键菜单中选择从Maven原型中创建新项目。
然后从下拉选单中选择“maven-archetype-quickstart”。
然后随便选择一个版本即可。
然后再输入组织的名称(该名称在将你的项目打包发布有用)。组织名称一般命名为`com.×××`,这里我命名为`com.orion`。注意组织名称只能由小写字符和下换线组成,不能包括大写字符和空格之类的东西。 接下来输入工程(archetype)的名称,这里我们命名为"hello_world"即可。然后我们的工程目录就选择 "/Users/lonelyprince7/Documents/LocalCode/Hadoop-MapReduce"
就行。
之后VSCode会提示是否跳转到"hello_world"工程的目录,选择“是”,然后就退跳转到该目录。
进入该目录后,我们会发现控制台正在初始化Maven配置,有以下打印输出:
其中,会跳出一些选项让我们选择,我们只需要全部按回车选择默认值即可。
最后,我们看到项目"hello_world"成功创建,项目目录如下:
3.进行测试
我们可以看到App.java已经为我们生成,为如下内容:
package com.orion;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
pom.xml只引入了最基本的junit依赖包,并配置了一些基本的Maven项目初始化的内容:
<?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.orion</groupId>
<artifactId>hello_world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hello_world</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
选中App.java
文件按F5
进行编译并运行,看到控制台成功打印Hello World!
:
(base) lonelyprince7@lonelyprince7deMacBook-Pro hello_world % /usr/bin/env /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:50684 -XX:+ShowCodeDetailsInExceptionMessages -cp /Users/lonelyprince7/Documents/LocalCode/Hadoop-MapReduce/hello_world/target/classes com.orion.App
Hello World!
我们接下来试试用Maven导入Hadoop依赖。我们只需要在pom.xml依赖项就行。相关的依赖项的名称和版本号我们可以在Maven仓库的官网 https://mvnrepository.com/ 进行查询,比如我们可以查询到Hadoop-common(3.3.1)版本的依赖项如下:
一般来说,Hadoop项目需要引入hadoop-common、hadoop-hdfs、hadoop-client、hadoop-yarn-api这几个api,我们需要将下列标签加入<dependencies> <dependencies/>
之间,也即:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 导入hadoop依赖环境 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
但是聪明的逆可能已经注意到了,这样版本号就完全属于“硬编码”了。后期我要修改hadoop的版本号,只能把上面的挨个修改,非常麻烦。好在我们在<properties></properties>
标签之间先集中定义好一些后面要使用的常量(比如版本号),后面再使用${变量名}
的形式来引用即可。如我们将hadoop的版本号hadoop.version
追加到<properties></properties>
标签之间:
<!-- 集中定义版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<hadoop.version>3.3.1</hadoop.version> <!--这里追加hadoop版本号-->
</properties>
然后后面我们就可以使用${hadoop.version}
来替代前面频繁出现的3.3.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.orion</groupId>
<artifactId>hello_world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hello_world</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!-- 集中定义版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<hadoop.version>3.3.1</hadoop.version> <!--这里追加hadoop版本号-->
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 导入hadoop依赖环境 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>s
</project>
按ctrl/command+s
保存,项目会对pom.xml
重新解析,导入我们添加进入的包。
最后我们尝试在App.java
中导入hadoop相关的包:
package com.orion;
import org.apache.hadoop.fs.FileSystem;
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;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
按'F5'重新编译运行,成功打印输出Hello World!
,说明我们的包导入成功。
(base) lonelyprince7@lonelyprince7deMacBook-Pro hello_world % /usr/bin/env /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:54181 --enable-preview -XX:+ShowCodeDetailsInExceptionMessages -cp "/Users/lonelyprince7/Library/Application Support/Code/User/workspaceStorage/fe34994f622cdbfbf60e8ed045c6bde3/redhat.java/jdt_ws/jdt.ls-java-project/bin" com.orion.App
Hello World!
注意,如果报错org.apache.hadoop.fs.FileSystem cant be resolved
这种错误,则需要先清空项目缓存,然后再重新编译运行。
至此,我们已经成功用VScode+Maven创建好一个Hadoop的开发环境。在Hadoop编程中,最基本的一种分布式编程范式即MapReduce编程。在后面我们会借由“WordCount”(词频统计)这个MapReduce编程的入门项来讲解Hadoop中MapReduce编程的基本语法,敬请继续关注。