zoukankan      html  css  js  c++  java
  • 用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.

    用java运行Hadoop例程报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.所写代码如下:

    package com.pcitc.hadoop;
    
    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.hdfs.DistributedFileSystem;
    import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
    
    /**
     * 获取HDFS集群上所有节点名称
     * @author lenovo
     *
     */
    public class GetList {
        public static void main(String[] args) throws IOException {
            Configuration conf = new Configuration();
            conf.set("dfs.default.name", "hdfs://hadoopmaster:9000");
            FileSystem fs = FileSystem.get(conf);
            DistributedFileSystem hdfs = (DistributedFileSystem) fs;
            DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
            String[] names = new String[dataNodeStats.length];
            for (int i = 0; i < dataNodeStats.length; i++) {
                names[i] = dataNodeStats[i].getHostName();
                System.out.println("node" + i + "name" + names[i]);
            }
        }
    }

    执行之后报如下错误:

    Exception in thread "main" java.lang.ClassCastException: org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.hadoop.hdfs.DistributedFileSystem
        at org.apache.hadoop.examples.FindFileOnHDFS.getHDFSNodes(FindFileOnHDFS.java:43)
        at org.apache.hadoop.examples.FindFileOnHDFS.main(FindFileOnHDFS.java:16)

    原因是DistributedFileSystem和LocalFileSystem都是FileSystem的子类,FileSystem.get(conf)得到的是LocalFileSystem的instance, 这个类型应该是默认的,要获得DistributedFileSystem,需要配置conf对象,按照我的写法我觉得应该是配了conf对象了,但是还是保存,最后按照网上的说法进行相应修改就可以了。直接上修改后的代码如下(注意红色部分):

    package com.pcitc.hadoop;
    
    import java.io.IOException;
    import java.net.URI;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.hdfs.DistributedFileSystem;
    import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
    
    /**
     * 获取HDFS集群上所有节点名称
     * 
     * @author lenovo
     * 
     */
    public class GetList {
        public static void main(String[] args) throws IOException {
            Configuration conf = new Configuration();
            // conf.set("dfs.default.name", "hdfs://hadoopmaster:9000");
            String uri = "hdfs://hadoopmaster:9000";
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            DistributedFileSystem hdfs = (DistributedFileSystem) fs;
            DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
            String[] names = new String[dataNodeStats.length];
            for (int i = 0; i < dataNodeStats.length; i++) {
                names[i] = dataNodeStats[i].getHostName();
                System.out.println("node:" + i + ",name:" + names[i]);
            }
        }
    }
  • 相关阅读:
    nginx部署vue工程和反向代理nodejs工程
    memcache 原理 & 监测 & 查看状态 & stats & 结构
    CRT(secureCRT)中文显示研究&Linux中文字符显示
    Linux rz的使用
    htmlspecialschars与htmlentities的区别
    ie下php session不能用(域名的合法定义)
    常用生产环境的PHP安装参数
    XMLREADER/DOM/SIMPLEXML 解析大文件
    Linux中的小括号和大括号,${}/$()/()/{}/${var:-string}/${var:=string}/${var:+string}/${var:?string}/${var%pattern}/${var#pattern}/${var%%pattern}/${var##pattern}
    好用的window命令
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/4811639.html
Copyright © 2011-2022 走看看