1. 程式人生 > >執行一個mapreduce例項

執行一個mapreduce例項

本文改編自開啟
因為參考文中步驟有部分執行不正確,所以自己記錄下自己的步驟,並將原因整理了下。

Score.java檔案

下載

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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.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 Score { public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { // 實現map函式 public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 將輸入的純文字檔案的資料轉化成String String line = value.toString
(); // 將輸入的資料首先按行進行分割 StringTokenizer tokenizerArticle = new StringTokenizer(line, "\n"); // 分別對每一行進行處理 while (tokenizerArticle.hasMoreElements()) { // 每行按空格劃分 StringTokenizer tokenizerLine = new StringTokenizer(tokenizerArticle.nextToken()); String strName = tokenizerLine.nextToken();// 學生姓名部分 String strScore = tokenizerLine.nextToken();// 成績部分 Text name = new Text(strName); int scoreInt = Integer.parseInt(strScore); // 輸出姓名和成績 context.write(name, new IntWritable(scoreInt)); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { // 實現reduce函式 public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; int count = 0; Iterator<IntWritable> iterator = values.iterator(); while (iterator.hasNext()) { sum += iterator.next().get();// 計算總分 count++;// 統計總的科目數 } int average = (int) sum / count;// 計算平均成績 context.write(key, new IntWritable(average)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); // "localhost:9000" 需要根據實際情況設定一下 conf.set("mapred.job.tracker", "localhost:9000"); // 一個hdfs檔案系統中的 輸入目錄 及 輸出目錄 String[] ioArgs = new String[] { "input/score", "output" }; String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: Score Average <in> <out>"); System.exit(2); } Job job = new Job(conf, "Score Average"); job.setJarByClass(Score.class); // 設定Map、Combine和Reduce處理類 job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); // 設定輸出型別 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 將輸入的資料集分割成小資料塊splites,提供一個RecordReder的實現 job.setInputFormatClass(TextInputFormat.class); // 提供一個RecordWriter的實現,負責資料輸出 job.setOutputFormatClass(TextOutputFormat.class); // 設定輸入和輸出目錄 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

編譯Score.java

javac Score.java

如果出現錯誤


vim /etc/profile
----------------

新增如下內容

#set hadoop environment
export HADOOP_HOME=/usr/local/hadoop
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
export CLASSPATH=$HADOOP_HOME/share/hadoop/common/hadoop-common-2.6.2.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.2.jar:$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar

注意:這裡的環境變數中的classpath就是和原文的不同之處,也應該就是這個原因造成編譯不通過

重新執行

javac Score.java

會生成三個class檔案

ls | grep class

打成jar包

jar -cvf Score.jar ./Score*.class

這裡也和原文不同,不是用tar命令打包,而應該是jar命令

新建路徑/input/score

hadoop fs -mkdir  -p /user/root/input/scores

上傳到Hadoop的HDFS

hadoop fs -put ./*.txt /input/score

檢視上傳結果

hadoop fs -ls -R /input/score

執行

hadoop jar Score.jar Score /input/score /output

格式
hadoop jar jar包所在目錄 類名稱 HDFS中需要處理資料路徑 HDFS中存放資料路徑

1、路徑不對,注意當前路徑和jar包所在的路徑不一致
2、jar包有問題(一開始用的tar打包,所以出錯尷尬)
3、jar包檔名錯了

輸出結果

hdfs dfs -ls output 或者 hadoop fs –ls –R /

檢視結果

hdfs dfs -cat output/part-r-00000  或者 hadoop fs  -cat output/part-r-00000