Java代码对HDFS基本操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

/**
 * @ClassName:HDFSClientTest
 * @author: zhengkw
 * @description: HDFSClientAPI练习
 * @date: 20/02/21下午 10:11
 * @version:1.0
 * @since: jdk 1.8
 */
public class HDFSClientTest {
    FileSystem fileSystem;
    Configuration configuration;

    @After
    /**
     * @descrption:回收资源
     * @return: void
     * @date: 20/02/21 下午 10:18
     * @author: zhengkw
     */
    public void close() throws IOException {
        fileSystem.close();
    }

    @Before
    /**
     * @descrption:初始化fs对象
     * @return: void
     * @date: 20/02/21 下午 10:19
     * @author: zhengkw
     */
    public void init() throws URISyntaxException, IOException, InterruptedException {
        configuration = new Configuration();
        fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
    }


    /**
     * @descrption: 副本数重置 HDFS文件上传(测试参数优先级)
     * @return: void
     * @date: 20/02/21 下午 10:29
     * @author: zhengkw
     */

    @Test
    public void fileUpLoad() throws IOException {
        //set replication=5 oldVal=3
        configuration.set("dfs.replication", "5");
        fileSystem.copyFromLocalFile(new Path("E:/123.txt"), new Path("/sanguo/"));
    }

    /**
     * @descrption: HDFS文件下载
     * @return: void
     * @date: 20/02/21 下午 10:32
     * @author: zhengkw
     */
    @Test
    public void fileDownLoad() throws IOException {
        fileSystem.copyToLocalFile(new Path("/sanguo/zhengkw.txt"), new Path("F:/wozuishuai.txt"));
    }

    //    HDFS文件夹删除

    /**
     * @descrption: HDFS文件名更改
     * @return: void
     * @date: 20/02/21 下午 10:37
     * @author: zhengkw
     */
    @Test
    public void deleteDir() throws IOException {
        fileSystem.delete(new Path("/zhengkw"), true);
    }


    /**
     * @descrption:HDFS文件详情查看(遍历)
     * @return: void
     * @date: 20/02/21 下午 11:04
     * @author: zhengkw
     */
    @Test
    public void showFileDescrption() throws IOException {
        RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/zhengkw/"), true);
        //遍历迭代器
        while (iterator.hasNext()) {
            LocatedFileStatus fileStatus = iterator.next();
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation bk : blockLocations
            ) {
                String[] hosts = bk.getHosts();
                for (String host : hosts
                ) {
                    System.out.println("host:" + host);

                }
                long length = bk.getLength();
                String[] names = bk.getNames();
                for (String name : names
                ) {
                    System.out.println("datanode:" + name);
                }
                System.out.println("length" + length);

                System.out.println("-----------------------------");

            }
            String group = fileStatus.getGroup();
            Path path = fileStatus.getPath();
            long len = fileStatus.getLen();
            short replication = fileStatus.getReplication();
            System.out.println("group:" + group);
            System.out.println("path :" + path);
            System.out.println("len:" + len);
            System.out.println("replication:" + replication);


        }
    }


    /**
     * @descrption: HDFS文件和文件夹判断
     * @return: void
     * @date: 20/02/21 下午 11:15
     * @author: zhengkw
     */
    @Test
    public void judgeFileOrDirectory() throws IOException {
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/zhengkw/hadoop/capacity-scheduler.xml"));
        if (fileStatus.isFile()) {
            System.out.println("f:" + fileStatus.getPath().getName());
        } else {
            System.out.println("d:" + fileStatus.getPath().getName());
        }

     /*   Path path = fileStatus.getPath();
        long blockSize = fileStatus.getBlockSize();
        long len = fileStatus.getLen();
        FsPermission permission = fileStatus.getPermission();
        short replication = fileStatus.getReplication();

        System.out.println("path:" + path);
        System.out.println("blockSize:" + blockSize);
        System.out.println("len:" + len);
        System.out.println("permission:" + permission);
        System.out.println("replication:" + replication);*/


    }

}

配置文件路径
HadoopClient\src\main\resources\hdfs-site.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>

    <!-- 指定副本数量5 ,集群配置文件内定义3-->
    <property>
        <name>dfs.replication</name>
        <value>5</value>
    </property>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值