Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for sc
问题描述
java代码,打包到虚拟机,希望运行后在HDFS创建一个文件夹 /java。代码如下:
public class HdfsStart {
private FileSystem fs;
// 初始化
public void init() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
fs = FileSystem.get(new URI("hdfs://localhost:9000"),conf);
}
// 创建文件夹
public void testMkdir() throws IOException {
boolean flag = fs.mkdirs(new Path("/java"));
System.out.println(flag);
}
/**
* 创建成功则会输出true
* @param args
* @throws InterruptedException
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException {
HdfsStart hdfsStart = new HdfsStart();
hdfsStart.init();
hdfsStart.testMkdir();
}
}
当然,虚拟机系统上也启动了hdfs服务(sbin/start-dfs.sh)
打包上传后运行方法为:
$ java -jar hadoop-demo.jar
报错如下:
Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:3281)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3301)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:124)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:3352)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:3320)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:479)
at cn.smileyan.hadoop.hdfs.HdfsStart.init(HdfsStart.java:20)
at cn.smileyan.hadoop.hdfs.HdfsStart.main(HdfsStart.java:38)
解决方法
运行时改用命令如下:
$ bin/hadoop jar hadoop-demo.jar