日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

以实例让你真正明白mapreduce---填空式、分布(分割)编程

發布時間:2023/12/2 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以实例让你真正明白mapreduce---填空式、分布(分割)编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題導讀:

1.如何在講mapreduce函數中的字符串等信息,輸出到eclipse控制臺?
2.除了使用下文方法,還有其它方法輸出到控制臺?
3.map中,系統默認接受的value值是什么?
4.reduce輸出不是自己想要的結果,可能的原因是什么?


附件不能下載,可查看原文鏈接:
http://www.aboutyun.com/thread-8303-1-1.html

mapreduce不是很好理解,為什么?
因為我們傳統編程,運行程序,都在本地,怎么會跑到別的客戶端或則服務器那,總之運行程序就是一太電腦。mapreduce牛啊,他竟然可以讓一個程序多臺電腦一塊跑,這也是它的神奇不同之處,同時也讓mapreduce蒙上了一層神秘的面紗。

這里我們就來揭開這個面紗。

這里難以理解的地方是什么?它是如何分割的,如何分組、如何分區的,什么shuffer,等等各種概念涌入初學者腦海中,然后就是云里霧里、似看清、又看不清。





這里我們拋棄這些所有的概念,讓我們來一個短平快、更直接、更簡單的的認識。

記得我們在上學的時候,有一種題型是填空題,而mapreduce就是一個填空式編程。

為什么被認為是填空式編程,因為mapreduce是一個框架,我們所作的就是編寫map函數、reduce函數、然后驅動函數main()。

填空,讓我們填寫的就是map、reduce函數。剩下的則是由整個mapreduce框架來完成。


首先從map函數入手

// map類
static class MyMapper extends
Mapper<LongWritable, Text, Text, LongWritable> {
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {


final String[] splited =?value.toString().split(" ");
org.apache.hadoop.mapreduce.Counter count= context.getCounter("map中的值value", value.toString());
count.increment(1l);

for (String word : splited) {
context.write(new Text(word), new LongWritable(1L));
//org.apache.hadoop.mapreduce.Counter count= context.getCounter("map中的值value", new Text(word).toString()+"個數"+new LongWritable(1L).toString());
//count.increment(1l);
}

}

}
我們知道map接受了數據,那么這個數據是是怎么個數據??

假如我們有下面數據
hello?www.aboutyun.com?hello word
hello hadoop
hello mapreduce
我們map函數如下:
map(LongWritable key, Text value, Context context)
上面有三個參數,其中key是偏移量,這里不是我們的重點,對于Context不了解,可以查看hadoop開發必讀:認識Context類的作用.
我們這里重點講value,這個value到底是什么?

hello?www.aboutyun.com?hello word
還是
hello
還是


hello?www.aboutyun.com
我們在做填空題,框架之外的我們還沒有看到,所以我需要明白value到底是什么?
下面我們開始運行程序





運行程序,這里讓我們犯愁了,為什么,因為在運行這個程序之前,你有環境了嗎?沒有, 一、搭建環境 參考新手指導:Windows上使用Eclipse遠程連接Hadoop進行程序開發,首先搭建環境,這里還用到了eclipse插件, 二、插件下載 hadoop-eclipse-plugin-2.2.0.jar 鏈接:?http://pan.baidu.com/s/1sjQ6Nnv密碼: uvwx
更多插件:hadoop家族、strom、spark、Linux、flume等jar包、安裝包匯總下載(持續更新)

三、遇到問題 環境搭建好了,我們開發運行程序了,遇到各種問題該如何解決,可參考 Win7 Eclipse調試Centos Hadoop2.2-Mapreduce出現問題解決方案
在window中,我們遇到最多的問題就是缺少 1.winutils.exe 2.hadoop.dll ?hadoop-common-2.2.0-bin-master.zip?(273.06 KB, 下載次數: 0, 售價: 2 云幣)?

上面下載附件,上面沒有必要都放到hadoop_home/bin下面,缺什么我們放到里面就ok了。我們的路徑是
  • D:\hadoop2\hadoop-2.2.0\bin
  • 復制代碼




    環境有了,我們需要準備數據以及mapreduce程序

    一、準備數據
    首先第一步我們上傳待分析文件:

    ?

    第二步:找到文件?


    第三步:上傳成功

    ?


    二、mapreduce函數分析


    map函數:
    static class MyMapper extends
    ? ?? ?? ?? ?? ?? ?? ?? ?Mapper<LongWritable, Text, Text, LongWritable> {
    ? ?? ?? ?? ?? ? protected void map(LongWritable key, Text value, Context context)
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???throws IOException, InterruptedException {
    ? ?? ?? ?? ?? ?? ?? ?? ?
    ? ?? ?? ?? ?? ?? ?? ?? ?
    ? ?? ?? ?? ?? ?? ?? ?? ?final String[] splited = value.toString().split(" ");
    ? ?? ?? ?? ?? ?? ?? ?? ?org.apache.hadoop.mapreduce.Counter count= context.getCounter("map中的值value",??value.toString());
    ? ?? ?? ?? ?? ?? ?? ?? ?count.increment(1l);

    ? ?? ?? ?? ?? ??
    ? ?? ?? ?? ?? ?? ?? ?? ?for (String word : splited) {
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???context.write(new Text(word), new LongWritable(1L));

    ? ?? ?? ?? ?? ?? ?? ?? ?}

    ? ?? ?? ?? ?? ? }
    我們看到上面紅字部分他的作用是什么,這也正是很多犯愁的地方,因為我們想把我們想看到的數據輸出到eclipse的控制臺,可惜的是 System.out.println并不如我們愿,所以我們可以使用 getCounter輸出我們想看到的內容:
  • org.apache.hadoop.mapreduce.Counter count= context.getCounter("map中的值value", value.toString());
  • count.increment(1l);
  • 復制代碼 這里我們主要驗證:value值傳遞過來到底是什么?
    運行之后下面結果
    結果分析:


  • map中的值value
  • ? ?? ?? ?? ?? ? hello hadoop=1
  • ? ?? ?? ?? ?? ? hello mapreduce=1
  • ? ?? ?? ?? ?? ? hello www.aboutyun.com hello word=1
  • 復制代碼 ?


    上面我們看到輸出數據輸出了3次,也就是說,我們的map執行了3次,那么我們的原始數據是什么情況,看下圖:



    結論:
    從這里我們看到有多少行就有多少個map,也就是說,系統默認一行調用一個map函數,value值為一行的數據



    同理reduce也是如此:


    // reduce類
    ? ?? ???static class MyReduce extends
    ? ?? ?? ?? ?? ?? ?? ?? ?Reducer<Text, LongWritable, Text, LongWritable> {
    ? ?? ?? ?? ?? ? @Override
    ? ?? ?? ?? ?? ? protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s,
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???Context ctx) throws java.io.IOException, InterruptedException {
    ? ?? ?? ?? ?? ?? ?? ?? ?
    ? ?? ?? ?? ?? ?? ?? ?? ?long times = 0L;
    ? ?? ?? ?? ?? ?? ?? ?? ?for (LongWritable count : v2s) {
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???times += count.get();
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???
    ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???
    ? ?? ?? ?? ?? ?? ?? ?? ?}
    ? ?? ?? ?? ?? ?? ?? ?? ?org.apache.hadoop.mapreduce.Counter count1= ctx.getCounter("reduce中的值"+k2.toString(), new LongWritable(times).toString());
    ? ?? ?? ?? ?? ?? ?? ?? ?count1.increment(1l);

    ? ?? ?? ?? ?? ?? ?? ?? ?ctx.write(k2, new LongWritable(times));

    ? ?? ?? ?? ?? ? }

    ? ?? ?? ?? ?? ??
    ? ?? ???}
    這里我們主要驗證:reduce中key出現的次數:

    ?

    從上面結果我們看到
    hadoop:? ?? ?? ?? ?? ?? ?? ? 1個
    hello :? ?? ?? ?? ?? ?? ?? ?? ?? ?4個
    mapreduce:? ?? ?? ?? ?? ?1個
    www.aboutyun.com:1個

    這里我們并沒有通過mapreduce的輸出文件來查看,而是通過getCounter來實現的。
    我們來看看reduce的輸出文結果:
    ?



    這里在做一個有趣的實驗:
    為什么那,因為很多初學者,可能會遇到一個問題,就是reduce的輸出結果不正確,為什么會不正確,下面我們對reduce稍微做一些改動:

    static class MyReduce extends
    Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s,
    Context ctx) throws java.io.IOException, InterruptedException {

    long times = 0L;
    for (LongWritable count : v2s) {
    times += count.get();
    org.apache.hadoop.mapreduce.Counter count1= ctx.getCounter("reduce中的值"+k2.toString(), new LongWritable(times).toString());
    count1.increment(1l);
    ctx.write(k2, new LongWritable(times));
    }



    }


    }
    我們查看下面結果:


    ?


    我們來看看reduce的輸出文結果:

    ?


    仔細對比我們把


    org.apache.hadoop.mapreduce.Counter count1= ctx.getCounter("reduce中的值"+k2.toString(), new LongWritable(times).toString());
    count1.increment(1l);
    ctx.write(k2, new LongWritable(times));
    一個放在循環內,一個放在了循環外,所以產生了下面的結果。這是很多初學者,在學習之初可能會碰到的問題

    總結

    以上是生活随笔為你收集整理的以实例让你真正明白mapreduce---填空式、分布(分割)编程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。