Java Reducer Mapreduce不显示错误,但不提供所需的输出

Java Reducer Mapreduce不显示错误,但不提供所需的输出,java,hadoop,mapreduce,reducers,Java,Hadoop,Mapreduce,Reducers,销售驱动程序类 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.outpu

销售驱动程序类

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));
    }
}

将reducer的输出值类型更改为Text,并将Float转换为预期格式的字符串

String.format("%f",tot)
有关格式化数字的更多详细信息,请参见以下帖子:

减速器:

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)));
    }
}
公共类SalesReducer扩展了Reducer
{
public void reduce(Text k、Iterablevlist、Context con)抛出IOException、InterruptedException
{
浮动tot=0;
for(浮动可写v:vlist)
{
tot+=v.get();
}
//整数总计=(整数)总计;
con.write(新文本(k),新文本(字符串格式(“%f”,tot));
}
}

您正在将浮点总和的值存储在一个int变量中。
现在,第一件事是int无法处理小数点后的浮点值。
其次,如果行数非常高,则总和值可能远远超出可接受的int范围

请尝试将tot变量从int更改为float或double


这是mapreduce程序Baby 5.7480884E7 Books 5.743978E7 CD 5.7400252E7摄像头5.728862E7儿童服装5.7612936E7计算机5.7303832E7消费电子5.744192E7工艺品5.7407532E7 DVD 5.763812E7 Garden 5.752884E7健康与美容5.7469112E7男装5.7609916E7音乐5.7484752E7宠物用品5.718632E7的输出运动用品5.7587608E7玩具5.7452464E7视频游戏5.750184E7女式服装5.7423576E7i将类型更改为浮动。它给出了与以前相同的结果。您的输入数据集包含可变长度的记录,您正试图在空格中分隔这些记录。2012-01-01 09:00圣何塞男装214.05美国运通这在w[6]位置有数值,而2012-01-01 09:01雷诺玩具80.46维萨这在w[4]位置有数值。我不确定制图员是否计算字符串的正确位置。字段按制表符间距分开,我在制图器中使用制表符间隔符。映射器给出了正确的输出。我看到一个浮点溢出或一些内部类型转换问题。我想我需要对iterable变量进行类型转换,但我不确定,你认为iterable变量FloatWritable对我来说合适吗。但如果有疑问,你可以尝试。你能告诉我你的数据集有多大吗?总共大约有多少行。?我最近遇到了一个类似的问题,数值变得荒谬,因为总数超过了9位数。我把总变量改成了两倍,我就这样做了。如果有帮助的话,你可以试试。我按照上面给出的转换为字符串。但我得到的错误是“java.util.IllegalFormatConversionException:f!=java.lang.Integer”。我将“%f”替换为“%s”。程序运行时没有错误,但输出是57400252,而不是5.7400252E7,即没有小数点,我发布它是为了给您有关问题的线索。您可以做的是将tot的数据类型更改为float。使用DecimalFormatAPI实现所需的格式。我还将tot类型更改为float,但它给出了相同的结果。我认为可能是浮点溢出或某些内部类型转换问题。我想我需要键入iterable变量,但我不确定,你有什么建议,我如何键入iterable。请按照科学符号链接回答。
String.format("%f",tot)
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)));
    }
}
double tot=0;