作者:humphrey7247 | 来源:互联网 | 2023-10-13 16:56
介绍怎样在IntellijIdea中通过创建mavenproject配置MapReduce的编程环境。一、软件环境我使用的软件版本号例如以下:IntellijIdea2017.1M
介绍怎样在Intellij Idea中通过创建mavenproject配置MapReduce的编程环境。
一、软件环境
我使用的软件版本号例如以下:
- Intellij Idea 2017.1
- Maven 3.3.9
- Hadoop伪分布式环境( 安装教程可參考这里)
二、创建mavenproject
打开Idea,file->new->Project,左側面板选择mavenproject。(假设仅仅跑MapReduce创建javaproject就可以,不用勾选Creat from archetype,假设想创建webproject或者使用骨架能够勾选)
设置GroupId和ArtifactId。下一步。
设置project存储路径。下一步。
Finish之后,空白project的路径例如以下图所看到的。
完整的project路径例如以下图所看到的:
三、加入maven依赖
在pom.xml加入依赖。对于hadoop 2.7.3版本号的hadoop,须要的jar包有下面几个:
- hadoop-common
- hadoop-hdfs
- hadoop-mapreduce-client-core
- hadoop-mapreduce-client-jobclient
log4j( 打印日志)
pom.xml中的依赖例如以下:
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-commonartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-hdfsartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-mapreduce-client-coreartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-mapreduce-client-jobclientartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
四、配置log4j
在src/main/resources
目录下新增log4j的配置文件log4j.properties
。内容例如以下:
log4j.rootLogger = debug,stdout
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
五、启动Hadoop
启动Hadoop,执行命令:
cd hadoop-2.7.3/
./sbin/start-all.sh
訪问http://localhost:50070/查看hadoop是否正常启动。
六、执行WordCount(从本地读取文件)在project根目录下新建input目录,input目录下新增dream.txt,随便写入一些单词:
I have a dream
a dream
在src/main/java目录下新建包。新增FileUtil.java,创建一个删除output文件的函数,以后就不用手动删除了。内容例如以下:
package com.mrtest.hadoop;
import java.io.File;
/** * Created by bee on 3/25/17. */
public class FileUtil {
public static boolean deleteDir(String path) {
File dir = new File(path);
if (dir.exists()) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
deleteDir(f.getName());
} else {
f.delete();
}
}
dir.delete();
return true;
} else {
System.out.println("文件(夹)不存在!");
return false;
}
}
}
编写WordCount的MapReduce程序WordCount.java,内容例如以下:
package com.mrtest.hadoop;
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 java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
/** * Created by bee on 3/25/17. */
public class WordCount {
public static class TokenizerMapper extends
Mapper {
public static final IntWritable One= new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, one);
}
}
}
public static class IntSumReduce extends
Reducer {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable values,
Context context)
throws IOException, InterruptedException {
int sum = 0;
IntWritable val;
for (Iterator i = values.iterator(); i.hasNext(); sum += val.get()) {
val = (IntWritable) i.next();
}
this.result.set(sum);
context.write(key, this.result);
}
}
public static void main(String[] args)
throws IOException, ClassNotFoundException, InterruptedException {
FileUtil.deleteDir("output");
Configuration cOnf= new Configuration();
String[] otherArgs = new String[]{"input/dream.txt","output"};
if (otherArgs.length != 2) {
System.err.println("Usage:Merge and duplicate removal ");
System.exit(2);
}
Job job = Job.getInstance(conf, "WordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCount.TokenizerMapper.class);
job.setReducerClass(WordCount.IntSumReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
执行完成以后。会在project根目录下添加一个output目录。打开output/part-r-00000,内容例如以下:
I 1
a 2
dream 2
have 1
这里在main函数中新增了一个String类型的数组,假设想用main函数的args数组接受參数。在执行时指定输入和输出路径也是能够的。执行WordCount之前,配置Configuration并指定Program arguments就可以。
七、执行WordCount(从HDFS读取文件)在HDFS上新建目录:
hadoop fs -mkdir /worddir
假设出现Namenode安全模式导致的不能创建目录提示:
mkdir: Cannot create directory /worddir. Name node is in safe mode.
执行下面命令关闭safe mode:
hadoop dfsadmin -safemode leave
上传本地文件:
hadoop fs -put dream.txt /worddir
改动otherArgs參数,指定输入为文件在HDFS上的路径:
String[] otherArgs = new String[]{"hdfs://localhost:9000/worddir/dream.txt","output"};
八、代码下载
代码下载地址:http://download.csdn.net/detail/napoay/9799523