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(){
    
    
        }
    }
    

      

  • 相关阅读:
    Things You Should Know
    因为web.config配置,导致(当前不会命中断点,还没有为该文档加载任何符号)
    【HTML5 Canvas游戏开发】笔记(二) 显示一张图片
    【HTML5 Canvas游戏开发】笔记(一) 概述和基础讲解
    const char* pcr&char* const pcr
    【Python扩展阅读【转】EasyGui 学习文档【超详细中文版】】
    【Python扩展阅读【转】】字符串的方法及注释
    【Python⑥】python的缩进,条件判断和循环
    【Python⑤】python序列---list和tuple
    【Python④】python恼人的字符串,格式化输出
  • 原文地址:https://www.cnblogs.com/huyangyang/p/9321477.html
Copyright © 2011-2022 走看看