Hadoop 配置的优先级

发布时间 2023-12-11 13:14:39作者: SpringCore

从低到高

1.默认配置

默认文件 文件存放在Hadoop的jar包中的位置
core-default.xml hadoop-common-3.3.6.jar/core-default.xml
hdfs-default.xml hadoop-hdfs-3.3.6.jar/hdfs-default.xml
yarn-default.xml hadoop-yarn-common-3.3.6.jar/yarn-default.xml
mapred-default.xml hadoop-mapreduce-client-core-3.3.6.jar/mapred-default.xml

2.自定义配置文件

core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml 四个配置存放在$HADOOP_HOME/etc/hadoop这个路径上,用户可以根据项目需求重新进行修改配置。

3.调用API程序的资源目录下的配置文件

core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml 四个配置存放在项目的src/main/resources/ 路径下

4.调用API程序的配置类

package cn.coreqi.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

/**
 * 客户端代码步骤
 * 1.首先获取一个客户端对象
 * 2.执行相关的操作命令
 * 3.关闭资源
 */
public class HdfsClient {

    /**
     * 客户端对象
     */
    private FileSystem fs;

    /**
     * 初始化客户端对象
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Before
    public void init() throws IOException, InterruptedException, URISyntaxException {
        // 需要连接的Hadoop NameNode地址
        URI uri = new URI("hdfs://192.168.58.130:8020");
        // 创建一个配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","7");   //设置默认副本数量为7
        // 用户
        String user = "root";
        // 获取客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }

    /**
     * 释放客户端资源
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        // 关闭资源
        fs.close();
    }

    /**
     * 创建目录
     * @throws IOException
     */
    @Test
    public void testMkdirs() throws IOException{
        // 创建一个文件夹
        fs.mkdirs(new Path("/coreqi"));
    }

    @Test
    public void testPut() throws IOException {
        // 参数解析
        // 参数一,表示是否删除源数据
        // 参数二,是否允许覆盖
        // 参数三,源数据路径
        // 参数四,目标路径
        //fs.copyFromLocalFile(false,false,new Path("C:\\Users\\fanqi\\Desktop\\bda6f29e-29de-4b9d-aa01-8c0fa850f99b.webp"),new Path("\\coreqi\\1.webp"));
        fs.copyFromLocalFile(false,false,new Path("C:\\Users\\fanqi\\Desktop\\bda6f29e-29de-4b9d-aa01-8c0fa850f99b.webp"),new Path("hdfs://192.168.58.130/coreqi/2.webp"));
    }
}