Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟
Maven 翻譯為”專家”、”內(nèi)行”,是 Apache 下的一個(gè)純 Java 開發(fā)的開源項(xiàng)目?;陧?xiàng)目對象模型(Project Object Model 縮寫:POM)概念,Maven利用一個(gè)中央信息片斷能管理一個(gè)項(xiàng)目的構(gòu)建、報(bào)告和文檔等步驟。Maven 是一個(gè)項(xiàng)目管理工具,可以對 Java 項(xiàng)目進(jìn)行構(gòu)建、依賴管理。
在開發(fā)一些大型項(xiàng)目的時(shí)候,需要用到各種各樣的開源包jar,為了方便管理及加載jar,使用maven開發(fā)項(xiàng)目可以節(jié)省大量時(shí)間且方便項(xiàng)目移動(dòng)至新的開發(fā)環(huán)境。
開發(fā)環(huán)境
- 系統(tǒng):MacOS 10.14.1
- Hadoop:2.7.0
- Java:1.8.0
- Eclipse:4.6.2
- Maven: 3.3.9
Maven安裝
我使用的這個(gè)版本的Eclipse已經(jīng)自帶了Maven插件,不需要在自行安裝,因此我也沒有實(shí)際操作,本文就不介紹如何配置。
至于怎么知道自己使用的Eclipse是否自帶有Maven,可以在Eclipse->Preference->Maven->Installations查看是否有Maven及版本號。或者直接新建項(xiàng)目查看是否有Maven選項(xiàng)。
構(gòu)建Hadoop環(huán)境
創(chuàng)建Maven項(xiàng)目
打開Eclipse,F(xiàn)ile->new->project,選擇Maven,然后下一步next

選擇Creat a simple project,然后下一步next

輸入Group id和artifact id。然后finish。
groupid和artifactId被統(tǒng)稱為“坐標(biāo)”是為了保證項(xiàng)目唯一性而提出的,如果你要把你項(xiàng)目弄到maven本地倉庫去,你想要找到你的項(xiàng)目就必須根據(jù)這兩個(gè)id去查找。
groupId一般分為多個(gè)段,這里我只說兩段,第一段為域,第二段為公司名稱。域又分為org、com、cn等等許多,其中org為非營利組織,com為商業(yè)組織。舉個(gè)apache公司的tomcat項(xiàng)目例子:這個(gè)項(xiàng)目的groupId是org.apache,它的域是org(因?yàn)閠omcat是非營利項(xiàng)目),公司名稱是apache,artigactId是tomcat。
比如我創(chuàng)建一個(gè)項(xiàng)目,我一般會(huì)將groupId設(shè)置為cn.snowin,cn表示域?yàn)橹袊?,snowin是我個(gè)人姓名縮寫,artifactId設(shè)置為testProj,表示你這個(gè)項(xiàng)目的名稱是testProj,依照這個(gè)設(shè)置,你的包結(jié)構(gòu)最后是cn.snowin.testProj打頭。(引自 鏈接 )

完成上述步驟后,就可以在Project Explorer中看到剛剛創(chuàng)建的Maven項(xiàng)目。
增加Hadoop依賴
我使用的Hadoop 2.7版本,以下是我的POM配置文件
<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>practice.hadoop</groupId> <artifactId>simple-examples</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>simple-examples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.mrunit</groupId> <artifactId>mrunit</artifactId> <version>1.1.0</version> <classifier>hadoop2</classifier> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>2.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-yarn-api --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-yarn-api</artifactId> <version>2.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-auth --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>2.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-minicluster --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-minicluster</artifactId> <version>2.7.0</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-jobclient --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-jobclient</artifactId> <version>2.7.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
在Project Explorer中右鍵該項(xiàng)目,選擇build project,Maven就會(huì)根據(jù)POM.xml配置文件下載所需要的jar包。

稍等一段時(shí)間后,就可以看到Maven Dependencies中已經(jīng)下載好的jar包。
hadoop配置文件
運(yùn)行 MapReduce 程序前,務(wù)必將 /usr/local/Cellar/hadoop/2.7.0/libexec/etc/hadoop 中將有修改過的配置文件(如偽分布式需要core-site.xml 和 hdfs-site.xml),以及l(fā)og4j.properties復(fù)制到 src/main/resources/

MapReduce實(shí)例—WordCount
在 src/main/java/ 路徑下,創(chuàng)建java文件,代碼如下
import java.io.IOException;
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 static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
/**
* LongWritable, IntWritable, Text 均是 Hadoop 中實(shí)現(xiàn)的用于封裝 Java
* 數(shù)據(jù)類型的類,這些類實(shí)現(xiàn)了WritableComparable接口,
* 都能夠被串行化從而便于在分布式環(huán)境中進(jìn)行數(shù)據(jù)交換,你可以將它們分別視為long,int,String 的替代品。
*/
private final static IntWritable one = new IntWritable(1); // 值為1
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); // 對字符串進(jìn)行切分
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.addResource("classpath:/hadoop/core-site.xml");
conf.addResource("classpath:/hadoop/hdfs-site.xml");
conf.addResource("classpath:/hadoop/mapred-site.xml");
// String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
String[] otherArgs = {"/input", "/output"};
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputDirRecursive(job, true);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Mybatis Mapper模糊查詢的幾種方法
在Spring結(jié)合Mybatis進(jìn)行開發(fā)時(shí),實(shí)現(xiàn)模糊查詢是一個(gè)常見需求,在Mybatis中,LIKE查詢可以通過多種方式實(shí)現(xiàn),本文給大家介紹了Spring Mybatis Mapper模糊查詢的幾種方法,需要的朋友可以參考下2024-03-03
解決IDEA?JDK9沒有module-info.java的問題
這篇文章主要介紹了解決IDEA?JDK9沒有module-info.java的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
mybatisplus如何解決分頁最多500條數(shù)據(jù)
這篇文章主要介紹了mybatisplus如何解決分頁最多500條數(shù)據(jù)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
解決spring?data?jpa?saveAll()?保存過慢問題
這篇文章主要介紹了解決spring?data?jpa?saveAll()保存過慢問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java多線程之鎖的強(qiáng)化學(xué)習(xí)
Java多線程的鎖都是基于對象的,Java中的每一個(gè)對象都可以作為一個(gè)鎖。這篇文章主要來通過一下示例為大家強(qiáng)化一下鎖的相關(guān)知識的掌握,希望對大家有所幫助2023-02-02
新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目(圖文教程)
這篇文章主要介紹了新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目的圖文教程,需要的朋友可以參考下2018-01-01

