主頁 >  其他 > (超詳細)MapReduce作業原理及基礎編程

(超詳細)MapReduce作業原理及基礎編程

2021-11-12 07:46:12 其他

MapReduce作業原理及基礎編程(代碼見文章后半部分)

JunLeon——go big or go home


目錄

MapReduce作業原理及基礎編程(代碼見文章后半部分)

一、MapReduce概述

1、什么是MapReduce?

2、WordCount案例決議MapReduce計算程序

(1)運行hadoop自帶的樣例程式

(2)MapReduce作業程序

3、Shuffle程序詳解

二、MapReduce編程基礎

1、Hadoop資料型別

2、資料輸入格式InputFormat

3、輸入資料分塊InputSplit和資料記錄讀入RecordReader

4、資料輸出格式OutputFormat

5、資料記錄輸出類RecordWriter

6、Mapper類

7、Reduce類

三、MapReduce專案案例

1、經典案例——WordCount

2、計算考試平均成績

3、網站日志分析


前言:

Google于2003年在SOSP上發表了《The Google File System》,于2004年在OSDI上發表了《MapReduce: Simplified Data Processing on Large Clusters》,于2006年在OSDI上發表了《Bigtable: A Distributed Storage System for Structured Data》,這三篇論文為大資料及云計算的發展奠定了基礎,

一、MapReduce概述

1、什么是MapReduce?

MapReduce是一個分布式、并行處理的計算框架,

MapReduce 把任務分為 Map 階段和 Reduce 階段,開發人員使用存盤在HDFS 中資料(可實作快速存盤),撰寫 Hadoop 的 MapReduce 任務,由于 MapReduce作業原理的特性, Hadoop 能以并行的方式訪問資料,從而實作快速訪問資料,

表1 map函式和rudece函式

函式輸入輸出說明
map

<k1,v1>

<0,helle world>

<12,hello hadoop>

List<k2,v2>

<hello,1>

<world,1>

<hello,1>

<hhadoop,1>

將獲取到的資料集進一步決議成<key,value>,通過Map函式計算生成中間結果,進過shuffle處理后作為reduce的輸入
reduce

<k2,List(v2)>

<hadoop,1>

<hello,{1,1}>

<world,1>

<k3,v3>

<hadoop,1>

<hello,2>

<world,1>

reduce得到map輸出的中間結果,合并計算將最終結果輸出HDFS,其中List(v2),指同一k2的value

MapReduce體系結構主要由四個部分組成,分別是:Client、JobTracker、TaskTracker以及Task

  1)Client

  用戶撰寫的MapReduce程式通過Client提交到JobTracker端 用戶可通過Client提供的一些介面查看作業運行狀態,

  2)JobTracker

  JobTracker負責資源監控和作業調度 JobTracker 監控所有TaskTracker與Job的健康狀況,一旦發現失敗,就將相應的任務轉移到其他節點 JobTracker 會跟蹤任務的執行進度、資源使用量等資訊,并將這些資訊告訴任務調度器(TaskScheduler),而調度器會在資源出現空閑時,選擇合適的任務去使用這些資源,

  3)TaskTracker

  TaskTracker 會周期性地通過“心跳”將本節點上資源的使用情況和任務的運行進度匯報給JobTracker,同時接收JobTracker 發送過來的命令并執行相應的操作(如啟動新任務、殺死任務等) TaskTracker 使用“slot”等量劃分本節點上的資源量(CPU、記憶體等),一個Task 獲取到一個slot 后才有機會運行,而Hadoop調度器的作用就是將各個TaskTracker上的空閑slot分配給Task使用,slot 分為Map slot 和Reduce slot 兩種,分別供MapTask 和Reduce Task 使用,

  4)Task

  Task 分為Map Task 和Reduce Task 兩種,均由TaskTracker 啟動,

MapReduce各個執行階段:

MapReduce應用程式執行程序:

可以參考大佬黎先生的博客:MapReduce基本原理及應用 - 黎先生 - 博客園

2、WordCount案例決議MapReduce計算程序

(1)運行hadoop自帶的樣例程式

WordCount案例是一個經典案例,是Hadoop自帶的樣例程式,

作用:統計單詞數量(出現的次數)

應用:求和、求平均值、求最值,

jar包存盤在$HADOOP_HOME/share/hadoop/mapreduce/

$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.3.jar

例如:

步驟:

1.在本地創建一個檔案

輸入以下內容:

2.上傳到HDFS指定目錄

在HDFS中創建指定檔案:

上傳檔案:

3.使用hadoop jar命令運行jar程式,統計單詞數量

4.輸出結果

執行部分程序:

查看生成的檔案:

查看計算結果:

(2)MapReduce作業程序

作業流程是Input從HDFS里面并行讀取文本中的內容,經過MapReduce模型,最終把分析出來的結果用Output封裝,持久化到HDFS中,

1.Mapper作業程序:

附上Mapper階段代碼:

public static class WorldCount_Mapper extends Mapper<LongWritable, Text, Text, IntWritable>{

		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			System.out.println("split:<" + key + ","+ value + ">" );
			String[] strs = value.toString().split(" ");
			for (String string : strs) {
				System.out.println("map:<" + key + ","+ value + ">" );
				context.write(new Text(string),new IntWritable(1));
			}
	    }
    }

KEYIN--LongWritable:輸入key型別,記錄資料分片的偏移位置

VALUEIN—Text:輸入的value型別,對應分片中的文本資料

KEYOUT--Text:輸出的key型別,對應map方法中計算結果的key值

VALUEOUT—IntWritable:輸出的value型別,對應map方法中計算結果的value值

Mapper類從分片后傳出的背景關系中接收資料,資料以型別<LongWritable,Text>的鍵值對接收過來,通過重寫map方法默認一行一行的讀取資料并且以<key,value>形式進行遍歷賦值,

2.Reducer作業程序:

附上Reducer階段代碼:

public static class WorldCount_Reducer extends Reducer<Text, IntWritable, Text, IntWritable>{

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
			int index  = 0;
			for (IntWritable intWritable : values) {
				System.out.println("reduce:<" + key + ","+ intWritable + ">" );
				index  += intWritable.get();
			}
			context.write(key,new IntWritable(index));
		}
	}

Reducer任務繼承Reducer類,主要接收的資料來自Map任務的輸出,中間經過Shuffle磁區、排序、分組,最終以<key,value>形式輸出給用戶,

Job提交代碼:

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Job job = Job.getInstance();
		job.setJarByClass(WorldCount.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.setMapperClass(WorldCount_Mapper.class);
		job.setReducerClass(WorldCount_Reducer.class);
		FileInputFormat.addInputPath(job,new Path("hdfs://192.168.100.123:8020/input"));
		FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.100.123:8020/output"));
		job.waitForCompletion(true);
	}

JobClients是用戶提交的作業與ResourceManager互動的主要介面,JobClients提供提交作業、追蹤行程、訪問子任務的日志記錄、獲取的MapReduce集群狀態資訊等功能,

3、Shuffle程序詳解

Hadoop運行機制中,將map輸出進行磁區、分組、排序、和合并等處理后作為輸入傳給Reducer的程序,稱為shuffle程序,

shuffle階段又可以分為Map端的shuffle和Reduce端的shuffle,

  一、Map端的shuffle

  寫磁盤:Map端會處理輸入資料并產生中間結果,這個中間結果會寫到本地磁盤,而不是HDFS,每個Map的輸出會先寫到記憶體緩沖區中,當寫入的資料達到設定的閾值時,系統將會啟動一個執行緒將緩沖區的資料寫到磁盤,這個程序叫做spill,

  磁區、分組、排序:在spill寫入之前,會先進行二次排序,首先根據資料所屬的partition進行排序,然后每個磁區(partition)中的資料再按key來排序,partition的目是將記錄劃分到不同的Reducer上去,以期望能夠達到負載均衡,以后的Reducer就會根據partition來讀取自己對應的資料,接著運行combiner(如果設定了的話),combiner的本質也是一個Reducer,其目的是對將要寫入到磁盤上的檔案先進行一次處理,這樣,寫入到磁盤的資料量就會減少,最后將資料寫到本地磁盤產生spill檔案(spill檔案保存在{mapred.local.dir}指定的目錄中,Map任務結束后就會被洗掉),

檔案合并:最后,每個Map任務可能產生多個溢寫檔案(spill file),在每個Map任務完成前,會通過多路歸并演算法將這些spill檔案歸并成一個已經磁區和排序的輸出檔案,至此,Map的shuffle程序就結束了,

壓縮:在shuffle程序中如果壓縮被啟用,在map傳出資料傳入Reduce之前可執行壓縮,默認情況下壓縮是關閉的,可以將mapred.compress.map.output設定為true可實作壓縮,

  二、Reduce端的shuffle

  Reduce端的shuffle主要包括三個階段,copysort(merge)和reduce

  首先要將Map端產生的輸出檔案拷貝到Reduce端,但每個Reducer如何知道自己應該處理哪些資料呢?因為Map端進行partition的時候,實際上就相當于指定了每個Reducer要處理的資料(partition就對應了Reducer),所以Reducer在拷貝資料的時候只需拷貝與自己對應的partition中的資料即可,每個Reducer會處理一個或者多個partition,但需要先將自己對應的partition中的資料從每個Map的輸出結果中拷貝過來,

接下來就是排序(sort)階段,也成為合并(merge)階段,因為這個階段的主要作業是執行了歸并排序,從Map端拷貝到Reduce端的資料都是有序的,所以很適合歸并排序,最終在Reduce端生成一個較大的檔案作為Reduce的輸入,MapReduce編程介面

二、MapReduce編程基礎

1、Hadoop資料型別

Hadoop資料包括:BooleanWritable、ByteWritable、DoubleWritable、FloatWritale、IntWritable、LongWritable、Text、NullWritable等,它們實作了WritableComparable介面,其中Text表示使用UTF8格式存盤的文本、NullWritable型別是當(key,value)中的key或value為空時使用,

表2 Hadoop Writable與Java資料型別參照表

Java基本型別Writable封裝類型別序列化后的長度為
booleanBooleanWritable布爾型1
byteByteWritable位元組型1
doubleDoubleWritable雙精度浮點型8
floatFloatWritable單精度浮點型8
int

IntWritable


VIntWritable

整型

4


1-5

long

LongWritable

長整型8
shortShortWritable短整型2
nullNullWritable空值0
Text文本型別

除了上述Hadoop型別外,用戶還可以自定義新的資料型別,用戶自定義資料型別需要實作Writable介面,但如果需要作為主鍵key使用或需要比較大小時,則需要實作WritableComparable介面,

2、資料輸入格式InputFormat

抽象類InputFormat<K,V>有三個直接子類:

FileInputFormat<K,V>、DBInputFormat<T>、DelegatingInputFormat<K,V>

其中,檔案輸入格式類FileInputFormat<K,V>類有幾個子類:

TextInputFormat、KeyValueInputFormat、SequenceFileInputFormat<K,V>、NlineInputFormat、CombineFileInputFormat<K,V>

序列化檔案輸入類SequenceFileInputFormat<K,V>有幾個子類:

SequenceFileAsBinaryInputFormat、SequenceFileAsTextInputFormat、SequenceFileInputFilter<K,V>

資料庫輸入格式類DBInputFormat<T>的直接子類是:DataDriverDBInputFormat<T>,而這個子類又派生子類:OracleDataDriverDBInputFormat<T>

表3 常用資料輸入格式類

InputFormat類描述鍵(Key)值(Value)
TextInputFormat默認輸入格式,讀取文本檔案的行當前行的偏移量當前行內容
KeyValueTextInputFormat將行決議成鍵值對行內首個制表符的內容行內其余內容
SequenceFileInputFormat專用于高性能的二進制格式用戶定義用戶定義

3、輸入資料分塊InputSplit和資料記錄讀入RecordReader

編程時由用戶選擇的資料輸入格式InputFormat型別來自動決定資料分塊InputSplit和資料記錄RecordReader型別,一個InputSplit將單獨作為一個Mapper的輸入,即作業的Mapper數量是由InputSplit個數決定的,

表4 資料輸出格式類對應的Reader型別

InputFormat類RecordReader類描述
TextInputFormatLineRecordReader讀取文本檔案的行
KeyValueTextInputFormatKeyValueLineRecordReader讀取行并將行決議為鍵值對
SequenceFileInputFormatSequenceFileRecordReader用戶定義的格式產生鍵與值
DBInputFormatDBRecordReader僅適合讀取少量資料記錄,不適合資料倉庫聯機資料分析大量資料的讀取處理

4、資料輸出格式OutputFormat

抽象類OutputFormat<K,V>有四個直接子類:

FileOutputFormat<K,V>、DBOutputFormat<K,V>、NullOutputFormat<K,V>、FilterOutputFormat<K,V>

FileOutputFormat<K,V>有兩個直接子類:

TextOutputFormat<K,V>、SequenceFileOutputFormat<K,V>

SequenceFileOutputFormat<K,V>有直接子類:SequenceFileAsBinaryOutputFormat

FilterOutputFormat<K,V>有直接子類:LazyOutputFormat<K,V>

5、資料記錄輸出類RecordWriter

資料記錄輸出類RecordWriter是一個抽象類,

表5 資料輸出格式類對應的資料記錄Writer型別

OutputFormat類RecordWriter類描述
TextOutputFormatLineRecordWriter將結果資料以“key + \t + value”形式輸出到文本檔案中
SequenceFileOutputFormatSequenceFileRecordWriter用戶定義的格式產生鍵與值
DBOutputFormatDBRecordWriter將結果寫入到一個資料庫表中
FilterOutputFormatFilterRecordWriter對應于過濾器輸出模式的資料記錄模式,只將過濾器的結果輸出到檔案中

6、Mapper類

Mapper類是一個抽象類,位于hadoop-mapreduce-client-core-2.x.x.jar中,其完整類名是:org.apache.hadoop.mapreduce.Mapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT>,需派生子類使用,在子類中重寫map方法:map(KEYIN key,VALUEIN value,Mapper.Context context)對出入的資料分塊每個鍵值對呼叫一次,

7、Reduce類

Reduce類是一個抽象類,位于hadoop-mapreduce-client-core-2.x.x.jar中,其完整類名是:org.apache.hadoop.mapreduce.Reduce<KEYIN,VALUEIN,KEYOUT,VALUEOUT>,需派生子類使用,在子類中重寫reduce方法:reduce(KEYIN key,Inerable <VALUEIN> value,Reducer.Context context)對出入的資料分塊每個鍵值對呼叫一次,

三、MapReduce專案案例

1、經典案例——WordCount

代碼演示:

package hadoop.mapreduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;

public class MyWordCount {
	/*
	 * 	KEYIN:是map階段輸入的key(偏移量)
	 * 	VALUEIN:是map階段輸入的value(文本檔案的內容--行)
	 *  KEYOUT:是map階段輸出的key(單詞)
	 *  VALUEOUT:是map階段輸出的value(單詞的計數--1)
	 *  
	 *  Java基本資料型別:
	 *  	int、short、long、double、float、char、boolean、byte
	 *  hadoop資料型別
	 *  	IntWritable、ShortWritable、LongWritable、DoubleWritable、FloatWritable
	 *  	ByteWritable、BooleanWritable、NullWritable、Text
	 *  	Text:使用utf8編碼的文本型別
	 */
	public static class WordCount_Mapper extends Mapper<LongWritable, Text, Text, IntWritable>{
		@Override	//方法的重寫
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,
				Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			String[] line = value.toString().split(" ");	//將獲取到的資料以空格進行切分成一個個單詞
			for (String word : line) { 	//遍歷單詞的陣列
				context.write(new Text(word), new IntWritable(1));  //單詞進行計數,將中間結果寫入context
			}
		}												
	}
	
	/*
	 * KEYIN:reduce階段輸入的key(單詞)
	 * VALUEIN:reduce階段輸入的value(單詞的計數)
	 * KEYOUT:reduce階段輸出的key(單詞)
	 * VALUEOUT:reduce階段輸出的value(單詞計數的總和)
	 * 
	 * reduce方法中做以下修改:
	 * 	將Text arg0改為Text key
	 *  將Iterable<IntWritable> arg1改為Iterable<IntWritable> value
	 *  將Context arg2修改為Context context
	 */
	public static class WordCount_Reducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
						throws IOException, InterruptedException {
			int sum = 0;	//創建一個變數,和
			for (IntWritable intWritable : values) {		//遍歷相同key單詞的計數
				sum += intWritable.get();	//將相同key單詞的計數進行累加
			}
			context.write(key, new IntWritable(sum));	//將計算的結果寫入context
		}
	}

	//提交作業
	public static void main(String[] args) throws Exception {
		
		String inPath= "hdfs://192.168.182.10:8020/input.txt";
		String outPath = "hdfs://192.168.182.10:8020/output/";
		Configuration conf = new Configuration();
		Job job = Job.getInstance();	//創建Job物件job
		FileSystem fs = FileSystem.get(conf);
		if (fs.exists(new Path(outPath))) {
			fs.delete(new Path(outPath), true);
		}
		job.setJarByClass(MyWordCount.class); 	//設定運行的主類MyWordCount
		job.setMapperClass(WordCount_Mapper.class); 	//設定Mapper的主類
		job.setReducerClass(WordCount_Reducer.class); 	//設定Reduce的主類
		job.setOutputKeyClass(Text.class); 	//設定輸出key的型別
		job.setOutputValueClass(IntWritable.class); 	//設定輸出value的型別
		//設定檔案的輸入路徑(根據自己的IP和HDFS地址設定)
		FileInputFormat.addInputPath(job, new Path(inPath));	
		//設定計算結果的輸出路徑(根據自己的IP和HDFS地址設定)
		FileOutputFormat.setOutputPath(job, new Path(outPath));
		System.exit((job.waitForCompletion(true)?0:1)); 	//提交任務并等待任務完成
	}
}

打包上傳虛擬機:

步驟:

右鍵單擊專案名 --> 選擇 Export --> Java --> JAR file --> Browse...選擇存放路徑 --> 檔案名

命名為wordcount.jar,將打包好的jar包上傳到虛擬機中

運行代碼:

在本地創建一個檔案input.txt

vi input.txt

添加內容:

hello world
hello hadoop
bye world
bye hadoop

上傳到DHFS中:

hadoop fs -put input.txt /

使用jar命令執行專案:

hadoop jar wordcount.jar hadoop.mapreduce.MyWordCount

如下圖:

查看結果:

2、計算考試平均成績

代碼演示:

Mapper類

package hadoop.mapreduce;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

/*
 * 撰寫CourseScoreAverageMapper繼承Mapper類
 */
public class CourseScoreAverageMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	@Override	//方法的重寫
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,
			Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		String line = new String(value.getBytes(),0,value.getLength(),"UTF8");	//轉換中文編碼
		Counter countPrint =  context.getCounter("CourseScoreAverageMapper.Map 輸出傳遞Value:", line);	//通過計數器輸出變數值
		countPrint.increment(1L);	//將計數器加一
		StringTokenizer tokenArticle = new StringTokenizer(line,"\n");	//將輸入的資料按行“\n”進行分割
		while(tokenArticle.hasMoreElements()) {
			StringTokenizer tokenLine = new StringTokenizer(tokenArticle.nextToken());	//每行按空格劃分
			String strName = tokenLine.nextToken();		//按空格劃分出學生姓名
			String strScore = tokenLine.nextToken();	//按空格劃分出學生成績
			Text name = new Text(strName);	//轉換為Text型別
			int scoreInt = Integer.parseInt(strScore);	//轉換為int型別
			context.write(name, new IntWritable(scoreInt));		//將中間結果寫入context
			countPrint = context.getCounter("CourseScoreAverageMapper.Map中回圈輸出資訊:", "<key,value>:<"+strName+","+strScore+">");	//輸出資訊
			countPrint.increment(1L);	//將計數器加一
		}
	}												
}

Reducer類

package hadoop.mapreduce;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Reducer;

/*
 * 撰寫CourseScoreAverageReducer繼承Reduce類
 */
public class CourseScoreAverageReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	@Override  //重寫reduce方法
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, IntWritable>.Context context)
					throws IOException, InterruptedException {
		int sum = 0;	//總分
		int count = 0;	//科目數
		for (IntWritable val : values) {		//遍歷相同key的分數
			sum += val.get();	//將相同key的分數進行累加
			count++;	//計算科目數
		}
		int average = (int)sum/count;	//計算平均分
		context.write(key, new IntWritable(average));	//將計算的結果寫入context
		Counter countPrint = context.getCounter("CourseScoreAverageReducer.Reducer中輸出資訊:", "<key,value>:<"+key.toString()+","+average+">");	//輸出資訊
		countPrint.increment(1L);	//計數器加1
	}
}

Driver類

package hadoop.mapreduce;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class CourseScoreDriver {

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();	//獲取組態檔
		Job job = Job.getInstance(conf,"CourseScoreAverage");	//創建Job物件job
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();	//獲取命令列引數
		if(otherArgs.length<2) {	
			System.err.print("Usage:hadoop jar MyAverage.jar <in> <out> ");
			System.err.print("hadoop jar MyAverage.jar hadoop.mapreduce.CourseScoreDriver <in> <out>");
			System.exit(2);
		}else {
			for (int i = 0; i < otherArgs.length-1; i++) {	//設定檔案輸入路徑
				if(!("hadoop.mapreduce.CourseScoreDriver".equalsIgnoreCase(otherArgs[i]))) {  //排除hadoop.mapreduce.CourseScoreDriver這個引數
					FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
					System.out.println("引數IN:"+otherArgs[i]);
				}
			}
			//設定檔案輸出路徑
			FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1]));  //設定輸出路徑
			System.out.println("引數OUT:"+otherArgs[otherArgs.length-1]);
		}
		FileSystem hdfs = FileSystem.get(conf);	//創建檔案系統
		if(hdfs.exists(new Path(otherArgs[otherArgs.length-1]))) {	//如果已經存在該路徑,則洗掉該路徑
			hdfs.delete(new Path(otherArgs[otherArgs.length-1]), true);
		}
		job.setJarByClass(CourseScoreDriver.class); 	//設定運行的主類CourseScoreDriver
		job.setMapperClass(CourseScoreAverageMapper.class); 	//設定Mapper的主類
		job.setCombinerClass(CourseScoreAverageReducer.class); 	//設定Combiner的主類
		job.setReducerClass(CourseScoreAverageReducer.class); 	//設定Reduce的主類
		job.setOutputKeyClass(Text.class); 	//設定輸出key的型別
		job.setOutputValueClass(IntWritable.class); 	//設定輸出value的型別
		job.setInputFormatClass(TextInputFormat.class);		//設定輸入格式
		job.setOutputFormatClass(TextOutputFormat.class);	//設定輸出格式
		System.exit((job.waitForCompletion(true)?0:1)); 	//提交任務并等待任務完成
		System.out.println("Job Finished!");
	}
}

打包上傳虛擬機:

步驟:

右鍵單擊專案名 --> 選擇 Export --> Java --> JAR file --> Browse...選擇存放路徑 --> 檔案名

命名為average.jar , 將打包好的average.jar上傳到虛擬機中

運行代碼:

首先準備三個檔案 Chinese.txt、Math.txt、English.txt,添加如下內容:

將檔案上傳到HDFS的data目錄下:

hadoop fs -mkdir /data
hadoop fs -put Chinese.txt /data/
hadoop fs -put Math.txt /data/
hadoop fs -put English.txt /data/

執行代碼:

hadoop jar average.jar hadoop.mapreduce.CourseScoreDriver /data /data/output

查看結果,如下圖:

3、網站日志分析

代碼演示:

打包上傳虛擬機:

運行代碼:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/356065.html

標籤:其他

上一篇:Hadoop集群的搭建(三)——JDK和Hadoop的安裝和環境配置

下一篇:【大資料筆記】- Hadoop HDFS API

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more