您的位置:首页 > 编程语言 > Java开发

java.lang.IllegalArgumentException: Wrong FS://expected: file:///

2017-08-08 18:20 1456 查看


有错误提示file:///的提示信息是想需要本地文件系统(file:///为本地文件系统标识),代码无法识别HDFS文件系统,需要在Configuration中配置相应的参数。

如何来配置Configuration参数?

使用hadoop-2.6.0(因为我用的是apache-hadoop-2.6.0版本)的core-site.xml(/HADOOP_HOME/etc/hadoop/)的fs.defaultFS参数:



fs.defaultFS的vaue为hdfs://hadoopmaster:9000   在这里hadoopmaster为承载hadoop-2.6.0的linux系统(centOS-7.2)主机名称(hostname),

在centOS-7.2系统下设置hostname与IP映射:



所以,在此hostname与ip(192.168.184.99)是等价的

言归正传!!!

在程序中,把core-site.xml的fs.defaultFS参数配置给Configuration即可。



修改后的程序:

package org.hadoop.hdfs;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class ExeHDFS {

private String hdfsPath = "hdfs://192.168.184.99:9000/haichuan/";

public static void main(String[] args) throws Exception {

ExeHDFS exeHDFS = new ExeHDFS();
exeHDFS.testCreate();
}

// 创建HDFS文件
public void testCreate() throws Exception {

Configuration conf = new Configuration();
//使用hadoop的core-site.xml中的fs.defaultFS参数,防止Wrong FS: hdfs:.......file:///  错误异常
conf.set("fs.defaultFS", "hdfs://192.168.184.99:9000");

byte[] buff = "hello world!".getBytes();
FileSystem hdfs = FileSystem.get(conf);
Path dst = new Path(hdfsPath + "hello.txt");
FSDataOutputStream outputStream = null;
try {
outputStream = hdfs.create(dst);
outputStream.write(buff, 0, buff.length);
} catch (Exception e) {
e.printStackTrace();

} finally {
if (outputStream != null) {
outputStream.close();
}
}

FileStatus files[] = hdfs.listStatus(dst);
for (FileStatus file : files) {
System.out.println(file.getPath());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdfs
相关文章推荐