用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]);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM