zoukankan      html  css  js  c++  java
  • Hadoop命令行接口运行自己编写的类

    转自:http://blog.chinaunix.net/uid-11767650-id-1673086.html 因为我也遇到了这个问题,所以就直接转载了。。。 最近学习Hadoop,可以使用命令行来简单的管理Hadoop,例如使用: #查看hadoop的HDFS根节点情况
    hadoop fs -ls /
    
    #创建文件夹LGG hadoop fs mkdir /user/root/LGG 通过命令行接口来进行操作。 当然,hadoop命令行接口同样支持自己编写的类的参数,例如在Hadoop权威指南中的一个例子如下:
    import java.io.InputStream;
    import java.net.URL;
    import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
    import org.apache.hadoop.io.IOUtils;
    
    /**
     * 
     * @author LGG
     *
     */
    public class URLCat {
        static{
            URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        }
        public static void main(String[] args) throws Exception{
            InputStream in = null;
            try{
                in = new URL(args[0]).openStream();
                IOUtils.copyBytes(in, System.out, 4096,false);
            }finally{
                IOUtils.closeStream(in);
            }
        }
    }
    
    将URLCat.java编译后生成class文件,然后可以将此类作为一个hadoop命令行接口的参数进行使用,如下: hadoop URLCat hdfs://localhost/user/tom/readme.txt 作用是将HDFS文件里面的readme.txt输出到System.out中,就是标准输出。但是在执行的时候会报以下错误:
    Exception in thread "main" java.lang.NoClassDefFoundError: URLCat
    Caused by: java.lang.ClassNotFoundException: URLCat
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: URLCat. Program will exit.
    
    这个异常是典型的类装入问题的异常,异常表明在类装入的过程中,没有找到URLCat这个类的定义。于是LGG查阅了相关资料,发现hadoop脚本执行的时候,classpath的搜索路径默认会在当前系统变量$HADOOP_CLASSPATH中去寻找,于是echo查看之,发现竟然显示为空! 问题找到了,因为系统指定了classpath,所以回去这个环境变量中去寻找,但是在当前路径下找不到类URLCat(废话,都是空的了)。使用export命令就可以解决了: #.表示当前目录,即类所在目录,只有这样才能让hadoop找到这个类

    export HADOOP_CLASSPATH=.

    然后再执行: hadoop URLCat hdfs://master:9000/user/grid/test/test1.txt 就搞定了~~
  • 相关阅读:
    LSMW TIPS
    Schedule agreement and Delfor
    Running VL10 in the background 13 Oct
    analyse idoc by creation date
    New Journey Prepare
    EDI error
    CBSN NEWS
    Listen and Write 18th Feb 2019
    Microsoft iSCSI Software Target 快照管理
    通过 Microsoft iSCSI Software Target 提供存储服务
  • 原文地址:https://www.cnblogs.com/iamchao/p/3677154.html
Copyright © 2011-2022 走看看