01 ssh免密安装
02 jdk安装
03 hadoop伪分布式安装
04 hadoop全分布式
05 hadoop高可用
06 用hadoop的sdk进行开发
07 搭建yarn
上次在hdfs上面搭建了yarn对于不同客户端的计算要求可以通过resource manager 和nodemanger协调在每个datenode节点上进行计算。理论的东西这里不展开了,直接开始动手,大家可以也动手试试,真正能够解决技术问题才能有提高对吧。如果条件有限的兄弟也可以看看,增加一些体验的东西。遇到类似的问题也好有个思路。接下来我们在yarn的基础上写一个mapreduce的程序。
准备
这里准备了一个txt文档里面有很多单词,我们要计算这些单词出现的次数。
这个文档内主要是“hello”,“world”组成,我们来统计这两个单词出现的次数。这里我们提前把这个要处理的文件生成并且上传到对应的目录上去。/user/root/hello.txt
这里我们通过eclipse连接hdfs数据库,并且上传文件。
编码
这里我们需要做mapreduce的操作。map 是对单词做统计,每个单词出现一次就标记一个1,reduce就是对map的结果进行分类计算,也就是做累加的操作。这里我们建立了一个 叫做“com.test.hadoop.mr”的包,并且建立了三个java 类文件。
MyWC文件,主要负责生成mapreduce任务,并且制定输入输出文件,并且关联mapper和reducer类。
public class MyWC {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration(true);
Job job = Job.getInstance(conf);
job.setJarByClass(MyWC.class);
//mapreduce 任务名字
job.setJobName("myjob01");
//需要处理的文件
Path input = new Path("/user/root/hello.txt");
FileInputFormat.addInputPath(job, input);
//处理以后的输出文件
Path output = new Path("/data/wc/output");
//为了实验方便每次运行都删除之前输出的文件。
if (output.getFileSystem(conf).exists(output)){
output.getFileSystem(conf).delete(output,true);
}
//把任务和输出的文件做绑定
FileOutputFormat.setOutputPath(job, output);
//设置mapper 类
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置reducer类
job.setReducerClass(MyReducer.class);
//执行任务
job.waitForCompletion(true);
}
}
mapper类
public class MyMapper extends Mapper {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object 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);
}
}
}
reducer类
public class MyReducer extends Reducer {
private 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);
}
}
打包发布
针对我们写的jar包com.test.hadoop.mr 右键选择export,然后选择JAR file
选择一个路径保存起来
然后把这个jar包上传到hdfs的服务器上面。我这里用xftp上传的,上传的目录是/opt/的。
在节点node01上运行这mapreduce jar
hadoop jar mywc.jar com.test.hadoop.mr.MyWC
操作完成以后可以去看处理以后的文件
hdfs dfs -ls /data/wc/output
同时我们通过elclipse的插件也可以看到hdfs上有一个mapreduce之后生成的文件
并且可以看到"hello"和"world"各自出现了多少次。
由于我这里配置了node03和node04作为resourcemanager,这个时候node03作为主节点,所以可以通过后台看到这个运行的job
注意配置
整个过程比较简单,建议大家动手做一下。我这里遇到两个配置上的坑和大家分享一下。如果没有遇到类似的问题可以直接跳过了。
mapreduce 需要配置运行的路径这里如果没有配置运行程序会报错
node01配置如下
vi /usr/hadoop-3.1.2/etc/hadoop/mapred-site.xml
yarn.app.mapreduce.am.env
HADOOP_MAPRED_HOME=/usr/hadoop-3.1.2
mapreduce.map.env
HADOOP_MAPRED_HOME=/usr/hadoop-3.1.2
mapreduce.reduce.env
HADOOP_MAPRED_HOME=/usr/hadoop-3.1.2
mapreduce.application.classpath
/usr/hadoop-3.1.2/share/hadoop/mapreduce/*, /usr/hadoop-3.1.2/share/hadoop/mapreduce/lib/*
配置完毕以后分发到node02,node03,node04上去。
针对yarn-site.xml文件的配置,这里我们使用node03和node04作为两个resourcemanager,互为主备所以需要指定两个服务器的通讯端口,当接到mapreduce的请求之后,resourcemanager 会把他们分发到对应的datenode上面由nodemanager协助计算。所以这里需要配置端口,以及classpath。
在node01运行
vi /usr/hadoop-3.1.2/etc/hadoop/yarn-site.xml
yarn.application.classpath
/usr/hadoop-3.1.2/etc/hadoop/, /usr/hadoop-3.1.2/share/hadoop/common/*, /usr/hadoop-3.1.2/share/hadoop/common/lib/*, /usr/hadoop-3.1.2/share/hadoop/hdfs/*, /usr/hadoop-3.1.2/share/hadoop/hdfs/lib/*, /usr/hadoop-3.1.2/share/hadoop/yarn/*, /usr/hadoop-3.1.2/share/hadoop/yarn/lib/*
yarn.resourcemanager.address.rm1
node03:8032
yarn.resourcemanager.scheduler.address.rm1
node03:8030
yarn.resourcemanager.webapp.address.rm1
node03:8088
yarn.resourcemanager.resource-tracker.address.rm1
node03:8031
yarn.resourcemanager.admin.address.rm1
node03:8033
yarn.resourcemanager.ha.admin.address.rm1
node03:23142
yarn.resourcemanager.address.rm2
node04:8032
yarn.resourcemanager.scheduler.address.rm2
node04:8030
yarn.resourcemanager.webapp.address.rm2
node04:8088
yarn.resourcemanager.resource-tracker.address.rm2
node04:8031
yarn.resourcemanager.admin.address.rm2
node04:8033
yarn.resourcemanager.ha.admin.address.rm2
node04:23142
完成配置以后将此文件分发到node02,node03,node04三个服务器上。
这里的配置问题困扰了我很久,希望大家到这里的时候能够顺利通过。
喜欢的点个关注,谢谢各位。