Running the Hadoop routine in Java error: Org.apache.hadoop.fs.LocalFileSystem cannot be cast to Org.apache. The code is as follows:
PackageCom.pcitc.hadoop;Importjava.io.IOException;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.hdfs.DistributedFileSystem;ImportOrg.apache.hadoop.hdfs.protocol.DatanodeInfo;/*** Get all node names on the HDFs cluster *@authorLenovo **/ Public classGetList { Public Static voidMain (string[] args)throwsIOException {Configuration conf=NewConfiguration (); Conf.set ("Dfs.default.name", "hdfs://hadoopmaster:9000"); FileSystem FS=filesystem.get (conf); Distributedfilesystem HDFs=(Distributedfilesystem) FS; Datanodeinfo[] Datanodestats=hdfs.getdatanodestats (); String[] Names=NewString[datanodestats.length]; for(inti = 0; i < datanodestats.length; i++) {Names[i]=Datanodestats[i].gethostname (); System.out.println ("Node" + i + "name" +Names[i]); } }}
After execution, the following error is reported:
Exception in thread "main" Java.lang.ClassCastException:org.apache.hadoop.fs.LocalFileSystem cannot is 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)
The reason is that both Distributedfilesystem and LocalFileSystem are subclasses of FileSystem, and Filesystem.get (conf) gets LocalFileSystem instance, This type should be the default, to get Distributedfilesystem, need to configure Conf object, according to my style I think it should be matched with the Conf object, but still save, finally in accordance with the online version of the corresponding changes can be. Directly on the modified code as follows (note the Red section):
PackageCom.pcitc.hadoop;Importjava.io.IOException;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.hdfs.DistributedFileSystem;ImportOrg.apache.hadoop.hdfs.protocol.DatanodeInfo;/*** Get all node names on the HDFS cluster * *@authorLenovo **/ Public classGetList { Public Static voidMain (string[] args)throwsIOException {Configuration conf=NewConfiguration (); //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=NewString[datanodestats.length]; for(inti = 0; i < datanodestats.length; i++) {Names[i]=Datanodestats[i].gethostname (); System.out.println ("node:" + i + ", Name:" + names[i]); } }}
Run Hadoop program in Java error: Org.apache.hadoop.fs.LocalFileSystem cannot be cast to Org.apache.