Hadoop界的Hello World!

Hadoop界的Hello World!

                                                                                     2019-05-20  19:50:09

 

應用平臺:Eclipse+ubantu+hadoop包

注:例分析的形式給寶寶們解釋一下,詳細運行過程省略。

 

實例:定義一個進行統計的原始文件

 

Hello MrZhangxd Hello Yootk

Hello Bye Bye Bye

Hello MrZhangxd

 

預期結果:

 

Bye 3

Hello 4

MrZhangxd 2

Yootk 1

 

主要實現利用MapReduce,那麼什麼是MapReduce?

 

MapReduce是一種可用於數據處理的編程模型。MapReduce程序本質是並行運行的。

 

第一步 使用可視化表格進行分析

 

對於MapReduce而言有兩個階段

    Map階段:對數據的處理階段

    Reduce階段:對處理後的數據進行計算

以上實例如果使用MapReduce處理的話其流程如下:

 

Map處理

排序處理

合併處理

Reduce處理

<Hello,1>

<MrZhangxd,1>

<Hello,1>

<Yootk,1>

<Hello,1>

<Bye,1>
<Bye,1>

<Bye,1>

<Hello,1>

<MrZhangxd,1>

<Bye,1>

<Bye,1>

<Bye,1>

<Hello,1>

<Hello,1>

<Hello,1>

<Hello,1>

<MrZhangxd,1>

<MrZhangxd,1>

<Yootk,1>

 

 

 

<Bye,1,1,1>

<Hello,1,1,1,1>

<MrZhangxd,1,1>

<Yootk,1>

 

 

 

 

 

 

<Bye,3>

<Hello,4>

<MrZhangxd,2>

<Yootk,1>

 

 

 

以上整個操作稱作一個完整的作業“Job”

 

第二步 代碼編寫(代碼格式主要參考《代碼整潔之道》的編碼格式)

 

實現單詞統計的代碼:

 

package org.mrzhangxd.com.linux;

import java.io.IOException;

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.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

/**

 * 本操作主要是進行Map的數據處理

 * @author MrZhangxd

 * 在Mapper父類裏面接受的內容如下:

 * Object:輸入數據的具體內容

 * Text:每行的文本數據

 * Text:每個單詞分解後的統計結果

 * IntWritable:輸出記錄的結果

 */

public class WordCount {//本處要求實現單詞統計的處理操作

     //在整個代碼中最爲關鍵部分就是Map和Reduce部分,而且這兩個部分是需要用戶自己了實現的

     private static class WordCountReducer 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 {

                   // TODO Auto-generated method stub

                   super.reduce(key, values, context);

                   int sum = 0;//保存每個單詞出現的數據

                   for(IntWritable count : values) {

                       sum += count.get();

                   }

                   context.write(key, new IntWritable(sum));

              }   

         }

     @SuppressWarnings("unused")

     private static class WordCountMapper extends Mapper<Object,Text,Text,IntWritable>{   

         @Override

         protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)

                   throws IOException, InterruptedException {

                   // TODO Auto-generated method stub

                   super.map(key, value, context);

                   //默認情況下是提取每一行數據,所以每行數據裏面都會存在空格,那麼要按照空格進行分割,每當出現一個單詞就需要做一個統計的1

                   String lineContent = value.toString();    //取出每行的數據

                   String result [] = lineContent.split(" ");//按空格進行數據拆分

                   for(int x = 0;x < result.length;x++) {    //循環每行每個單詞而後進行數據的生成

                       //每一個單詞最終生成的保存個數是1

                       context.write(new Text(result[x]), new IntWritable(1)); 

                   }

         }

     }

     public static void main(String[] args) throws IOException {

         // TODO Auto-generated method stub

         if(args.length != 2) {

              System.out.println("本程序需要兩個參數,執行,hadoop yootk.jar /input/info.txt /output");

              System.exit(1);   

         }

         //每一次的執行實際上都屬於一個作業(Job),但是現在希望可以通過初始化參數來設置HDFS的文件存儲路徑

         //假設現在的文件保存在HDFS上的“input/info.txt”上,而且最終輸出結果也將保存在HDFS的“output”目錄中

         Configuration conf = new Configuration();

         //考慮到最終要使用HDFS進行內容的處理操作,並且輸入的時候不帶有HDFS地址

         String[] argArray = new GenericOptionsParser(conf, args).getRemainingArgs();//對輸入的參數進行處理

         //後面就需要作業進行處理了,而且Map與Reduces操作必須通過作業來配置

         Job job = Job.getInstance(conf,"hadoop");//定義一個hadoop作業

         job.setMapperClass(WordCountMapper.class);//設置執行的jar文件的程序類

         job.setJarByClass(WordCount.class);    //指定Mapper的處理類

         job.setMapOutputKeyClass(Text.class); //設置輸出key的類型

         job.setMapOutputValueClass(IntWritable.class);//設置輸出的value類型

         job.setReducerClass(WordCountReducer.class);//設置reduce操作的處理類

         //設置Map-Reduce最終的執行結果

         job.setOutputKeyClass(Text.class);//信息設置爲文本

         job.setOutputValueClass(IntWritable.class);//最終將內容設置爲一個數值

         //設置輸入以及輸出路徑

         //FileInputFormat.addInputPath(job, new Path(argArray[0]));

         FileInputFormat.addInputPath(job, new Path(argArray[0]));

         FileOutputFormat.setOutputPath(job,new Path(argArray[1]));

         //等待執行完畢

         try {

              System.exit(job.waitForCompletion(true) ? 0 : 1);

         } catch (ClassNotFoundException | InterruptedException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

         }//執行完畢並且退出

     }

 }

 

注:代碼的編寫是需要使用到Hadoop中提供的*.jar文件的。

 

C:\Users\ XXX \Desktop\大數據\hadoop-3.2.0\share\hadoop

需要配置如下幾個路勁的開發包:

    |--Common組件包:

    |   |--C:\Users\XXX\Desktop\大數據\hadoop-3.2.0\share\hadoop\common

    |   |--C:\Users\XXX\Desktop\大數據\hadoop-3.2.0\share\hadoop\common\lib;

    |--Mapreduce組件包:

    |   |--C:\Users\XXX\Desktop\大數據\hadoop-3.2.0\share\hadoop\mapreduce

    |   |--C:\Users\XXX\Desktop\大數據\hadoop-3.2.0\share\hadoop\mapreduce\lib;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章