2014-09-25 88 views
-1

我和我的MapReduce代码的麻烦。我的代码将写入最大高股票和相应的名称从输入到输出文件。问题是正在写零字节,并且在输出中得到一个空文件。的Hadoop没有书面方式输出到文件

HighestStock.java

JobConf conf = new JobConf(HighestStock.class); 
conf.setJobName("Highest Stock"); 

FileInputFormat.addInputPath(conf, new Path(args[0]); 
FileOutputFormat.setOutputPath(conf, new Path(args[1]); 

conf.setMapperClass(HighStockMapper.class); 
conf.setReducerClass(HighStockReducer.class); 

conf.setOutputKeyClass(Text.class); 
conf.setOutputValueClass(IntWritable.class); 

JobClient.runJob(conf); 

HighStockMapper.java

public class HighStockMapper implements Mapper<LongWritable, Text, Text, IntWritable> { 
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
      String line = value.toString(); 
      String[] tokens = line.split(","); 
      String name = tokens[0]; 
       int high = (int) Double.parseDouble(tokens[3]); 
       context.write(new Text(name), new IntWritable(high); 
      } 

HighStockReducer.java

public class HighStockReducer extends MapReduceBase 
implements Reducer<Text, IntWritable, Text, IntWritable> { 
     public void reduce (Tex key, Iterator<IntWritable> values, 
      OutputCollector<Text, IntWritable> output, Reporter reporter) 
     throws IOExceptiion { 
      int maxValue = Integer.MIN_VALUE; 
      while (values.hasNext()) { 
        maxValue = Math.max(maxValue, values.next().get()); 
      } 
      output.collect(key, new IntWritable(maxValue)); 
     } 
    } 

任何帮助调试这将非常感谢!

截图命令行的结果 Screenshot of command-line results

回答

0

你确实有一些拼写错误在那里。我不认为这会连编译..

context.write(new Text(name), new IntWritable(high); // Missing closing) 

throws IOExceptiion { // extra i 

我把它这是不是您正在运行的实际代码?你能发布运行作业的命令行结果(包括命令和计数器输出)吗?

编辑:@安娜·迈,你的屏幕截图显示,该映射器不发光时的任何记录(图输出记录= 0),所以这个问题是在你的映射器。

我注意到你缺少从你的映射器延伸MapReduceBase。尝试添加:

public class HighStockMapper extends MapReduceBase implements Mapper 
+0

是啊,我是从我的虚拟机重新键入这一点,因为它不会让我来回拷贝。我要发表新评论(上面这个帖子找到)用的,当我编译 – 2014-09-26 02:28:03

+0

后,您在混合新的Hadoop API和旧的API运行它,我得到什么的截图,在映射您使用context.write和减速您使用的输出.collect有没有特别的原因? – user3484461 2014-09-26 06:16:36

+0

没有特别的原因,我是立足我的代码关闭的旧代码,我在GitHub上发现这可能就是为什么有一些不一致的地方。我该怎么改变,以便mapper和reducer api的匹配? – 2014-09-26 13:26:47

0

如前所述,您正在混合使用较旧和较新的实现。您需要对映射程序类进行以下更改:

class HighStockMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { 

public void map(LongWritable key, Text value, 
     OutputCollector<Text, IntWritable> output, Reporter arg3) 
     throws IOException { 
    String line = value.toString(); 
    String[] tokens = line.split(","); 
    String name = tokens[0]; 
     int high = (int) Double.parseDouble(tokens[3]); 
     output.collect(new Text(name), new IntWritable(high)); 

} 

并且代码运行良好。

+0

感谢您的回答,我试着把你写的东西放进去,但Eclipse告诉我必须将map重命名为map1。创建jar文件并在命令行中运行后,我仍然得到一个空文件:/ – 2014-09-29 13:43:33