繁体   English   中英

Hadoop分布式缓存:使用-libjars:如何在代码中使用外部jar

[英]Hadoop distributed cache : using -libjars : How to use external jars in your code

好的,我能够使用ilibjars路径将外部jar添加到我的代码中。 现在如何在我的代码中使用这些外部jar。 说我有一个在罐子上定义的函数,该函数对String进行操作。 如何使用它。 使用context.getArchiveClassPaths(),我可以获取到它的路径,但是我不知道如何实例化该对象。

这是我要导入的示例jar类

package replace;

public class ReplacingAcronyms {

    public static String Replace(String abc){
        String n;
        n="This is trial";
        return n;
}

}





public class wc_runner extends Configured implements Tool {
        @Override
        public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        Job job = new Job(new Configuration());
        job.setJarByClass(wc_runner.class);

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

        job.setMapperClass(wc_mapper.class);
        job.setCombinerClass(wc_reducer.class);
        job.setReducerClass(wc_reducer.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        return (job.waitForCompletion(true)?0:1);


        }        

    public static void main(String[] args) throws Exception {
        int exitCode = ToolRunner.run(new wc_runner(), args);
        System.exit(exitCode);
    }
}

命令跑了

[training@localhost Desktop]$ export HADOOP_CLASSPATH=file:///home/training/Desktop/replace.jar 
[training@localhost Desktop]$ hadoop jar try1.jar wc_runner /user/training/MR/custom/trial1 /user/training/MR/custom/out -libjars ./replace.jar

错误

14/03/08 02:39:40 WARN mapred.JobClient: Use GenericOptionsParser for parsing the    arguments. Applications should implement Tool for the same.
14/03/08 02:39:41 INFO input.FileInputFormat: Total input paths to process : 1
14/03/08 02:39:41 WARN snappy.LoadSnappy: Snappy native library is available
14/03/08 02:39:41 INFO snappy.LoadSnappy: Snappy native library loaded
14/03/08 02:39:41 INFO mapred.JobClient: Running job: job_201403080114_0021
14/03/08 02:39:42 INFO mapred.JobClient:  map 0% reduce 0%
14/03/08 02:39:46 INFO mapred.JobClient: Task Id : attempt_201403080114_0021_m_000000_0, Status : FAILED
Error: java.lang.ClassNotFoundException: replace.ReplacingAcronyms
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190

将包导入到映射的代码中,然后在运行映射的作业之前在HADOOP_CLASSPATH中添加jar文件的路径。

例如在您的mapred java中

import your.external.package;

关于编译

javac -cp /path/to/your/external/package.jar:...

在运行Hadoop的罐子

export HADOOP_CLASSPATH=/path/to/your/external/package.jar
hadoop jar yourmapred.jar your.class -libjar /path/to/your/external/package.jar ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM