2016-06-08 21 views
2

有人可以幫助我瞭解爲什麼我收到的自定義數據類型的這種奇怪的行爲我指this和我的映射器代碼自定義數據的地圖輸出寫入未知的數據

public class customDataMapper extends Mapper<LongWritable, Text,Text,customText > { 

Text url = new Text(); 
Text date = new Text(); 
Text ip = new Text(); 
customText ctext = new customText(); 

public void map (LongWritable key , Text value , Context context) throws IOException , InterruptedException{ 

    String words[] = value.toString().split("|"); 
    url.set(words[1]); 
    date.set(words[2]); 
    ip.set(words[4]); 
    ctext.set(date,ip); 
    context.write(url, ctext); 
} 
} 

和 customText數據類型代碼是

public class customText implements WritableComparable<customText>{ 

private Text url , ip; 

public customText(){ 
    this.url=new Text(); 
    this.ip=new Text(); 

} 

public customText(Text URL , Text IP){ 
    this.url=URL; 
    this.ip=IP; 


} 


public void set (Text URL , Text IP){ 
    this.url=URL; 
    this.ip=IP; 

} 


public void readFields(DataInput in) throws IOException{ 
    url.readFields(in); 
    ip.readFields(in); 

} 

public void write(DataOutput out) throws IOException{ 
    url.write(out); 
    ip.write(out); 

} 


public int compareTo(customText o){ 
    if(url.compareTo(o.ip)==0){ 

     return (ip.compareTo(o.ip)); 

    } 
    else return (url.compareTo(o.ip)); 
} 


public boolean equals(Object o){ 


    if (o instanceof customText){ 
    customText other = (customText)o; 
    return (url.equals(other.ip)) && ip.equals(other.ip); 
    } 
    return false; 
} 

public int hashCode(){ 
    return url.hashCode(); 

} 

,我收到了我的輸出

hduser @ PRA深的VirtualBox:〜/構建$ hadoop的FS -cat /用戶/ hadoop的/ dir8_customData /輸出/部分-M-00000 1 [email protected] 1 [email protected] 1 customData.customDataSample1 .customText @ 51 1 [email protected] 1 [email protected]

和我的輸入文件是

127248|/rr.html|2014-03-10|12:32:08|42.416.153.181 
12|/rr12.html|2014-03-11|12:00:00|42.416.153.182 
127241|/rr3232.html|2014-03-12|13:32:00|42.416.153.183 
1272|/rrw33232.html|2014-03-15|14:32:08|42.416.153.184 
121|/rr21212.html|2015-12-10|16:32:08|42.416.153.185 

有人可以幫助我瞭解爲什麼我收到此輸出和 其次我不知道compareTo是如何工作的,我的意思是說如何在減速機中創建新組。我是hadoop和java編程的新手。

感謝

回答

3

您在使用|分裂split("|")。這應該是split("\\|")。看到這個SO回答why escaping a pipe is needed

您的customText類需要重寫toString(),以便它知道如何反序列化對象中包含的數據。例如:

@Override 
public String toString() { 
    return url + "," + ip; 
} 

您還設置您的Text對象錯誤:

public void set (Text URL , Text IP){ 
    this.url=URL; 
    this.ip=IP; 
} 

這應該是:

public void set(Text URL , Text IP){ 
    this.url.set(URL); 
    this.ip.set(IP); 
} 

如果您的自定義Writable對象被用作一個值,只需要實現Writable接口而不是WritableComparableWritableComparable接口僅用於Hadoop需要對鍵進行分組和排序的鍵。

compareTo()方法是沒有意義的(你比較網址到IP):

public int compareTo(customText o){ 
    if(url.compareTo(o.ip)==0){ 
     return (ip.compareTo(o.ip)); 
    } 
    else return (url.compareTo(o.ip)); 
} 

應該像這樣:

@Override 
public int compareTo(customText o) { 

    int result = url.compareTo(o.url); 
    if (result != 0) { 
     return result; 
    } 
    return ip.compareTo(o.ip); 
} 

你的散列碼應該是這個樣子:

@Override 
public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((ip == null) ? 0 : ip.hashCode()); 
    result = prime * result + ((url == null) ? 0 : url.hashCode()); 
    return result; 
} 

目前它只使用url而忽略ip

您也正在通過datectext.set(date,ip)。該自定義對象內部的該變量被稱爲url

風格明智的,你的變量名應該小寫URL = url和類應以大寫字母開始customText = CustomText

+0

非常感謝@Binary書呆子,你的評論幫了我很多。 –

1

由於toString()方法是在你所繼承的,你必須@Override了toString類繳費()

它應該在運行程序之前給出錯誤,而不是錯誤,但至少有一個黃色的通知,說這應該被覆蓋,或者我混淆了android工作室?

+0

感謝您提供的信息@Xenidia。我需要知道,如果我必須像使用自定義文本(文本,文本)那樣編寫數據類型,那麼我必須使用toString()方法來編寫上下文,但如果使用自定義數據類型包含兩個IntWritable,即使那麼我不得不使用toString來寫? –

+1

這與如何通過輸出格式將對象寫入磁盤有關。如果在將CustomText寫入磁盤時使用TextOutputFormat,則會調用它的'toString()'方法。在你的輸出中,你可以看到這個結果是一個指向對象的指針,這是Java中'toString()'的默認實現。 –

+1

無論你正在使用什麼數據類型,你必須使用toString()方法是否不同(上下文或構造函數有異常)或數據庫不同(例外情況是什麼二進制書呆子建議) – Xenidia

相關問題