1.1用Maven创建一个标准化的Java项目
mvn archetype:generate -DgroupId=com.baidu -DartifactId=InstructionsHdfs -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
注:
Problem:出错的地方
Unable to add module to the current project as it is not of packaging type ‘pom’
原因:maven不允许在使用maven创建的项目里面,再用maven来创建文件。我在eclipseWorkSpace文件夹里边不小心方了pom文件,所以maven以为eclipseWorkSpace文件夹是maven创建的文件,所以不允许再在eclipseWorkSpace里边创建文件
方法:删去eclipseWorkSpace下的pom.xml(不要乱放文件)
1.2导入项目到eclipse
打开eclipse,在工作栏右键import。注意:import的是maven文件
效果:
1.3增加hadoop依赖
修改文件:pom.xml
注:这个文件的意义是,你把你工程需要的东西全部都写到里边,待会后边会下载这些你需要的依赖
举个例子:我使用的是hadoop2.9.2版本,所以我这么写
4.0.0
my.hadoopstudy
hadoopstudy
jar
1.0-SNAPSHOT
hadoopstudy
http://maven.apache.org
org.apache.hadoop
hadoop-common
2.9.2
org.apache.hadoop
hadoop-hdfs
2.9.2
org.apache.hadoop
hadoop-client
2.9.2
junit
junit
3.8.1
test
Problem:出错的地方
网上的教程一个模板的都写
org.apache.hadoop
hadoop-core
1.0.3
你这要注意:这里指的是他的版本是hadoop1.0.3,所以他写的是1.0.3。比如我安装的是hadoop2.9.2,如果我这样写,在后边运行的时候就会出现:
Server IPC version 9 cannot communicate with client version 4错误。但是又不能像下面这样写:因为maven没有hadoop-core2.9.2版本,要像我上边那样,用对应版本的hadoop-common、hadoop-client
org.apache.hadoop
hadoop-core
2.9.2
1.4下载依赖
进入刚才创建的那个文件夹,执行命令
mvn clean install
1.5从Hadoop集群环境下载hadoop配置文件
这里要配置三个文件:core-site.xml、hdfs-site.xml、mapred-site.xml
我的配置如下
fs.default.name
hdfs://host:9000
dfs.replication
1
mapred.job.tracker
hdfs://host:9001
养成习惯,保存在src/main/resources/hadoop目录下面
1.6 配置本地host,增加master的域名指向
要明白这个的意义是什么:可以想一下,上面第五步,配置的时候,全都是用host这个量,但是host是什么?得让计算机知道你的host是什么
找到host,打开,添加下面一行(如果hosts文件是只读的,把可读文件权限改成可写)
host 你的hadoopIP
1.7 写个程序测试一下
前提是:我已经搭建好HDFS,并且启动了hadoop的结点了,所以不要像网上教程一样,傻傻的复制代码(还用的是别人的HDFS配置运行程序),觉得无法运行
下面试着在eclipse上运行最简单的字数统计程序:WordCount
1.7.1 WordCountJobSubmitter
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCountJobSubmitter {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();//实例化,从Hadoop的配置文件里读取参数
//String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();//args命令行参数
//if (otherArgs.length != 2) {//判断参数的个数是否正确
// System.err.println("Usage: wordcount ");
//System.exit(2);
//}
Job job = new Job(conf, "wordcount");//job_name = "wordcount"
job.setJarByClass(WordCountJobSubmitter.class);//输入
job.setMapperClass(WordCountMapper.class); //输入
job.setReducerClass(WordCountReducer.class); //输入
job.setOutputKeyClass(Text.class); //输出
job.setOutputValueClass(IntWritable.class); //输出
System.out.println("1");
FileInputFormat.addInputPath(job, new Path("hdfs://193.178.136.132:9000/user/nancy/readme.txt"));//输入文件
FileOutputFormat.setOutputPath(job, new Path("hdfs://193.178.136.132:9000/user/result/wordcount/try2"));//输出文件
System.out.println("2");
System.exit(job.waitForCompletion(true) ? 0 : 1);//若执行完毕,退出
}
}
1.7.2 WordCountMapper
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;
public class WordCountMapper extends Mapper
1.7.3 WordCountReducer
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
//输入键类型,输入值类型,输出键类型, 输出值类型
public class WordCountReducer extends Reducer {
IntWritable result = new IntWritable();
public void reduce(Text key, Iterable values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}