Hadoop界的Hello World!

Hadoop界的Hello World!

                                                                                     2019-05-20  19:50:09java

 

應用平臺:Eclipse+ubantu+hadoop包linux

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

 

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

 

Hello MrZhangxd Hello Yootkapp

Hello Bye Bye Byeide

Hello MrZhangxdoop

 

預期結果:大數據

 

Bye 3編碼

Hello 4spa

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;

相關文章
相關標籤/搜索