1. 程式人生 > 其它 >hadoop詳細筆記(十七) 將MapReduce程式提交到Yarn上執行

hadoop詳細筆記(十七) 將MapReduce程式提交到Yarn上執行

1 windows上
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
// 設定訪問的叢集的位置
conf.set("fs.defaultFS", "hdfs://doit01:9000");
// 設定yarn的位置
conf.set("mapreduce.framework.name", "yarn");
// yarn的resourcemanager的位置
conf.set("yarn.resourcemanager.hostname", "doit01");
// 設定MapReduce程式執行在windows上的跨平臺引數
conf.set("mapreduce.app-submission.cross-platform","true");
job.setJar() ;
package com._51doit.yarn.wc;
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;

/**
* @Auther: 多易教育-行哥
* @Date: 2020/7/10
* @Description:
* 1 執行模式 預設是local 設定執行在yarn上
* 2 yarn的位置 resourcemanage
* 3 讀取資料 HDFS
* 4 使用者名稱
* 5 跨平臺引數
*
*/
public class WordCountDriver {
public static void main(String[] args) throws Exception {
// 1 配置物件
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
// 設定訪問檔案系統
conf.set("fs.defaultFS", "hdfs://linux01:9000");
// 設定MR程式執行模式 yarn
conf.set("mapreduce.framework.name", "yarn");
// yarn的resourcemanager的位置
conf.set("yarn.resourcemanager.hostname", "linux01");
// 設定MapReduce程式執行在windows上的跨平臺引數
conf.set("mapreduce.app-submission.cross-platform","true");
// 2 建立任務物件
Job job = Job.getInstance(conf, "wc");
job.setJar("C:\\Users\\ThinkPad\\Desktop\\demo.jar");
job.setJarByClass(WordCountDriver.class);
// 2.1 設定 map和reduce任務類
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//2.2 設定map和reduce 的輸出KV
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 2.3 設定reduce的個數 預設1
job.setNumReduceTasks(2);
// 2.3 設定輸入和輸出路徑
FileInputFormat.setInputPaths(job,new Path("/data/wc/"));
FileOutputFormat.setOutputPath(job,new Path("/data/wc_res"));
// 3 提交任務 等待程式執行完畢 返回值是否成功
boolean b = job.waitForCompletion(true);
System.exit(b?0:-1);
}
}
打包程式到指定的目錄中

2 linux上
打包程式,將jar包上傳到Linux作業系統

package com._51doit.yarn.wc;
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;

/**
* @Auther: 多易教育-行哥
* @Date: 2020/7/10
* @Description:
* 1 執行模式 預設是local 設定執行在yarn上
* 2 yarn的位置 resourcemanage
* 3 讀取資料 HDFS
* 4 使用者名稱
* 5 跨平臺引數
*
*/
public class WordCountDriver {
public static void main(String[] args) throws Exception {
// 1 配置物件
// System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
// 設定訪問檔案系統
//conf.set("fs.defaultFS", "hdfs://linux01:9000");
// 設定MR程式執行模式 yarn
conf.set("mapreduce.framework.name", "yarn");
// yarn的resourcemanager的位置
conf.set("yarn.resourcemanager.hostname", "linux01");
// 設定MapReduce程式執行在windows上的跨平臺引數
// conf.set("mapreduce.app-submission.cross-platform","true");
// 2 建立任務物件
Job job = Job.getInstance(conf, "wc");
// job.setJar("C:\\Users\\ThinkPad\\Desktop\\demo.jar");
job.setJarByClass(WordCountDriver.class);
// 2.1 設定 map和reduce任務類
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//2.2 設定map和reduce 的輸出KV
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 2.3 設定reduce的個數 預設1
job.setNumReduceTasks(2);
// 2.3 設定輸入和輸出路徑
FileInputFormat.setInputPaths(job,new Path("/data/wc/"));
FileOutputFormat.setOutputPath(job,new Path("/data/wc_res"));
// 3 提交任務 等待程式執行完畢 返回值是否成功
boolean b = job.waitForCompletion(true);
System.exit(b?0:-1);
}
}
在配置了HADOOP環境變數的機器上執行命令 執行MR程式

hadoop jar /demo.jar com._51doit.yarn.wc.WordCountDriver

20/07/14 14:41:46 INFO client.RMProxy: Connecting to ResourceManager at linux01/192.168.133.201:8032
20/07/14 14:41:47 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
20/07/14 14:41:47 INFO input.FileInputFormat: Total input files to process : 1
20/07/14 14:41:48 INFO mapreduce.JobSubmitter: number of splits:1
20/07/14 14:41:48 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1594624386350_0003 非本地模式
20/07/14 14:41:48 INFO impl.YarnClientImpl: Submitted application application_1594624386350_0003
20/07/14 14:41:48 INFO mapreduce.Job: The url to track the job: http://linux01:8088/proxy/application_1594624386350_0003/
20/07/14 14:41:48 INFO mapreduce.Job: Running job: job_1594624386350_0003

在yarn監控頁面可以檢視到任務


————————————————
版權宣告:本文為CSDN博主「白眼黑刺蝟」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/qq_37933018/article/details/107337603