分享

第一个 hadoop程序 有问题,求解答,多谢。

zuiaituantuan 发表于 2013-10-16 13:41:16 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 5479
写了一个练习的mapreduce程序,参照《hadoop权威指南》第二章,代码见后:NewMaxTemperature.java 成功编译,并将三个class打包成jar,然后调用如下:
hadoop jar NewMaxTemperature.jar NewMaxTemperature /tempData/998218-99999-2008.op /7.txt
运行出错。
问题如下:
总是提示
java.lang.RuntimeException: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper;
但是我已经设置了jar包并且设置了全局变量 HADOOP_CLASSPATH=".:/opt/hadoop/build/classes"
错误信息如下:
11/12/04 20:20:48 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
11/12/04 20:20:48 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
11/12/04 20:20:48 INFO input.FileInputFormat: Total input paths to process : 1
11/12/04 20:20:48 INFO mapred.JobClient: Running job: job_201112041508_0034
11/12/04 20:20:49 INFO mapred.JobClient:  map 0% reduce 0%
11/12/04 20:21:00 INFO mapred.JobClient: Task Id : attempt_201112041508_0034_m_000000_0, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:809)
        at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:157)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:569)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:305)
        at org.apache.hadoop.mapred.Child.main(Child.java:170)
Caused by: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:762)
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:807)
        ... 4 more
11/12/04 20:21:06 INFO mapred.JobClient: Task Id : attempt_201112041508_0034_m_000000_1, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:809)
        at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:157)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:569)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:305)
        at org.apache.hadoop.mapred.Child.main(Child.java:170)
Caused by: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:762)
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:807)
        ... 4 more
11/12/04 20:21:12 INFO mapred.JobClient: Task Id : attempt_201112041508_0034_m_000000_2, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:809)
        at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:157)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:569)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:305)
        at org.apache.hadoop.mapred.Child.main(Child.java:170)
Caused by: java.lang.ClassNotFoundException: NewMaxTemperature$NewMaxTemperatureMapper
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:762)
        at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:807)
        ... 4 more
11/12/04 20:21:21 INFO mapred.JobClient: Job complete: job_201112041508_0034
11/12/04 20:21:21 INFO mapred.JobClient: Counters: 3
11/12/04 20:21:21 INFO mapred.JobClient:   Job Counters
11/12/04 20:21:21 INFO mapred.JobClient:     Launched map tasks=4
11/12/04 20:21:21 INFO mapred.JobClient:     Data-local map tasks=4
11/12/04 20:21:21 INFO mapred.JobClient:     Failed map tasks=1
=========================================================
代码为:
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class NewMaxTemperature {
        static class NewMaxTemperatureMapper
                /*[*/extends Mapper/*]*/ {
                        private static final int MISSING = 9999;
                        public void map(LongWritable key, Text value, /*[*/Context context/*]*/) throws IOException, /*[*/InterruptedException/*]*/ {
        //                public void map( LongWritable key , Text value, OutputCollector output, Reporter reporter ) throws IOException {
                                String line = value.toString();
                                String year = line.substring(15,18);
                                float airTemperature;
                                airTemperature = Float.parseFloat( line.substring( 25,30 ));
                                context.write( new Text(year),new FloatWritable( airTemperature ) );
                        }
                }
/*
        public static class MaxTemperatureReducer extends MapReduceBase
                implements Reducer {
                        public void reduce( Text key,Iterator values, OutputCollector output,Reporter reporter) throws IOException {
*/
        static class NewMaxTemperatureReducer
        /*[*/extends Reducer/*]*/ {
                public void reduce(Text key, /*[*/Iterable/*]*/ values,
                                /*[*/Context context/*]*/)
                        throws IOException, /*[*/InterruptedException/*]*/ {
                                float maxValue = -9999;
                                for (FloatWritable value : values) {
                                        maxValue = Math.max(maxValue, value.get());
                                }
                                /*[*/context.write/*]*/(key, new FloatWritable(maxValue));
                        }
                }
        public static void main( String[] args ) throws Exception {
                if( args.length != 2) {
                        System.err.println( "usage: MaxTemperature [i] " );
                        System.exit( -1 );
                }
                /*
                        System.err.println( "usage: 1" );
                JobConf conf = new JobConf( MaxTemperature.class );
                        System.err.println( "usage: 2" );
                conf.setJobName("Max Temperature");
                FileInputFormat.addInputPath( conf,new Path(args[0]) );
                FileOutputFormat.setOutputPath(conf,new Path(args[1]));
                conf.setMapperClass( MaxTemperatureMapper.class );
                conf.setReducerClass( MaxTemperatureReducer.class);
                //        conf.setOutputkeyClass( Text.class );
                conf.setOutputKeyClass( Text.class );
                conf.setOutputValueClass( FloatWritable.class );
                JobClient.runJob( conf );
                */
                /*[*/Job job = new Job();
    job.setJarByClass(NewMaxTemperature.class);/*]*/
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
   
    job.setMapperClass(NewMaxTemperatureMapper.class);
    job.setReducerClass(NewMaxTemperatureReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
   
    System.exit( job.waitForCompletion(true) ? 0 : 1 );
        }
}
              
               
               

已有(4)人评论

跳转到指定楼层
tntzbzc 发表于 2013-10-16 13:42:13

            我和楼主遇到同样问题了,哎
        
回复

使用道具 举报

jessezhang1981 发表于 2013-10-16 13:42:56

            注意这行错误:
11/12/04 20:20:48 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
11/12/04 20:20:48 INFO input.FileInputFormat: Total input paths to process : 1
问题原因及解决办法如下:
因为使用的是0.20以上的Hadoop版本,在调用jar中的自定义mapper时,需要设置setJarByClass方法,设置方法如下:
job.setJarByClass(MyJob.class);
谢谢!
JesseZhang http://www.cnzqs.com
        
回复

使用道具 举报

jessezhang1981 发表于 2013-10-16 13:43:32

            楼主发了帖子,不来看了啊。。。
        
回复

使用道具 举报

haizishiwo 发表于 2013-10-16 13:44:06

            job.setMapperClass(NewMaxTemperatureMapper.class);
    job.setReducerClass(NewMaxTemperatureReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
   
    System.exit( job.waitForCompletion(true) ? 0 : 1 );
楼主不是已经设置了吗,所以你说的不对呀。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条