mapreduce调试查询System.out的结果

MapReduce调试技巧
本文针对初学者遇到的MapReduce程序调试难题,介绍了如何利用Hadoop的Web界面和日志文件来查看作业信息,帮助理解MapReduce任务的运行状态。

1.前言

刚接触mapreduce的人肯定为碰到这样的问题,就是我们在程序中如下类似的命令

 

[cpp]  view plain copy
  1. System.out.println(year+"   "+airTemperature);//无效,控制台没有输出。  

但是console控制台不给我们输出相应的结果,这对于很多通过System.out来调试的人来说,会是一个很头疼的事情,我也对这个很头疼。昨天在看《hadoop权威指南第二版》的第五章的时候,书中有介绍通过web界面来浏览hadoop的作业信息,发现在web界面中能看到许多作业的相关信息。并且知道mapreduce的作业信息都写在了用户日志中,存放在目录hadoop_home/logs/userlogs中。其他日志存放地点参考《hadoop权威指南第二版》p152的表5-2。通过web界面很容用找到这些日志。

package cn.cast; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; public class Mapreduce { public static class CleanMapper extends Mapper<LongWritable, Text, Text, Text> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); if (line.startsWith("Order_ID")) return; String[] fields = line.split(","); if (fields.length != 4) return; String orderId = fields[0]; String customerId = fields[1]; double amount = 0.0; try { amount = Double.parseDouble(fields[3]); } catch (NumberFormatException e) { return; } if (customerId.isEmpty() || amount <= 0) return; context.write(new Text(orderId), new Text(customerId + "," + fields[2] + "," + amount)); } } public static class CleanReducer extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { for (Text val : values) { context.write(key, val); } } } public static void main(String[] args) throws Exception { System.out.println("Debug: args.length = " + args.length); for (int i = 0; i < args.length; i++) { System.out.println(" args[" + i + "] = " + args[i]); } if (args.length != 2) { System.err.println("Usage: Mapreduce <input-path> <output-path>"); System.exit(-1); } try { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://node1:9000"); Job job = Job.getInstance(conf, "Data Cleaning"); job.setJarByClass(Mapreduce.class); job.setMapperClass(CleanMapper.class); job.setReducerClass(CleanReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
06-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值