5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco

程序使用的測試文本數(shù)據(jù)

坊子網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),坊子網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為坊子上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的坊子做網(wǎng)站的公司定做!

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:代表著文本一行的的數(shù)據(jù): dear bear river
        String[] words = value.toString().split("\t");
        for (String word : words) {
            // 每個(gè)單詞出現(xiàn)1次,作為中間結(jié)果輸出
            context.write(new Text(word), new IntWritable(1));
        }
    }
}

?????這個(gè)Map類是一個(gè)泛型類型,它有四個(gè)形參類型,分別指定map()函數(shù)的輸入鍵、輸入值、輸出鍵和輸出值的類型。LongWritable:輸入鍵類型,Text:輸入值類型,Text:輸出鍵類型,IntWritable:輸出值類型.
?????String[] words = value.toString().split("\t");,words 的值為Dear River Bear River
?????輸入鍵key是一個(gè)長整數(shù)偏移量,用來尋找第一行的數(shù)據(jù)和下一行的數(shù)據(jù),輸入值是一行文本Dear River Bear River,輸出鍵是單詞Bear ,輸出值是整數(shù)1
?????Hadoop本身提供了一套可優(yōu)化網(wǎng)絡(luò)序列化傳輸?shù)幕绢愋?,而不直接使用Java內(nèi)嵌的類型。這些類型都在org.apache.hadoop.io包中。這里使用LongWritable類型(相當(dāng)于Java的Long類型)、Text類型(相當(dāng)于Java中的String類型)和IntWritable類型(相當(dāng)于Java的Integer類型)。
?????map()方法的參數(shù)是輸入鍵和輸入值。以本程序?yàn)槔?,輸入鍵LongWritable key是一個(gè)偏移量,輸入值Text valueDear Car Bear Car ,我們首先將包含有一行輸入的Text值轉(zhuǎn)換成Java的String類型,之后使用substring()方法提取我們感興趣的列。map()方法還提供了Context實(shí)例用于輸出內(nèi)容的寫入。

(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));// 輸出最終結(jié)果
    };
}

Reduce任務(wù)最初按照分區(qū)號(hào)從Map端抓取數(shù)據(jù)為:
(River, 1)
(River, 1)
(River, 1)
(spark, 1)
(Spark , 1)
(Spark , 1)
(Spark , 1)
經(jīng)過處理后得到的結(jié)果為:
key: hello value: List(1, 1, 1)
key: spark value: List(1, 1, 1,1)
所以reduce()函數(shù)的形參 Iterable&lt;IntWritable&gt; values 接收到的值為List(1, 1, 1)List(1, 1, 1,1)

(3)Main函數(shù)

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中本地執(zhí)行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");
        //調(diào)用getInstance方法,生成job實(shí)例
        Job job = Job.getInstance(configuration, WordCountMain.class.getSimpleName());
        // 打jar包
        job.setJarByClass(WordCountMain.class);

        // 通過job設(shè)置輸入/輸出格式
        // MR的默認(rèn)輸入格式是TextInputFormat,所以下兩行可以注釋掉
        // job.setInputFormatClass(TextInputFormat.class);
        // job.setOutputFormatClass(TextOutputFormat.class);
        // 設(shè)置輸入/輸出路徑
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 設(shè)置處理Map/Reduce階段的類
        job.setMapperClass(WordCountMap.class);
        //map combine減少網(wǎng)路傳出量
        job.setCombinerClass(WordCountReduce.class);
        job.setReducerClass(WordCountReduce.class);

        //如果map、reduce的輸出的kv對(duì)類型一致,直接設(shè)置reduce的輸出的kv對(duì)就行;如果不一樣,需要分別設(shè)置map, reduce的        輸出的kv類型
        //job.setMapOutputKeyClass(.class)
        // job.setMapOutputKeyClass(Text.class);
        // job.setMapOutputValueClass(IntWritable.class);

        // 設(shè)置reduce task最終輸出key/value的類型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 提交作業(yè)
        job.waitForCompletion(true);

    }
}

2本地運(yùn)行

首先更改mapred-site.xml文件配置
將mapreduce.framework.name的值設(shè)置為local
5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
然后本地運(yùn)行:
5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
查看結(jié)果:
5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco

3集群運(yùn)行

方式一:

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

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

5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
設(shè)置允許跨平臺(tái)遠(yuǎn)程調(diào)用:

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

5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
修改輸入?yún)?shù):
5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
運(yùn)行結(jié)果:
5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco

方式二:

將maven項(xiàng)目打包,在服務(wù)器端用命令運(yùn)行mr程序

hadoop jar com.kaikeba.hadoop-1.0-SNAPSHOT.jar
com.kaikeba.hadoop.wordcount.WordCountMain /tttt.txt  /wordcount11

網(wǎng)頁名稱:5、Window端實(shí)現(xiàn)Mapreduce程序完成wordco
轉(zhuǎn)載源于:http://bm7419.com/article48/ijphep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、商城網(wǎng)站做網(wǎng)站、域名注冊(cè)網(wǎng)站策劃、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管