黑猴子的家:API 操作 HDFS 文件上传

1、Code -> GitHub

https://github.com/liufengji/hadoop_hdfs.git

2、Code

    @Test
    public void putFileToHDFS() throws Exception{
        // 1 创建配置信息对象
        // new Configuration();的时候,它就会去加载jar包中的hdfs-default.xml
        // 然后再加载classpath下的hdfs-site.xml
        Configuration configuration = new Configuration();

        // 2 设置参数 
        // 参数优先级: 1、客户端代码中设置的值  2、classpath下的用户自定义配置文件 3、服务器的默认配置
        configuration.set("dfs.replication", "2");

        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9000"),configuration, "victor");
        
        // 3 创建要上传文件所在的本地路径
        Path src = new Path("e:/hello.txt");
        
        // 4 创建要上传到hdfs的目标路径
        Path dst = new Path("hdfs://node1:9000/user/victor/hello.txt");
        
        // 5 拷贝文件
        fs.copyFromLocalFile(src, dst);

        // 6 关闭资源
        fs.close(); 
}

3、将core-site.xml拷贝到项目的根目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>

    <!-- 指定HDFS中NameNode的地址 -->
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://node1:9000</value>
    </property>

</configuration>

4、将hdfs-site.xml拷贝到项目的根目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>

    <property>
        <name>dfs.replication</name>
         <value>1</value>
    </property> 

</configuration>

5、测试参数优先级

优先级:客户端代码中设置的值 > classpath下的用户自定义配置文件 > 服务器的默认配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值