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


    }
}

  

posted @ 2018-07-17 08:53  YOUNG111  阅读(930)  评论(0编辑  收藏  举报