hadoop linux集群设置
- 详细步骤:参考文章
1.安装jdk1.8以上。设置环境变量。
2.安装hadoop,设置环境变量。
3.修改配置文件。
- 环境安装好之后可以登录网站查看:
- 集群情况:http://192.168.37.207:9870/dfshealth.html#tab-overview
- yarn节点情况:http://192.168.37.207:8088/cluster
windows远程调用集群
一.windows系统环境设置
- 详细步骤:参考文章
1.拷贝linux集群hadoop文件夹复制到linux系统
2.配置windows环境,hosts。
3.安装对应版本的hadoop.dll、winutils.exe。
二.windows远程调用集群计算
1.项目构成-代码附在最后
- wordMapper:map()方法
- wordReduce:reduce()方法
- wordCount:提交作业到集群运行
- resource:linux集群文件拷贝过来
2.设置运行环境
(1)mainclass格式都是:包名.类名
(2)program arguments:运行程序时需要两个参数,一个是输入文件,一个是结果输出路径。
其中第一个路径及文件必须是已经存在的,第二个参数中的output目录必须是不存在的(hdfs会自己创建输出文件,文件已存在会报错)。
(3)Working directory:工作路径,选择你本地安装的hadoop的路径。
3.提交job之后在网页查看任务运行状况
- IDE运行截图
- 网页截图:
- 可以看到任务提交次数和运行status,history查看详细记录。
代码Wordcount
public class WordCount {
private static String uri="hdfs://master:9000";
private static String log4jPropertiesPath="/log4j.properties";
private static String owner="wy";
public static void main(String[] args) throws Exception {
System.setProperty("hadoop.home.dir", "D:\\hadoop-3.1.2");
Configuration conf = new Configuration();
//远程操作配置
//配置了fs.defaultFS选项,则下面的setInputPaths等配置不需要再配置前缀
conf.set("fs.defaultFS", uri);
//MapReduce运行环境为yarn
conf.set("mapreduce.framework.name", "yarn");
//yarn的recourseManager的主机名
// conf.set("yarn.resourcemanager.hostname", "192.168.37.207");
//表示MapReduce是跨平台运行的
conf.set("mapreduce.app-submission.cross-platform", "true");
//设置jar包位置
conf.set("mapred.jar","C:\\javaProject\\idealProject\\Hadoop\\hadoopdemo\\build\\libs\\hadoopdemo-1.0-SNAPSHOT.jar");
Job job = Job.getInstance(conf,"myTask-1");
job.setMapperClass(WordMapper.class);
job.setReducerClass(WordReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//设置文件输入类型和文件输出类型
//参数代入文件
// String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
// //若每次传入的数据量不是两个,则显示报错信息。
// if (otherArgs.length !=2) {
// System.err.println("Usage: wordcount <in> <out>");
// System.exit(2);
// }
// FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
// FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
//代码写入文件
FileInputFormat.addInputPath(job, new Path("/input/wordcount/"));
FileOutputFormat.setOutputPath(job, new Path("/output/wordcount/resultOf_"+job.getJobName()));
//job.waitForCompletion(true);
System.exit(job.waitForCompletion(true)? 0 : 1);
}
}