久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何用mapreduce得到top最大的前n條記錄

共計(jì) 3442 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。

這篇文章主要介紹“如何用 mapreduce 得到 top 最大的前 n 條記錄”,在日常操作中,相信很多人在如何用 mapreduce 得到 top 最大的前 n 條記錄問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何用 mapreduce 得到 top 最大的前 n 條記錄”的疑惑有所幫助!接下來,請(qǐng)跟著丸趣 TV 小編一起來學(xué)習(xí)吧!

  在最初接觸 mapreduce 時(shí),top n 問題的解決辦法是將 mapreduce 輸出(排序后)放入一個(gè)集合中,取前 n 個(gè),但這種寫法過于簡(jiǎn)單,內(nèi)存能夠加載的集合的大小是有上限的,一旦數(shù)據(jù)量大,很容易出現(xiàn)內(nèi)存溢出。
    今天在這里介紹另一種實(shí)現(xiàn)方式,當(dāng)然這也不是最好的方式。

    需求,得到 top 最大的前 n 條記錄
  這里只給出一些核心的代碼,其他 job 等配置的代碼略

Configuration conf = new Configuration();
conf.setInt(N , 5);

    初始化 job 之前需要 conf.setInt(N ,5); 意在在 mapreduce 階段讀取 N,N 就代表著 top N
    以下是 map
 

package com.lzz.one;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

*  #orderid,userid,payment,productid * [root@x00 hd]# cat seventeen_a.txt * 1,9819,100,121 * 2,8918,2000,111 * 3,2813,1234,22 * 4,9100,10,1101 * 5,3210,490,111 * 6,1298,28,1211 * 7,1010,281,90 * 8,1818,9000,20 * [root@x00 hd]# cat seventeen_b.txt * 100,3333,10,100 * 101,9321,1000,293 * 102,3881,701,20 * 103,6791,910,30 * 104,8888,11,39   *  預(yù)測(cè)結(jié)果:(求  Top N=5  的結(jié)果)* 1 9000 * 2 2000 * 3 1234 * 4 1000 * 5 910  * @author Administrator  *  */ public class TopNMapper extends Mapper LongWritable, Text, IntWritable, IntWritable {  int len;  int top[];  @Override  public void setup(Context context) throws IOException,InterruptedException { len = context.getConfiguration().getInt(N , 10);  top = new int[len+1];  }    @Override public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException { String line = value.toString();  String arr []= line.split( ,  if(arr != null   arr.length == 4){ int pay = Integer.parseInt(arr[2]);  add(pay);  }
@Override public void cleanup(Context context) throws IOException,InterruptedException { for(int i=1;i =len;i++){ context.write(new IntWritable(top[i]),new IntWritable(top[i]));  }  }   }

    接下來是 reduce

package com.lzz.one;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class TopNReduce extends Reducer IntWritable, IntWritable, IntWritable, IntWritable {
 int len;
 int top[];
 @Override
 public void setup(Context context)
 throws IOException, InterruptedException {len = context.getConfiguration().getInt(N , 10);
 top = new int[len+1];
 @Override
 public void reduce(IntWritable key, Iterable IntWritable  values,
 Context context)
 throws IOException, InterruptedException {for(IntWritable val : values){add(val.get());
 public void add(int pay){top[0] = pay;
 Arrays.sort(top);
 @Override
 public void cleanup(Context context)
 throws IOException, InterruptedException {for(int i=len;i i--){context.write(new IntWritable(len-i+1),new IntWritable(top[i]));
}

    說一下邏輯,雖然畫圖比較清晰,但是時(shí)間有限,畫圖水平有限,只用語言來描述吧,希望能說的明白
    如果要取 top 5,則應(yīng)該定義一個(gè)長(zhǎng)度為為 6 的數(shù)組,map 所要做的事情就是將每條日志的那個(gè)需要排序的字段放入數(shù)組第一個(gè)元素中,調(diào)用 Arrays.sort(Array[]) 方法可以將數(shù)組按照正序,從數(shù)字角度說是從小到大排序,比如第一條記錄是 9000,那么排序結(jié)果是[0,0,0,0,0,9000],第二條日志記錄是 8000,排序結(jié)果是[0,0,0,0,8000,9000],第三條日志記錄是 8500,排序結(jié)果是[0,0,0,8000,8500,9000],以此類推,每次放進(jìn)去一個(gè)數(shù)字如果大于數(shù)組里面最小的元素,相當(dāng)于將最小的覆蓋掉了,也就是說數(shù)組中元素永遠(yuǎn)是拿到日志中最大的那些個(gè)記錄
    ok,map 將數(shù)組原封不動(dòng)按照順序輸出,reduce 接收到從每個(gè) map 拿到的五個(gè)排好序的元素,在進(jìn)行跟 map 一樣的排序,排序后數(shù)組里面就是按照從小到大排好序的元素,將這些元素倒序輸出就是最終我們要的結(jié)果了

    與之前的方式做個(gè)比較,之前的 map 做的事情很少,在 reduce 中排序后哪前 5 條,reduce 的壓力是很大的,要把所有的數(shù)據(jù)都處理一遍,而一般設(shè)置 reduce 的個(gè)數(shù)較少,一旦數(shù)據(jù)較多,reduce 就會(huì)承受不了,悲劇了。而現(xiàn)在的方式巧妙的將 reduce 的壓力轉(zhuǎn)移到了 map,而 map 是集群效應(yīng)的,很多臺(tái)服務(wù)器來做這件事情,減少了一臺(tái)機(jī)器上的負(fù)擔(dān),每個(gè) map 其實(shí)只是輸出了 5 個(gè)元素而已,如果有 5 個(gè) map,其實(shí) reduce 才對(duì) 5 * 5 個(gè)數(shù)據(jù)進(jìn)行了操作,也就不會(huì)出現(xiàn)內(nèi)存溢出等問題了

到此,關(guān)于“如何用 mapreduce 得到 top 最大的前 n 條記錄”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-16發(fā)表,共計(jì)3442字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 达州市| 福州市| 漾濞| 玉门市| 嘉义市| 济源市| 彩票| 绩溪县| 河南省| 万载县| 溧水县| 上犹县| 财经| 兴文县| 庆云县| 家居| 简阳市| 昌乐县| 边坝县| 烟台市| 措勤县| 岱山县| 中江县| 斗六市| 兴宁市| 八宿县| 阿克陶县| 云和县| 濉溪县| 呼伦贝尔市| 佳木斯市| 罗源县| 延寿县| 岱山县| 镇沅| 大石桥市| 凉城县| 资讯 | 磴口县| 丹棱县| 郓城县|