2016-04-05 19 views
0

我有兩個名爲A.txt和B.txt的不同文件。如何使用從兩個映射器到一個reducer的鍵和值?

A.txt:     B.txt 
id name des   id name 
1 one  0   1 apple 
2 two  1   2 pine 
3 three 0   3 orange 

在這裏,我在這兩個文件中都有相同的字段「id」。 我使用了兩個mapper類。一個用於A.txt,另一個用於B.txt。有一個減速器類。

In MapperA.class "des" is checked and if "0" then its id is sent as key and value as name. 
In MapperB.class id and name is sent as key and value. 
Now, in reducer I need to determine the "id" from MapperA and check with "id" from MapperB and if present, then send key as "id" and value as "name". 
How can we compare the key and value from two mapper in a reducer to get desired result? 

從減速機所需的輸出是:

id  name 
1  apple 
3  orange 

回答

0

你不需要比較過程中減少功能。 會有每個輸入A.TXT和B.txt

映射器,用於A.TXT 如果(DES == 0) 然後 寫入ID,值2個獨立的映射類 - > DES>// DES將TextWritable( 「0」) 其他 不寫任何東西

映射器B.txt

ID,值 - >蘋果或橘子或松木...>

減速器

輸入將< 1,{0,蘋果}> < 2,{松樹}> < 3,{0,橙}>

然後在減少過程

Boolean shouldBeWritten=false; 
foreach value 
{ 
    char firstLetter = first char of value; 
    if(firstletter == 0) 
    shouldBeWritten=true; 
} 
if(shouldBeWritten) 
write(key, value); 

在這個實現中,你必須得到fruitname的第一個字母,這是不高效的,但你不必在第一個映射器中發送不必要的對。

這只是一個示例決定是由你決定的。

該決定取決於a.txt和b.txt的大小。

+0

我需要通過比較「des」值並將該ID傳遞給reducer來確定「id」。 – bthapa

相關問題