2016-08-16 5 views
4

セールスドライバークラスの減速、MapReduceの、エラーが表示されませんが、所望の出力を与えるものではありません

package mr.map; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.mapreduce.Job; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 

import org.apache.hadoop.io.Text; 
import org.apache.hadoop.io.FloatWritable; 
//import org.apache.hadoop.mapreduce.Mapper; 
//import org.apache.hadoop.mapreduce.Reducer; 

public class SalesDriver 
{ 
    public static void main(String args[]) throws Exception 
    { 
     Configuration c=new Configuration(); 
     Job j=new Job(c,"Sales"); 

     j.setJarByClass(SalesDriver.class); 
     j.setMapperClass(SalesMapper.class); 
     j.setReducerClass(SalesReducer.class); 

     //j.setNumReduceTasks(0); 
     j.setOutputKeyClass(Text.class); 
     j.setOutputValueClass(FloatWritable.class); 

     Path in=new Path(args[0]); 
     Path out=new Path(args[1]); 

     FileInputFormat.addInputPath(j, in); 
     FileOutputFormat.setOutputPath(j, out); 

     System.exit(j.waitForCompletion(true)?0:1); 
    } 
} 

販売マッパーのクラス

package mr.map; 

import java.io.IOException; 

//import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.FloatWritable; 
import org.apache.hadoop.io.LongWritable; 
//import org.apache.hadoop.io.DoubleWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Mapper; 

public class SalesMapper extends Mapper<LongWritable, Text, Text, FloatWritable> 
{ 
    public void map(LongWritable k, Text v, Context con) throws IOException, InterruptedException 
    { 
     String w[]=v.toString().split(" "); 
     String product=w[3]; 
     //String store=w[2]; 
     //float cost=Integer.parseInt(w[4]); 
     float costx = Float.parseFloat(w[4]); 

     //String newline= product+","+store; //","+costx; 
     //String newline = product; 
     con.write(new Text(product), new FloatWritable(costx)); 
    } 
} 

販売リデューサークラス

package mr.map; 

import java.io.IOException; 

import org.apache.hadoop.io.FloatWritable; 
//import org.apache.hadoop.io.IntWritable; 
//import org.apache.hadoop.io.DoubleWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Reducer; 

public class SalesReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> 
{ 
    public void reduce(Text k, Iterable<FloatWritable>vlist, Context con) throws IOException, InterruptedException 
    { 
     int tot=0; 
     for (FloatWritable v:vlist) 
     { 
      tot += v.get(); 
     } 
     //int total= (int)tot; 
     con.write(new Text(k), new FloatWritable(tot)); 
    } 
} 

結果MapReduce

Result of the MapReduce

私は大きな浮動小数点数とすべての数値5.7480884E7にすべての結果が出てくるのを理解できません。テキストと予想形式の文字列にフロートを変換する

2012-01-01 09:00 San Jose Men's Clothing 214.05 Amex    
    2012-01-01 09:00 Fort Worth Women's Clothing 153.57 Visa   
    2012-01-01 09:00 San Diego Music 66.08 Cash      
    2012-01-01 09:00 Pittsburgh Pet Supplies 493.51 Discover   
    2012-01-01 09:00 Omaha Children's Clothing 235.63 MasterCard  
    2012-01-01 09:00 Stockton Men's Clothing 247.18 MasterCard   
    2012-01-01 09:00 Austin Cameras 379.6 Visa       
    2012-01-01 09:00 New York Consumer Electronics 296.8 Cash   
    2012-01-01 09:00 Corpus Christi Toys 25.38 Discover    
    2012-01-01 09:00 Fort Worth Toys 213.88 Visa      
    2012-01-01 09:00 Las Vegas Video Games 53.26 Visa     
    2012-01-01 09:00 Newark Video Games 39.75 Cash      
    2012-01-01 09:00 Austin Cameras 469.63 MasterCard     
    2012-01-01 09:00 Greensboro DVDs 290.82 MasterCard     
    2012-01-01 09:00 San Francisco Music 260.65 Discover    
    2012-01-01 09:00 Lincoln Garden 136.9 Visa       
    2012-01-01 09:00 Buffalo Women's Clothing 483.82 Visa    
    2012-01-01 09:00 San Jose Women's Clothing 215.82 Cash    
    2012-01-01 09:00 Boston Cameras 418.94 Amex      
    2012-01-01 09:00 Houston Baby 309.16 Visa       
    2012-01-01 09:00 Las Vegas Books 93.39 Visa      
    2012-01-01 09:00 Virginia Beach Children's Clothing 376.11 Amex 
    2012-01-01 09:01 Riverside Consumer Electronics 252.88 Cash  
    2012-01-01 09:01 Tulsa Baby 205.06 Visa       
    2012-01-01 09:01 Reno Crafts 88.25 Visa       
    2012-01-01 09:01 Chicago Books 31.08 Cash       
    2012-01-01 09:01 Fort Wayne Men's Clothing 370.55 Amex    
    2012-01-01 09:01 San Bernardino Consumer Electronics 170.2 Cash 
    2012-01-01 09:01 Madison Men's Clothing 16.78 Visa     
    2012-01-01 09:01 Austin Sporting Goods 327.75 Discover    
    2012-01-01 09:01 Portland CDs 108.69 Amex       
    2012-01-01 09:01 Riverside Sporting Goods 15.41 Discover   
    2012-01-01 09:01 Reno Toys 80.46 Visa        
    2012-01-01 09:01 Anchorage Music 298.86 MasterCard  
+0

これは、MapReduceのprogramBaby \t 5.7480884E7 ブックス\t 5.743978E7 のCD \t 5.7400252E7 カメラ\t 5.728862E7 子供服\t 5.7612936E7 コンピュータ\t 5.7303832E7 コンシューマーエレクトロニクス\t 5.744192E7 工芸のこの出力\t 5.7407532e7 DVD \t 5.763812e7 庭\t 5.7528848e7 健康と美容\t 5.7469112E7 メンズファッション\t 5.7609916E7 音楽\t 5.7484752E7 ペット\t 5.7186328E7 スポーツ用品\t 5.7587608E7 おもちゃ\t 5.7452464E7 ビデオゲーム\t 5.750184E7 婦人服\t 5に供給する。7423576E7 – habeebsiddique

答えて

0

変更リデューサの出力値の種類:

は、以下のMapReduceプログラムへの入力の一例です。数値をフォーマット詳細は記事下の

String.format("%f",tot) 

フォロー:
with scientific
without scientific notation

リデューサー:

public class SalesReducer extends Reducer<Text, FloatWritable, Text, Text> 
{ 
    public void reduce(Text k, Iterable<FloatWritable>vlist, Context con) throws IOException, InterruptedException 
    { 
     float tot=0; 
     for (FloatWritable v:vlist) 
     { 
      tot += v.get(); 
     } 
     //int total= (int)tot; 
     con.write(new Text(k), new Text(String.format("%f",tot))); 
    } 
} 
+0

上記のように文字列に変換した。しかし、私は "java.util.IllegalFormatConversionException:f!= java.lang.Integer"というエラーを受け取りました。私は "%f"を "%s"に置き換えました。プログラムはエラーなしで実行されましたが、出力は5.7400252E7ではなく57400252になりました。小数点なしに – habeebsiddique

+0

私はそれを投稿して問題の手がかりを与えました。あなたができることは、totのデータ型をfloatに変更することです。 DecimalFormat apiを使用して必要な書式を設定します。 –

+0

私もtotタイプをfloatに変更しましたが、同じ結果が得られました。 – habeebsiddique

0

あなたはint型の変数に山車の和の値を格納しています。
まず、intは浮動小数点数の後の精度でfloat値を処理できません。
第2に、行の数が非常に多い場合、合計値はの許容範囲を十分に超える可能性があります。

tot変数をintから floatまたは doubleに変更してみてください。

double tot=0; 
+0

タイプをfloatに変更しました。以前と同じ結果が得られました。 – habeebsiddique

+0

入力データセットには、スペースで区切る可変長レコードが含まれています。 ** 2012年1月1日午前9時00分サンノゼメンズファッション214.05アメックス** これは[6]位ワットの数値を持っているのに対して、 ** 2012年1月1日午前9時01リノのおもちゃ80.46 Visa ** これはw [4]です。mappersが文字列の正しい位置を数えているとは確信していません。 –

+0

フィールドはタブの間隔で区切られています。私はマッパーでタブスペーサーを使用しています。マッパーは正しい出力を出しています。 – habeebsiddique

関連する問題