程式使用的測驗文本資料:
Dear River
Dear River Bear Spark
Car Dear Car Bear Car
Dear Car River Car
Spark Spark Dear Spark
1撰寫主要類
(1)Maper類
首先是自定義的Maper類代碼
public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//fields:代表著文本一行的的資料: dear bear river
String[] words = value.toString().split("\t");
for (String word : words) {
// 每個單詞出現1次,作為中間結果輸出
context.write(new Text(word), new IntWritable(1));
}
}
}
?????這個Map類是一個泛型型別,它有四個形參型別,分別指定map()函式的輸入鍵、輸入值、輸出鍵和輸出值的型別,LongWritable:輸入鍵型別,Text:輸入值型別,Text:輸出鍵型別,IntWritable:輸出值型別.
?????String[] words = value.toString().split("\t");,words 的值為Dear River Bear River
?????輸入鍵key是一個長整數偏移量,用來尋找第一行的資料和下一行的資料,輸入值是一行文本Dear River Bear River,輸出鍵是單詞Bear ,輸出值是整數1,
?????Hadoop本身提供了一套可優化網路序列化傳輸的基本型別,而不直接使用Java內嵌的型別,這些型別都在org.apache.hadoop.io包中,這里使用LongWritable型別(相當于Java的Long型別)、Text型別(相當于Java中的String型別)和IntWritable型別(相當于Java的Integer型別),
?????map()方法的引數是輸入鍵和輸入值,以本程式為例,輸入鍵LongWritable key是一個偏移量,輸入值Text value是Dear Car Bear Car ,我們首先將包含有一行輸入的Text值轉換成Java的String型別,之后使用substring()方法提取我們感興趣的列,map()方法還提供了Context實體用于輸出內容的寫入,
(2)Reducer類
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
/*
(River, 1)
(River, 1)
(River, 1)
(Spark , 1)
(Spark , 1)
(Spark , 1)
(Spark , 1)
key: River
value: List(1, 1, 1)
key: Spark
value: List(1, 1, 1,1)
*/
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable count : values) {
sum += count.get();
}
context.write(key, new IntWritable(sum));// 輸出最終結果
};
}
Reduce任務最初按照磁區號從Map端抓取資料為:
(River, 1)
(River, 1)
(River, 1)
(spark, 1)
(Spark , 1)
(Spark , 1)
(Spark , 1)
經過處理后得到的結果為:
key: hello value: List(1, 1, 1)
key: spark value: List(1, 1, 1,1)
所以reduce()函式的形參 Iterable<IntWritable> values 接收到的值為List(1, 1, 1)和List(1, 1, 1,1)
(3)Main函式
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;
import java.io.IOException;
public class WordCountMain {
//若在IDEA中本地執行MR程式,需要將mapred-site.xml中的mapreduce.framework.name值修改成local
public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
if (args.length != 2 || args == null) {
System.out.println("please input Path!");
System.exit(0);
}
//System.setProperty("HADOOP_USER_NAME","hadoop2.7");
Configuration configuration = new Configuration();
//configuration.set("mapreduce.job.jar","/home/bruce/project/kkbhdp01/target/com.kaikeba.hadoop-1.0-SNAPSHOT.jar");
//呼叫getInstance方法,生成job實體
Job job = Job.getInstance(configuration, WordCountMain.class.getSimpleName());
// 打jar包
job.setJarByClass(WordCountMain.class);
// 通過job設定輸入/輸出格式
// MR的默認輸入格式是TextInputFormat,所以下兩行可以注釋掉
// job.setInputFormatClass(TextInputFormat.class);
// job.setOutputFormatClass(TextOutputFormat.class);
// 設定輸入/輸出路徑
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 設定處理Map/Reduce階段的類
job.setMapperClass(WordCountMap.class);
//map combine減少網路傳出量
job.setCombinerClass(WordCountReduce.class);
job.setReducerClass(WordCountReduce.class);
//如果map、reduce的輸出的kv對型別一致,直接設定reduce的輸出的kv對就行;如果不一樣,需要分別設定map, reduce的 輸出的kv型別
//job.setMapOutputKeyClass(.class)
// job.setMapOutputKeyClass(Text.class);
// job.setMapOutputValueClass(IntWritable.class);
// 設定reduce task最終輸出key/value的型別
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 提交作業
job.waitForCompletion(true);
}
}
2本地運行
首先更改mapred-site.xml檔案配置
將mapreduce.framework.name的值設定為local

然后本地運行:

查看結果:

3集群運行
方式一:
首先打包

更改組態檔,改成yarn模式

添加本地jar包位置:
Configuration configuration = new Configuration();
configuration.set("mapreduce.job.jar","C:\\Users\\tanglei1\\IdeaProjects\\Hadooptang\\target");

設定允許跨平臺遠程呼叫:
configuration.set("mapreduce.app-submission.cross-platform","true");

修改輸入引數:

運行結果:

方式二:
將maven專案打包,在服務器端用命令運行mr程式
hadoop jar com.kaikeba.hadoop-1.0-SNAPSHOT.jar
com.kaikeba.hadoop.wordcount.WordCountMain /tttt.txt /wordcount11
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/39981.html
標籤:大數據
下一篇:svaeas問題
