zoukankan      html  css  js  c++  java
  • 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(){
    
    
        }
    }
    

      

  • 相关阅读:
    奖券数目
    用jQuery和ajax实现搜索框文字自动补全功能
    简单的文件上传功能实现(java)
    示例演示公告通知标题无缝向上滚动,文字段落无缝向上滚动,简单的wangeditor富文本编辑器,简单的音乐播放demo
    SSM框架中注解含义及应用场景小结
    数据库优化以及SQL优化小结
    javaWEB中前后台乱码解决问题小结
    线程同步的方法
    MySQL的简单使用
    springMVC运行流程图及运行原理
  • 原文地址:https://www.cnblogs.com/huyangyang/p/9321477.html
Copyright © 2011-2022 走看看