課程
/云計算&大數(shù)據(jù)
/大數(shù)據(jù)
/快速入門Hadoop3.0大數(shù)據(jù)處理
為什么會出現(xiàn)這種問題呢?
2020-02-12
源自:快速入門Hadoop3.0大數(shù)據(jù)處理 5-4
正在回答
大佬們,本人剛剛接觸hadoop就遇到這個,這個問題我太頭疼了,大佬們請幫我一下?。。?/p>
帝國大學
/創(chuàng)建靜態(tài)內(nèi)部類MyMapper
public static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
//調(diào)用自帶的map()函數(shù)
protected void map(LongWritable k1,Text v1,Context context)?
throws IOException,InterruptedException{
//k1代表的是每一行的偏移量,v1代表的是每一行的內(nèi)容
//將傳送進來的每一行數(shù)據(jù)切割,切割成單詞
String[] words =v1.toString().split(" ");
//迭代切割出來的單詞數(shù)據(jù)
for(String word:words) {
//將迭代出來的單詞數(shù)據(jù)封裝成<k2,v2>
Text k2 =new Text(word);
LongWritable v2=new LongWritable();
? ? ?//把<k2,v2>寫出去
context.write(k2, v2);
}
//創(chuàng)建自定義靜態(tài)內(nèi)部類MyReducer
public static class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable>{
/*
* 針對v2s做累加求和,并且最終把<k3,v3>寫出去
*/
protected void reducer(Text k2,Iterable<LongWritable> v2s,Context context)
? ? ? throws IOException,InterruptedException{
//創(chuàng)建一個表量sum來保存v2s的累加值
long sum=0;
for(LongWritable v2:v2s) {
sum=sum+v2.get();
//組裝<k3,v3>
Text k3=k2;
LongWritable v3=new LongWritable(sum);
//把組裝好的<k3,v3>寫出去
context.write(k3, v3);
代碼好像沒什么問題?
我猜你的map的初始值是0吧?而且reduce沒有求和,檢查一下你的代碼?
qq_慕斯2033129 提問者
0CJJ0 回復 qq_慕斯2033129 提問者
舉報
快速入門Hadoop3.0數(shù)據(jù)處理,輕松邁入大數(shù)據(jù)領域
2 回答老師求救啊
2 回答大鵬哥~~有個資料
1 回答有沒有什么好的方法造大量數(shù)據(jù)的
6 回答老師,什么時候出大數(shù)據(jù)的實戰(zhàn)課程?
1 回答中間數(shù)據(jù)存儲在map中,數(shù)據(jù)量大能能解決嗎?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2020-02-26
2020-02-12
/創(chuàng)建靜態(tài)內(nèi)部類MyMapper
public static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
//調(diào)用自帶的map()函數(shù)
protected void map(LongWritable k1,Text v1,Context context)?
throws IOException,InterruptedException{
//k1代表的是每一行的偏移量,v1代表的是每一行的內(nèi)容
//將傳送進來的每一行數(shù)據(jù)切割,切割成單詞
String[] words =v1.toString().split(" ");
//迭代切割出來的單詞數(shù)據(jù)
for(String word:words) {
//將迭代出來的單詞數(shù)據(jù)封裝成<k2,v2>
Text k2 =new Text(word);
LongWritable v2=new LongWritable();
? ? ?//把<k2,v2>寫出去
context.write(k2, v2);
}
}
}
//創(chuàng)建自定義靜態(tài)內(nèi)部類MyReducer
public static class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable>{
/*
* 針對v2s做累加求和,并且最終把<k3,v3>寫出去
*/
protected void reducer(Text k2,Iterable<LongWritable> v2s,Context context)
? ? ? throws IOException,InterruptedException{
//創(chuàng)建一個表量sum來保存v2s的累加值
long sum=0;
for(LongWritable v2:v2s) {
sum=sum+v2.get();
}
//組裝<k3,v3>
Text k3=k2;
LongWritable v3=new LongWritable(sum);
//把組裝好的<k3,v3>寫出去
context.write(k3, v3);
}
}
代碼好像沒什么問題?
2020-02-12
我猜你的map的初始值是0吧?而且reduce沒有求和,檢查一下你的代碼?