hadoop中conf.set("fsxxxx",hdfs)的作用

hadoop中conf.set(“fsxxxx”,hdfs)的作用

为什么有时候不需要加conf.set设置configuration的参数?

解答:
因为进行文件操作的时候需要知道在哪个文件系统进行操作。


可以不设置
就需要将hadoop安装目录下的etc/hadoop/core-site.xml放到工程目录的bin目录下,eclipse才能加载。


另一种解决办法就是在程序里面设置好
举个创建目录的例子

a:将文件系统的地址放在url中,通过path.getFileSystem获得文件系统

这种太过于混合了,万一要操作其他地址的文件系统,就麻烦了

 public static void main(String[] args) throws IOException {
    	
        Configuration conf= new Configuration();
        String url="hdfs://0.0.0.0:8020/test";
        Path path_dir=new Path(url);
        
        FileSystem fs=path_dir.getFileSystem(conf);
        boolean result=fs.mkdirs(path_dir);
        if(result) {
            System.out.println("success!");
        }else {
            System.out.println("failed");
        }
    }

b.conf先设置好文件系统的地址与操作的path_dir分离

public static void main(String[] args) throws IOException {
    	
        Configuration conf= new Configuration();
        conf.set("fs.defaultFS","hdfs://0.0.0.0:8020");
        FileSystem fs=FileSystem.get(conf);
  
        String uri="hdfs://0.0.0.0:8020/test";
        Path path_dir=new Path(uri);
        
        boolean result=fs.mkdirs(path_dir);
        
        if(result) {
            System.out.println("success!");
        }else {
            System.out.println("failed");
        }

猜你喜欢

转载自blog.csdn.net/weixin_41487978/article/details/105275821