Hadoop 叢集運行測試代碼(Hadoop 權威指南天氣資料樣本)

來源:互聯網
上載者:User

今天將Hadoop 權威指南天氣資料範例程式碼在hadoop叢集上跑通,記錄一下。

之前在百度/Google上怎麼也沒有找到怎麼樣將自己的Map-Reduce方法跑在叢集上的每一步都具體描述,經過一番痛苦的無頭蒼蠅式的摸索,成功了,心情不錯...

 

1準備天氣預報資料(權威指南上的資料的簡化版 5-9為year,15-19為temperature)

aaaaa1990aaaaaa0039a
bbbbb1991bbbbbb0040a
ccccc1992cccccc0040c
ddddd1993dddddd0043d
eeeee1994eeeeee0041e
aaaaa1990aaaaaa0031a
bbbbb1991bbbbbb0020a
ccccc1992cccccc0030c
ddddd1993dddddd0033d
eeeee1994eeeeee0031e
aaaaa1990aaaaaa0041a
bbbbb1991bbbbbb0040a
ccccc1992cccccc0040c
ddddd1993dddddd0043d
eeeee1994eeeeee0041e
aaaaa1990aaaaaa0044a
bbbbb1991bbbbbb0045a
ccccc1992cccccc0041c
ddddd1993dddddd0023d
eeeee1994eeeeee0041e

2 編寫Map-Reduce函數和調度函數(Job)

簡單點:如下

package hadoop.test;

import java.io.第;

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 MaxTemperature {

    static class MaxTemperatureMapper extends Mapper<LongWritable , Text , Text, IntWritable>
    {
        private static final int MISSING = 9999;
       
        public void map(LongWritable key, Text value, Context conext) throws IOException, InterruptedException
        {
            String line = value.toString();
            String year = line.substring(5, 9);  //自己準備的資料,是天氣預報資料的簡化版
            int airTemperature = Integer.parseInt(line.substring(15, 19)); //自己準備的資料,是天氣預報資料的簡化版
           
            if(airTemperature != MISSING)
            {
                conext.write(new Text(year), new IntWritable(airTemperature));
            }
           
        }
       
    }
   
    static class MaxTemperatureReducer extends Reducer<Text,IntWritable,Text,IntWritable>
    {
        public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException
        {
            int maxValue = Integer.MIN_VALUE;
            for(IntWritable value : values)
            {
                maxValue = Math.max(maxValue, value.get());
            }
            context.write(key, new IntWritable(maxValue));
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        if(args.length != 2)
        {
            System.err.println("Usage: MaxTemperature <input path> <output path>");
            System.exit(-1);
        }
         
        try {
            Job job = new Job();
            job.setJarByClass(MaxTemperature.class);
           
            FileInputFormat.addInputPath(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
           
            job.setMapperClass(MaxTemperatureMapper.class);
            job.setReducerClass(MaxTemperatureReducer.class);
           
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
           
            System.exit(job.waitForCompletion(true) ? 0 : 1);
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }
       

    }

}

 

3 將第二步編寫的代碼打包成HadoopTest.jar放到本地某一個目錄下,例如/home/hadoop/Documents/

   然後export HADOOP_CLASSPATH=/home/hadoop/Documents/

(打包的時候要選擇mainclass,不選擇好像執行的時候有錯誤,eclipse的export選項中有MainClass選項

否則:運行hadoop jar 命令時在***.jar後面需要指定包括包路徑的mainclass類名

例如 hadoop jar /home/hadoop/Documents/HadoopTest.jar hadoop.test.MaxTemperature /user/hadoop/temperature output

)

 

4將要分析的資料傳到hdfs上去

    hadoop dfs -put /home/hadoop/Documents/temperature ./temperature

 

5 開始執行

  hadoop jar /home/hadoop/Documents/HadoopTest.jar /user/hadoop/temperature output

 

 跟書上的命令不大一樣,不過他那裡是指的local的方式,另外不知道export HADOOP_CLASSPATH=/home/hadoop/Documents/有什麼用,執行hadoop jar HadoopTest.jar /user/hadoop/temperature output是不行滴,具體為什麼,繼續探究吧,先這樣了。

 這裡HadoopTest.jar在本地,要分析的資料檔案temperature 在hdfs上,產生的輸出在hdfs上,output是一個檔案夾

 

hadoop@hadoop1:~$ hadoop dfs -cat ./output/part-r-00000
1990    44
1991    45
1992    41
1993    43
1994    41

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.