2017-07-21 105 views
-1
public class PrimeMapper extends Mapper<LongWritable,IntWritable,IntWritable,NullWritable> 
    { 
     public void map(LongWritable k,IntWritable val,Context c) throws IOException, InterruptedException 
      { 
       int v=val.get(); 
       int i=2; 
       if(v==1) 
       c.write(new IntWritable(v), NullWritable.get()); 
       for(i=2;i<v;i++) 
        { 
         if(v%i==0) 
         break; 
        } 
       if(v==i) 
       c.write(new IntWritable(v),NullWritable.get()); 
      } 
    } 

當我試圖運行此代碼我得到類型錯誤的錯誤。java.lang.Exception的:java.lang.ClassCastException:org.apache.hadoop.io.Text不能轉換到org.apache.hadoop.io.IntWritable

enter image description here

+0

後堆棧跟蹤,並指定該行拋出異常。 – talex

+0

請添加堆棧跟蹤 – Sergii

+0

int v = val.get();此行錯誤 –

回答

0

當您使用默認的輸入格式是TextInputFormat(延伸FileInputFormat<LongWritable, Text>),映射器預計LongWritablekeyTextvalue。如果在程序中沒有明確地更改它,則映射器定義不正確。

正確的實現將是這個樣子:

public class PrimeMapper extends Mapper<LongWritable,Text,IntWritable,NullWritable>{ 
    public void map(LongWritable k,Text val,Context c) throws IOException, InterruptedException{ 
    int v=Integer.parseInt(val.toString().trim()); 
    int i=2; 
    if(v==1) 
    c.write(new IntWritable(v), NullWritable.get()); 
    for(i=2;i<v;i++) 
     { 
      if(v%i==0) 
      break; 
     } 
    if(v==i) 
    c.write(new IntWritable(v),NullWritable.get()); 
    } 
} 
相關問題