Four ways to get FileSystem

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class fileSystem {
	public static void main(String [] args) throws IOException, URISyntaxException {
		filedemo1();
		filedemo2();
		filedemo3();
		filedemo4();
		
	}
	public static void filedemo1() throws IOException {
		Configuration conf = new Configuration();//创建Configuration对象
		conf.set("fs.defaultFs,","hdfs://Master:9000");//指定获取文件类型:分布式还是本机
		FileSystem file = FileSystem.get(conf);//获取指定文件系统
		System.out.println(file);
			
	}
	public static void filedemo2() throws IOException, URISyntaxException {

		FileSystem file = FileSystem.get(new URI("hdfs://Master:9000"),new Configuration());//推荐使用这种方式
		System.out.println(file);
			
	}
	public static void filedemo3() throws IOException, URISyntaxException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFs,","hdfs://Master:9000");
		FileSystem file = FileSystem.newInstance(conf);//通过创建FilsSystem实例
		System.out.println(file);
	}
	public static void filedemo4() throws IOException, URISyntaxException {

		FileSystem file = FileSystem.newInstance(new URI("hdfs://Master:9000"),new Configuration());
		System.out.println(file);
			
	}
}

operation result
Insert picture description here

Guess you like

Origin blog.csdn.net/NewDay_/article/details/108847318