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

用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.的更多相关文章

  1. eclipse运行hadoop程序报错:Connection refused: no further information

    eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...

  2. Window7中Eclipse运行MapReduce程序报错的问题

    按照文档:http://www.micmiu.com/bigdata/hadoop/hadoop2x-eclipse-mapreduce-demo/安装配置好Eclipse后,运行WordCount程 ...

  3. eclipse 运行 mapreduce程序报错 No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).

    报错信息 17/07/06 17:00:27 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Ap ...

  4. eclipse cdt运行c程序报错“launch failed,binary not found”

    1. 安装了eclipsecdt版 2. 采用mingw 编译第一个c程序,报错“launch failed,binary not found”.检查是mingw下的bin目录在环境变量里设置错了. ...

  5. 在VC++空工程中使用MFC类,采用Unicode字符集后,运行工程程序报错的解决方案

    创建一个VC++空工程,将Project Properties->General->Use of MFC改为Use MFC in a Shared DLL 新建一个源文件,内容如下 #in ...

  6. 运行ASP程序报错

    错误提示: An error occurred on the server when processing the URL. Please contact the system administrat ...

  7. Flash Builder4.0运行应用程序报错

    1.错误描述 SecurityError: Error #2148: SWF 文件 file:///D:/Adobe Flash Builder 4 Installer/HVBox/bin-debug ...

  8. debug运行java程序报错

    debug运行java程序报错 ERROR: transport error 202: connect failed: Connection timed out ERROR: JDWP Transpo ...

  9. eclipse下执行maprdeuc程序报错 java.lang.ClassNotFoundException

    最近遇到一个问题,不知怎么突然运行hadoop的map程序报错,困扰了我很久,现在来给大家分享分享.. 错误信息 2017-05-18 21:34:22,104 INFO [main] client. ...

随机推荐

  1. iOS的沙箱目录和文件操作

    一.沙箱 iOS的每一个应用程序都有自己的目录来存放数据,这个目录称为沙箱目录.沙箱目录是一种数据安全策略,它设计的原理是只能允许自己的应用访问目录,而不允许其他的应用访问,这样可以保证数据的安全,应 ...

  2. 字符串转与ASCII码之间的互换

    1.字符串转换成ASCII码 public static String stringToAscii(String value) { StringBuffer sbu = new StringBuffe ...

  3. jquery实现抽奖转盘

    用jquery通过配置参数实现抽奖转盘 1.html代码 <!DOCTYPE html> <html lang="zh-CN"> <head> ...

  4. C++中执行windows指令

    执行windows指令: BOOL ExecDosCmd(]) { SECURITY_ATTRIBUTES sa; HANDLE hRead,hWrite; sa.nLength = sizeof(S ...

  5. TextBox 绑定到DataTable某一列属性上

    将TextBox绑定到DataTable某一列属性上 DataTable dt = GetDataTable() textBox1.DataBindings.Add("Text", ...

  6. JS远程获取网页源代码的例子

    js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...

  7. CentOS6.5 MySQL 配置设置总结笔记

    三.登录MySQL 登录MySQL的命令是mysql, mysql 的使用语法如下:  mysql [-u username] [-h host] [-p[password]] [dbname]  u ...

  8. Android类库常用类库一览

    在Android SDK中包括很多包文件,通过了解这些包的功能也有助于了解可以开发的功能. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: android.app ...

  9. mac 下 sphinx + mysql + php 实现全文搜索(xampp)(3)sphinx 的配置项解析

    source:数据的来源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一个字典检索.有了整本字典内容以后,才会有字典检索. searchd:提供搜 ...

  10. ENVI/IDL中.sav和.pro文件用法的不同

    软件envi5.0 sp3 .sav是IDL binaryfile。 安装方法:把ENVIProgramGenerator.sav文件拷贝到…\Exelis\ENVI50\extensions\文件夹 ...