Run Hadoop program in Java error: Org.apache.hadoop.fs.LocalFileSystem cannot be cast to Org.apache.

Source: Internet
Author: User
Tags lenovo

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.