美文网首页
Java代码操作HDFS

Java代码操作HDFS

作者: 虫儿飞ZLEI | 来源:发表于2019-07-14 19:28 被阅读0次

    1. 创建文件夹示例

    1.1 环境准备

    java

    hadoop
    解压hadoop到本地,配置环境变量

    image.png

    1.2 新建maven项目

    引入依赖:

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>2.7.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>2.7.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-hdfs</artifactId>
                <version>2.7.2</version>
            </dependency>
            <dependency>
                <groupId>jdk.tools</groupId>
                <artifactId>jdk.tools</artifactId>
                <version>1.8</version>
                <scope>system</scope>
                <systemPath>C:\SoftWare\Java\jdk1.8.0_211\lib\tools.jar</systemPath>
            </dependency>
        </dependencies>
    

    编写客户端代码:

        @Test
        public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
    
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 创建目录
            fs.mkdirs(new Path("/ff/qq/qq"));
    
            // 3 关闭资源
            fs.close();
        }
    

    more:

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class HdfsClient{
        /**
         * 创建文件夹
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
    
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 创建目录
            fs.mkdirs(new Path("/ff/qq/qq"));
    
            // 3 关闭资源
            fs.close();
        }
    
        /**
         * 从本地上传文件
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
            configuration.set("dfs.replication", "2");
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 上传文件
            fs.copyFromLocalFile(new Path("C:\\Users\\zl\\Desktop\\banzhang.txt"), new Path("/banzhang.txt"));
    
            // 3 关闭资源
            fs.close();
    
            System.out.println("over");
        }
    
        /**
         * 下载文件到本地
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 执行下载操作
            // boolean delSrc 指是否将原文件删除
            // Path src 指要下载的文件路径
            // Path dst 指将文件下载到的路径
            // boolean useRawLocalFileSystem 是否开启文件校验
            fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("C:\\Users\\zl\\Desktop\\banzhang2.txt"), true);
    
            // 3 关闭资源
            fs.close();
        }
    
        /**
         * 删除文件夹
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testDelete() throws IOException, InterruptedException, URISyntaxException{
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 执行删除
            fs.delete(new Path("/test"), true);
    
            // 3 关闭资源
            fs.close();
        }
    
        /**
         * 重命名文件
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testRename() throws IOException, InterruptedException, URISyntaxException{
    
            // 1 获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 修改文件名称
            fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));
    
            // 3 关闭资源
            fs.close();
        }
    
    
        /**
         * 列出文件信息
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
    
            // 1获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 获取文件详情
            RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
    
            while(listFiles.hasNext()){
                LocatedFileStatus status = listFiles.next();
    
                // 输出详情
                // 文件名称
                System.out.println(status.getPath().getName());
                // 长度
                System.out.println(status.getLen());
                // 权限
                System.out.println(status.getPermission());
                // 分组
                System.out.println(status.getGroup());
    
                // 获取存储的块信息
                BlockLocation[] blockLocations = status.getBlockLocations();
    
                for (BlockLocation blockLocation : blockLocations) {
    
                    // 获取块存储的主机节点
                    String[] hosts = blockLocation.getHosts();
    
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                }
    
                System.out.println("-----------分割线----------");
            }
    
            // 3 关闭资源
            fs.close();
        }
    
        /**
         * 判断文件是文件还是 文件夹
         * @throws IOException
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        @Test
        public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
    
            // 1 获取文件配置信息
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");
    
            // 2 判断是文件还是文件夹
            FileStatus[] listStatus = fs.listStatus(new Path("/"));
    
            for (FileStatus fileStatus : listStatus) {
    
                // 如果是文件
                if (fileStatus.isFile()) {
                    System.out.println("f:"+fileStatus.getPath().getName());
                }else {
                    System.out.println("d:"+fileStatus.getPath().getName());
                }
            }
    
            // 3 关闭资源
            fs.close();
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Java代码操作HDFS

          本文链接:https://www.haomeiwen.com/subject/iuuikctx.html