Hadoop(二):只有开发需求情况,即只在windows配置开发环境
如果你只是想开发代码,而不需要hadoop集群,那么就可以仅在windows环境下安装hadoop
准备
-
windows编译后的hadoop包,下面提供了windows10编译后的
链接: https://pan.baidu.com/s/189OTTMOZ8IZLXC3SsWr3TA 提取码: gxsn
-
配置好java
这一点就不用多说了,打开cmd输入java -version看看是否配好了java环境变量
-
开发java代码的IDE
可以用IDEA;Eclipse
配置环境变量
-
配置java环境变量
这个不用多说了,但是还是要确认一下自己是否配好了,去cmd确认一下
java -vesion
2. 配置hadoop环境变量
将刚下载的hadoop编译后的包解压,解压到没有英文目录的路径,然后打开环境变量配置。
在系统变量中添加HADOOP_HOME
在Path中加入
%HADOOP_HOME%\bin
然后确认关闭,打开cmd确认一下
创建工程
配置Maven,如果使用的IDEA就可以忽略了。
新建Maven项目,在pom添加如下(其中hadoop版本要跟你下载的一致,我这里是2.7.2)
<dependencies>
<!-- 配置hadoop的日志 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- 配置hadoop相应jar包 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
测试程序
下面程序是修改了源码中的example
package officialWordCount;
import java.io.PrintStream;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
public class WordCount {
public static void main(String[] args)
throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(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.addInputPath(job, new Path("C:\\Users\\Ace\\Desktop\\hadoop\\input\\wordcount"));
FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\Ace\\Desktop\\hadoop\\output\\wordcount"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, one);
}
}
}
class IntSumReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
this.result.set(sum);
context.write(key, this.result);
}
}
然后运行看看是否有结果。
如果出现
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
类似问题那么是因为,hadoop编译后的包与你的机器不匹配,你可以选择重新编译,或者新建一个类覆盖:解决链接如下:
https://blog.csdn.net/u011463794/article/details/105910685