用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. Objective-c中的对象间的消息传递以及消息路由

    刚开始使用Objective-C时,总是习惯将对象间发送消息之间称呼为方法调用.心想,这和c#不是一回事吗?不就是调用实例方法吗,还搞个消息发送作甚,最后还不是要转化为方法的调用?通过一段时间的理解学 ...

  2. javascript——base64

    <!DOCTYPE html> <head> <title>欢迎</title> <meta charset="utf-8"& ...

  3. const define 定义常量的区别

    1.用const定义常量在编译的时候,提供了类型安全检查,而define 只是简单地进行字符串的替换 2.const定义的常量,会分配相应的内存空间.而define没有分配空间,只是在程序中与处理的时 ...

  4. Linux ed

    ed 编辑器是 Linux 操作系统下最简单的文本编辑器.它是以行为单位对文件进行编辑的编辑器,而不像 MS-DOS 系统下的 edit 那样是以整个屏幕框架为单位对文件进行编辑的.因此,如果你已经习 ...

  5. 【Winform】 Enum逆向解析

    将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象.   名称 说明   Parse(Type, String) 将一个或多个枚举常数 ...

  6. Winform 下拉框绑定问题

    在Winform中下拉框绑定的时候只能读到text属性值,Id的值不管怎么搞都读取不到,所以就百度找到了一种方式: public void CmdBind() { var data = _logic. ...

  7. 给虚拟机中的CentOS7配置固定ip

    在虚拟机中安装完了CentOS7之后,使用了DHCP来获取ip,vmware的网络连接使用了NAT模式.但是在把Linux设置为固定ip地址后,虚拟机里的linux可以ping通全网段的ip地址,但是 ...

  8. SPI协议及其工作原理详解

    一.概述. SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控 ...

  9. 支持异步通知的globalfifo平台设备驱动程序及其测试代码

    驱动: #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #incl ...

  10. Beaglebone Back学习一(开发板介绍)

    随着开源软件的盛行.成熟,开源硬件也迎来了春天,先有Arduino,后有Raspherry Pi,到当前的Beaglebone .相信在不久的将来,开源项目将越来越多,越来越走向成熟.         ...