本文详述如何在windows开发环境下通过mvn+eclipse构建hadoop项目并运行
必备环境
- windows7操作系统
- eclipse-4.4.2
- mvn-3.0.3及用mvn生成项目架构(参阅mvn入门指南)
- hadoop-2.5.2(直接上hadoop官网下载hadoop-2.5.2.tar.gz并解压到某个目录)
windows7下环境配置
1、本地hadoop环境配置
添加环境变量HADOOP_HOME=E:\doc_api\ebook\hadoop-2.5.2
追加环境变量path内容:%HADOOP_HOME%\bin
2、bin下增加hadoop.dll,winutils.exe文件
从github或从我的csdn资源页下载hadoop.dll,winutils.exe,放置到${HADOOP_HOME}\bin目录下
构建hadoop项目
下面以经典的WordCount为例,构建我们第一个hadoop项目。
pom文件中加入依赖包
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.5.2</version>
<exclusions>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>jasper-compiler</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.5.2</version>
</dependency>
注意:hadoop-common引入项排除了jasper-compiler.jar包,否则可能与tomcat自带的jsp编译器冲突,报如下错误
org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/IProblem
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* @version 1.0
* @author tangqian
*/
public class WordCount extends Configured implements Tool {
public static void main(String[] args) throws Exception {
int result = ToolRunner.run(new Configuration(),new WordCount(), args);
System.exit(result);
}
@Override
public int run(String[] args) throws Exception {
Path inputPath, outputPath;
if(args.length == 2){
inputPath = new Path(args[0]);
outputPath = new Path(args[1]);
}else{
System.out.println("usage <input> <output>");
return 1;
}
Configuration conf = getConf();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
return job.waitForCompletion(true) ? 0 : 1;
}
public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
result.set(sum);
context.write(key, result);
}
}
}
然后在该类上右键Run As->Run Configurations->Arguments标签的Program arguments中指定输入路径和输出路径如下:
file:///e:/word.txt file:///e:/hadoop/result2
点Run即可运行该类,此时可在Console看到输出信息。等完成后,可到e:/hadoop/result2看到结果文件part-r-00000内容如下
is 1
test 2
this 1
two 1
说明:由于是在本地hadoop单机模式下运行,故采用本地文件系统(以file://开头指定输入输出路径)。
附
hadoop-2.5.2集群安装指南(参阅http://blog.csdn.net/tang9140/article/details/42869531)
如何修改Windows7下的hosts文件?
hosts文件一般在C:\Windows\System32\drivers\etc目录下,在windows7下如果不是管理员身份登录,可能无权限修改,此时可右键hosts文件->属性->安全->编辑,选择当前登录用户,开放修改权限即可,具体操作如下图。
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
hadoop2.7汇总:新增功能最新编译64位安装、源码包、API、eclipse插件下载
下载hadoop2.6.0的源码辛苦编译成的,网上没找到编译好现成的,费了几个小时用maven编译好的带源码的jar包,中间还得安装编译protoc,可以直接供eclipse使用。
windows下直接运行hadoop必需的工具(不需要安装Cygwin)。结合eclipse和mvn可快速在windows下环境下开发和调试hadoop程序
生成和安装说明要求: JDK 1.7以上Maven 3.0或更高版本首次构建的Internet连接(以获取所有Maven和Hadoop依赖项) Maven构建目标: 清洁:MVN清洁编译:mvn编译运行测试:MVN测试生成Eclipse文件:mvn eclipse:...
在centos7下,使用mvn,jdk 1.8.0_65,protoc 2.5.0,执行mvn install,以及mvn eclipse:eclipse -DskipTests之后的压缩文件。 可以导入到eclipse项目中,有可能报错
项目:maven-hadoop-java-wordcount-template 这是一个 ...编译你的项目要编译项目,请使用 maven 命令 mvn clean package运行您的应用程序使用 Hadoop 在你的 shell 中 hadoop jar your-hadoop-application.jar arg0
Maven在Eclipse中远程调试hadoop2.6.0项目 1.系统环境 调试:Win7,64bit Cluster: Linux, Centos 2.创建hadoop项目命令 mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org....
#Fuse 电子表格语料库工具该存储库... 运行mvn eclipse:eclipse为其中一个模块构建一个 Eclipse 项目。 运行mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true以构建一个带有 javadocs 和源链接的
或者在cat目录下输入 mvn eclipse:clean eclipse:eclipse 然后将项目导入到eclipse中,运行cat-home项目里得‘com.dianping.cat.TestServer’来启动CAT。 介绍内容来自:高可用架构 标签:大众点评
清洁: $ mvn clean编译: $ mvn compile创建罐子: $ mvn package创建Eclipse项目: $ mvn eclipse:eclipse使用 maven 运行(仅适用于独立): $ mvn exec:java -Dexec.mainClass="ch.hesso.master.Ngram" -Dexec....
电子地图还原数据源要求火花1.3+介绍该项目支持在Spark运行时环境中与Aliyun的基础服务(例如...>属性-> Java构建路径->添加JAR 选择并导入SDK 您可以在Eclipse项目中使用sdk玛文 <dependency> <groupId>com.aliyun.em
安装下载项目并运行命令: mvn package 编译的jar是target/CombineFileDemo-0.0.1-SNAPSHOT.jar 。用法hadoop jar CombineFileDemo-0.0.1-SNAPSHOT.jar TestMain <src> 执照版权所有:copyright:2014 Felix Chern ...
CAT是基于Java开发的实时应用监控平台,为美团点评提供了全面的...或者在cat目录下输入mvn eclipse:clean eclipse:eclipse然后将项目导入到eclipse中,运行cat-home项目里得‘com.dianping.cat.TestServer’来启动CAT。
在Windows中构建和运行此代码(通常是Hadoop)存在很多问题,因此请使用推荐的VM。 推荐的虚拟机 安装和提供的虚拟机。 您也可以从此链接(5.3 GB)下载虚拟机,请记住,您至少需要为VM分配4GB:[ ] 在自己的...