zoukankan      html  css  js  c++  java
  • 获取FileSystem

    	/**
         * 根据配置文件获取HDFS操作对象
         * 有两种方法:
         *  1.使用conf直接从本地获取配置文件创建HDFS对象
         *  2.多用于本地没有hadoop系统,但是可以远程访问。使用给定的URI和用户名,访问远程的配置文件,然后创建HDFS对象。
         * @return FileSystem
         */
    	public FileSystem getHadoopFileSystem1() {
            FileSystem fs = null;
            Configuration conf = null;
            // 方法一,本地有配置文件,直接获取配置文件(core-site.xml,hdfs-site.xml)
            // 根据配置文件创建HDFS对象
            // 此时必须指定hdsf的访问路径。
            conf = new Configuration();
            // 文件系统为必须设置的内容。其他配置参数可以自行设置,且优先级最高
            conf.set("fs.defaultFS", "hdfs://huabingood01:9000");
            try {
                // 根据配置文件创建HDFS对象
                fs = FileSystem.get(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fs;
        }
    	@Test
    	public FileSystem getHadoopFileSystem2() {
    		System.out.println("start...");
            FileSystem fs = null;
            Configuration conf = null;
            // 方法二:本地没有hadoop系统,但是可以远程访问。根据给定的URI和用户名,访问hdfs的配置参数
            // 此时的conf不需任何设置,只需读取远程的配置文件即可。
            conf = new Configuration();
            // Hadoop的用户名
            String hdfsUserName = "root";
    
            URI hdfsUri = null;
            try {
                // HDFS的访问路径
                hdfsUri = new URI("hdfs://master:9000");
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
    
            try {
                // 根据远程的NN节点,获取配置信息,创建HDFS对象
                fs = FileSystem.get(hdfsUri,conf,hdfsUserName);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return fs;
        }
        public FileSystem getHadoopFileSystem3() {
            FileSystem fs = null;
            Configuration conf = null;
            // 方法三,反正我们没有搞懂。
            conf  = new Configuration();
            conf.addResource("/opt/huabingood/pseudoDistributeHadoop/hadoop-2.6.0-cdh5.10.0/etc/hadoop/core-site.xml");
            conf.addResource("/opt/huabingood/pseudoDistributeHadoop/hadoop-2.6.0-cdh5.10.0/etc/hadoop/hdfs-site.xml");
            try {
                fs = FileSystem.get(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fs;
        }
    

      

  • 相关阅读:
    discuz登录流程解析(版本X3.2)
    利用AngularJs实现京东首页轮播图效果
    CC攻击原理及防范方法
    Session优缺点
    jQuery EasyUI教程之datagrid应用
    XSS攻击的解决方法
    DIV+CSS 命名规范
    30个你必须记住的CSS选择符
    导出虚拟机的OVF 模板
    k8s 常用命令
  • 原文地址:https://www.cnblogs.com/guoziyi/p/10278929.html
Copyright © 2011-2022 走看看