2 rjy1989 rjy1989 于 2013.11.19 16:26 提问

我在java上运行Hadoop的例程 FindFileOnHDFS 结果报错了

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)
怎么办?
例程的代码如下(代码是肯定没错的):
package org.apache.hadoop.examples;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

public class FindFileOnHDFS {

public static void main(String[] args) throws Exception{
getHDFSNodes();
getFileLocal();
}

public static void getFileLocal() throws Exception {

Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
Path fpath = new Path("/user/root/20120722/word.txt");

FileStatus fileStatus = hdfs.getFileStatus(fpath);
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());

int blockLen = blkLocations.length;

for(int i = 0 ; i < blockLen ; ++i ){
String[] hosts = blkLocations[i].getHosts();
System.out.println("block_"+i + "_location:" + hosts[i]);
}
}

public static void getHDFSNodes() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();

for( int i = 0 ; i < dataNodeStats.length ; ++i ){
System.out.println("DataNode_" + i + "_Node:" + dataNodeStats[i].getHostName());
}
}
}

1个回答

qq_30135665
qq_30135665   2016.08.22 23:33

你给的是一个本地文件,怎么能转成hdfs文件,你先把文件传到hdfs上去吧,然后给个hdfs文件的路径而不是本地文件的路径。

Csdn user default icon
上传中...
上传图片
插入图片