上一篇文章介绍了在ubuntu系统中安装Hadoop的伪分布式环境,这篇文章主要为MapReduce开发环境的搭建流程。
1.HDFS伪分布式配置
使用MapReduce时,如果需要与HDFS建立连接,及使用HDFS中的文件,还需要做一些配置。
首先进入Hadoop的安装目录
cd /usr/local/hadoop/hadoop2
- 1
- 1
在HDFS中创建用户目录
./bin/hdfs dfs -mkdir -p /user/hadoop
- 1
- 1
创建input目录,并将./etc/hadoop中的xml文件复制到分布式文件系统中
./bin/hdfs dfs -mkdir input
./bin/hdfs dfs -put ./etc/Hadoop/*.xml input
- 1
- 2
- 1
- 2
复制完成后,可以使用下面命令查看文件列表
./bin/hdfs dfs -ls input
- 1
- 1
2.开发环境搭建
1.1 调整虚拟机内存为2G+
1.2 eclipse Linux版本下载
下载地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/neon2
我下载的文件为:eclipse-jee-neon-2-linux-gtk-x86_64.tar.gz
1.3 为hadoop用户分配opt文件夹的操作权限
sudo chown hadoop /opt
sudo chmod -R 777 /opt
- 1
- 2
- 1
- 2
1.4 将下载的文件拷贝到opt文件夹下
1.5 解压(解压后文件夹名为eclipse)
cd /opt
sudo tar -zxf eclipse-jee-neon-2-linux-gtk-x86_64.tar.gz
- 1
- 2
- 3
- 1
- 2
- 3
1.6 下载eclispe的Hadoop插件(hadoop-eclipse-plugin-2.6.0.jar)
1.7 为hadoop用户分配eclipse文件夹的权限
sudo chown hadoop /opt/eclipse
sudo chmod -R 777 /opt/eclipse
- 1
- 2
- 1
- 2
然后将hadoop-eclipse-plugin-2.6.0.jar拷贝到eclipse的plugins文件夹下
1.8 通过命令行启动eclipse
cd /usr/local/bin
sudo ln -s /opt/eclipse/eclipse
- 1
- 2
- 1
- 2
这样设置完成后,以后在命令行中输入eclispe就可启动
eclipse
- 1
- 1
注意,选择工作区间一定要选在自己有操作权限的目录下,比如我是/home/hadoop/workspace
1.9 启动eclipse后,window - show view - other中将会有MapReduceTools
1.10 打开MapReduce窗口开始配置文件系统连接,要与/usr/local/hadoop/hadoop2/etc/hadoop/下的core-site.xml的配置保持一致
配置完成后,查看左边的DFS树目录变成了下图:
1.11 确保所有的守护进程都已开启
1.12 window -preference - Hadoop Map/Reduce 选择Hadoop的安装目录,我这里是 /usr/local/hadoop/hadoop2,配置完成后新建的hadoop项目会自动导入需要的jar包
1.13 File - new - Project ,选择Map/Reduce Product 新建一个Map/Reduce项目,然后再src下新建一个package,并新建一个WordCount测试类
package test;
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 {
//继承mapper接口,设置map的输入类型为<Object,Text>
//输出类型为<Text,IntWritable>
public static class Map extends Mapper<Object,Text,Text,IntWritable>{
//one表示单词出现一次
private static IntWritable one = new IntWritable(1);
//word存储切下的单词
private Text word = new Text();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
//对输入的行切词
StringTokenizer st = new StringTokenizer(value.toString());
while(st.hasMoreTokens()){
word.set(st.nextToken());//切下的单词存入word
context.write(word, one);
}
}
}
//继承reducer接口,设置reduce的输入类型<Text,IntWritable>
//输出类型为<Text,IntWritable>
public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
//result记录单词的频数
private static IntWritable result = new IntWritable();
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{
int sum = 0;
//对获取的<key,value-list>计算value的和
for(IntWritable val:values){
sum += val.get();
}
//将频数设置到result
result.set(sum);
//收集结果
context.write(key, result);
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "localhost:9001");
args = new String[]{"hdfs://localhost:9000/user/hadoop/input/count_in","hdfs://localhost:9000/user/hadoop/output/count_out"};
//检查运行命令
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("Usage WordCount <int> <out>");
System.exit(2);
}
//配置作业名
Job job = new Job(conf,"word count");
//配置作业各个类
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
然后将/usr/local/hadoop/hadoop2/etc/hadoop目录下的log4j.properties文件拷贝到src目录下(否则无法在控制台打印日志)
1.14.在input文件夹上右键,创建一个文件夹–count_in,在桌面创建两个文件word1.txt和word2.txt,并写入一些字符串,如:
aaaa
bbbb
cccc
aaaa
然后在count_in文件夹上右键,选择upload file to DFS ,选中word1.txt和word2.txt,将其导入DFS文件系统中
1.15 代码上邮件Run as – Run on Hadoop 运行程序,运行结束后在DFS中的文件夹上右键Refresh,会生产输出文件
输出的内容无误后,MapReduce的开发环境就搭建好了