Yarn的Tool接口案例

Tool接口环境准备

之前写wordcount里通过命令行传入的参数来获取输入路径与输出路径。执行命令

[ranan@hadoop102 hadoop-3.1.3]$ hadoop jar wc.jar
com.ranan.mapreduce.wordcount2.WordCountDriver /input
/output1

期望可以动态传参,结果报错,被人误是第一个输出参数

[rana@hadoop102 hadoop-3.1.3]$ hadoop jar wc.jar
com.rana.mapreduce.wordcount2.WordCountDriver -
D mapreduce.job.queuename=root.test /input /output2

需求:自己写的程序也可以动态修改参数。编写Yarn的Tool接口

1 新建Maven项目YarnDemo

修改Maven设置

修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion
<groupId>com.ranan</groupId>
<artifactId>YarnDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--打包使用的是JDK8-->
<maven.conpiler.source>8</maven.conpiler.source>
<maven.conpiler.target>8</maven.conpiler.target>
</properties> <!-- 新增依赖 -->
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
</project>

注意

打包用的JDK8,hadoop里面是JDK8,但是本地安装的是JDK11、Maven使用的是JDK11。

所以执行命令的时候会报错。

hadoop3.x目前只支持jdk1.8。修改版本到JDK8。

修改Java编译器

创建包名

编写代码

使用ToolRunner的方法运行

WordCount.class里编写Mapper,Reducer及部分驱动代码

driver驱动里面对conf进行设置

WordCount.class

package com.ranran.yarn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.Tool; import java.io.IOException; /**
* @author ranan
* @create 2021-11-03 20:13
*/
public class WordCount implements Tool {
//配置
private Configuration conf;
//核心驱动(conf 需要传入,不能直接new)
public int run(String[] args) throws Exception {
Job job = Job.getInstance(conf);
//设置jar包驱动
job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WorCountReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1])); return job.waitForCompletion(true)?0:1;
}
//conf的set方法,用于driver驱动设置conf配置信息
public void setConf(Configuration configuration) {
this.conf = conf;
}
//get方法
public Configuration getConf() {
return conf;
}
//mapper
public static class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable>{
private Text outk = new Text();
private IntWritable outV = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//获取一行
String line = value.toString();
//切割
String[] words = line.split(" ");
//循环遍历写出
for (String word : words) {
outk.set(word);
context.write(outk,outV);
}
}
}
//reduce
public static class WorCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values){
sum +=value.get();
} context.write(key,new IntWritable(sum));
}
}
}

WordCountDriver.class

起过滤效果

package com.ranran.yarn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import java.util.Arrays; /**
* @author ranan
* @create 2021-11-03 21:24
*/
public class WordCountDriver { private static Tool tool; //处理Tool接口相关
public static void main(String[] args) throws Exception {
//实例化配置信息
Configuration conf = new Configuration(); /*
命令行动态传过来的参数进行处理:hadoop jar wc.jar com.rana.mapreduce.wordcount2.WordCountDriver -D mapreduce.job.queuename=root.test /input /output2 --> hadoop jar wc.jar com.ranan.mapreduce.wordcount2.WordCountDriver /input /output1
*/ //合法性校验 args[0]:wordcount -D 这部分
switch (args[0]){
//执行wordcount程序
case "wordcount":
//WordCount实现了Tool接口
tool = new WordCount();
break;
default:
throw new RuntimeException("no such tool" + args[0]);
}
//执行程序,第一个参数配置信息,第二个tool接口,第三个接口传参
//只传输入输出路径到tool
int run = ToolRunner.run(conf, tool, Arrays.copyOfRange(args, 1, args.length));
System.exit(run);
}
}

打包jar上传到集群

[ranan@hadoop102 hadoop-3.1.3]$ rz

在 HDFS 上准备输入文件,假设为/input 目录,向集群提交该 Jar 包,先看两个参数的是否能运行。

com.ranan.yarn.WordCountDriver驱动的全类名

[ranan@hadoop102 hadoop-3.1.3]$ yarn jar YarnDemo-1.0-SNAPSHOT.jar com.ranran.yarn.WordCountDriver wordcount /input /output

注意此时提交的 3 个参数,第一个用于生成特定的 Tool,第二个和第三个为输入输出目录。此时如果我们希望加入设置参数,可以在 wordcount 后面添加参数,例如:

[ranan@hadoop1022 hadoop-3.1.3]$ yarn jar YarnDemo-1.0-SNAPSHOT.jar wordcount -Dmapreduce.job.queuename=root.test /input /output1

注:以上操作全部做完过后,快照回去或者手动将配置文件修改成之前的状态,因为本身资源就不够,分成了这么多,不方便以后测试

Yarn的Tool接口案例的更多相关文章

  1. Spark集群之yarn提交作业优化案例

    Spark集群之yarn提交作业优化案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.启动Hadoop集群 1>.自定义批量管理脚本 [yinzhengjie@s101 ...

  2. 关于Tool接口--------hadoop接口:extends Configured implements Tool 和 ToolRunner.run

    我们在写Hadoop--map/reduce程序时,遇到使用按文件url来分析文件----------多表连接的DistributedCache方式,看不懂使用extends Configured i ...

  3. httprunner学习1-环境与登录接口案例

    前言 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试. 具有以下优点: 继承 Requests 的全部特性,轻松实 ...

  4. Yarn 公平调度器案例

    目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...

  5. 记录ABAP开发的日常——SAP_PO开发同步接口案例

    前言:在项目中遇到任务PO接口,需求是SRM发送采购订单信息给SAP,SAP根据信息调用BAPI同步数据,在此作为案例记录. 本次接口采用的协议是SOAP,当然也有其他的协议比如REST等等,在此不做 ...

  6. 复习 - node.js(接口案例)

    其实复习一次的作用真实太大了,真的,自从上次ajax开始其实就开始i有点懵懵懂懂的感觉,一直拖想到了node在去回顾一遍,这一次回去复习,ajax已经很熟练了,node之前搞不懂那些原理也顺清楚了好多 ...

  7. 笔记本USB接口案例分析和是实现

    笔记本电脑 笔记本电脑(laptop)通常具备使用USB设备的功能.在生产时,笔记本都预留了可以插入USB设备的USB接口,但具体是什么USB设备,笔记本厂商并不关心,只要符合USB规格的设备都可以 ...

  8. 笔记本Usb接口案例

    笔记本电脑通常具备使用USB设备的功能.在生产的时候,笔记本都预留了可以插入USB设备的USB接口.但具体是什么USB设备,笔记本厂商并不关心,只要符合USB规格的设备都可以. 定义USB接口,具备最 ...

  9. 获取APP最新版本的接口案例

    思路: 开发初期.安卓的应用可能没有上传到应用市场,可以把应用apk放到服务器上,供用户下载.把对应用的版本信息整理成为一个XML文件,放到服务器上,通过接口读取xml文件,获取有版本信息,然后安卓端 ...

随机推荐

  1. 你一定不知道的Unsafe用法

    Unsafe是什么 首先我们说Unsafe类位于rt.jar里面sun.misc包下面,Unsafe翻译过来是不安全的,这倒不是说这个类是不安全的,而是说开发人员使用Unsafe是不安全的,也就是不推 ...

  2. JavaScript中的this对象指向理解

    在JavaScript中,this不是固定不变的,它的指向取决于上下文环境,一般的,认为this指向使用它时所在的对象.主要有以下几类指向: 在方法中,this 表示该方法所属的对象. 如果单独使用, ...

  3. gawk使用方法简介

    转载:gawk 使用方法简介 - 简书 (jianshu.com) gawk 是最初 Unix 系统上 awk 程序的 GNU 版本.相对于作为流式编辑器的 sed 而言,它提供了更为强大的编程语言特 ...

  4. svn与git区别

    代码扫描工具介绍:https://baijiahao.baidu.com/s?id=1629218655164599200&wfr=spider&for=pc Git和SVN的区别与联 ...

  5. testNG 注解使用说明

    1.TestNG常用注解 @BeforeSuite 标记的方法:在某个测试套件(suite)开始之前运行 @BeforeTest 在某个测试(test)开始之前运行 @BeforeClass 在某个测 ...

  6. 大白话讲解如何解决HttpServletRequest的请求参数只能读取一次的问题

    大家在开发过程中,可能会遇到对请求参数做下处理的场景,比如读取上送的参数中看调用方上送的系统编号是否是白名单里面的(更多的会用request中获取IP地址判断).需要对请求方上送的参数进行大小写转换或 ...

  7. 【AI测试】人工智能 (AI) 测试--第二篇

    测试用例 人工智能 (AI) 测试 或者说是 算法测试,主要做的有三件事. 收集测试数据 思考需要什么样的测试数据,测试数据的标注 跑测试数据 编写测试脚本批量运行 查看数据结果 统计正确和错误的个数 ...

  8. lua入门之环境搭建、第一个demo

    前言 前段时间因为有些项目功能需要,自己研究了下lua,今天整理下,并以一个demo为示例演示 手机上的运行效果 分为几个步骤来逐步讲解. 1.lua介绍,为什么选择它? 2.环境安装 3.撸一个简单 ...

  9. Visual Studio 2019连接MySQL数据库详细教程

    前言 如果要在 Visual Studio 2019中使用MySQL数据库,首先需要下载MySQL的驱动 Visual Studio默认只显示微软自己的SQL Server数据源,点击其它也是微软自己 ...

  10. linux删除文件未释放

    https://access.redhat.com/solutions/2316 $ /usr/sbin/lsof | grep deleted ora 25575 data 33u REG 65,6 ...