• Intellij IDEA 创建maven项目,利用API操作HDFS


    首先,新建Maven项目,添加相应的依赖,新建Java类:

    package com.young;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.util.Progressable;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.io.*;
    import java.net.URI;
    
    public class HDFSTest {
        Configuration configuration;
        FileSystem fileSystem;
        String HDFS_PATH = "hdfs://192.168.131.142:9000";
        @Before
        public void before() throws Exception{
            configuration = new Configuration();
            fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,"root");
    
        }
        @Test
        public void mkdir() throws Exception{//新建文件夹
            boolean result = fileSystem.mkdirs(new Path("/idea"));
            System.out.print(result);
        }
        @Test
        public void write() throws Exception {//新建文件
            FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/idea/hello.txt"));
            fsDataOutputStream.writeBytes("hello IDEA!");
            fsDataOutputStream.close();
        }
        @Test
        public void read() throws Exception {//查看HDFS上的文件
            FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/idea/hello.txt"));
            InputStreamReader isr = new InputStreamReader(fsDataInputStream);//创建输入流
            BufferedReader br = new BufferedReader(isr);//按行读取
            String str = br.readLine();
            while(str!=null)
            {
                System.out.println(str);
                str = br.readLine();
            }
            br.close();
            isr.close();
            fsDataInputStream.close();
        }
        @Test
        public void upload() throws Exception{//上传文件到HDFS
            fileSystem.copyFromLocalFile(new Path("D:/test/abc.txt"),new Path("/idea"));
            fileSystem.close();
        }
        @Test
        public void rename() throws Exception{//文件重命名
            fileSystem.rename(new Path("/idea/hello.txt"),new Path("/idea/h.txt"));
            fileSystem.close();
        }
        @Test
        public void download() throws Exception{//从HDFS下载文件
            fileSystem.copyToLocalFile(new Path("/idea/h.txt"),new Path("D:/test"));
            fileSystem.close();
        }
        @Test
        public void query() throws Exception{//查询某目录下所有文件
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
            for(int i=0;i<fileStatuses.length;i++)
            {
                System.out.println(fileStatuses[i].getPath().toString());
            }
            fileSystem.close();
        }
        @Test
        public void delete() throws Exception{//删除文件
            fileSystem.delete(new Path("/idea/h.txt"),true);
            fileSystem.close();
    
        }
        @After
        public void destroy(){
    
    
        }
    }
    

      

  • 相关阅读:
    UrlRewriter配置IIS支持伪静态
    Linux 安装PAE内核
    Tmux入门教程
    Tmux与Oh-my-zsh环境整合
    MySQL Route负载均衡与读写分离Docker环境使用
    MySQL数据表的基本操作
    Git安全配置
    GitLab使用自定义端口
    Gitlab搭建安装及使用中遇到的问题。
    执行Docker命令报错解决办法
  • 原文地址:https://www.cnblogs.com/huyangyang/p/9321477.html
Copyright © 2020-2023  润新知