溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

5、Window端實現Mapreduce程序完成wordco

發布時間:2020-07-25 21:55:54 來源:網絡 閱讀:2565 作者:victor19901114 欄目:大數據

程序使用的測試文本數據

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 valueDear 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&lt;IntWritable&gt; 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
5、Window端實現Mapreduce程序完成wordco
然后本地運行:
5、Window端實現Mapreduce程序完成wordco
查看結果:
5、Window端實現Mapreduce程序完成wordco

3集群運行

方式一:

首先打包
5、Window端實現Mapreduce程序完成wordco
更改配置文件,改成yarn模式
5、Window端實現Mapreduce程序完成wordco
添加本地jar包位置:

 Configuration configuration = new Configuration();
 configuration.set("mapreduce.job.jar","C:\\Users\\tanglei1\\IdeaProjects\\Hadooptang\\target");

5、Window端實現Mapreduce程序完成wordco
設置允許跨平臺遠程調用:

configuration.set("mapreduce.app-submission.cross-platform","true");

5、Window端實現Mapreduce程序完成wordco
修改輸入參數:
5、Window端實現Mapreduce程序完成wordco
運行結果:
5、Window端實現Mapreduce程序完成wordco

方式二:

將maven項目打包,在服務器端用命令運行mr程序

hadoop jar com.kaikeba.hadoop-1.0-SNAPSHOT.jar
com.kaikeba.hadoop.wordcount.WordCountMain /tttt.txt  /wordcount11
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女