hdfs的java接口简单示例

public class HDFSDemo {
    
    private FileSystem fs = null;
    @Before
    public void init() throws IOException, URISyntaxException, InterruptedException{
        fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration());
    }
    @Test
    //递归删除文件或者文件夹
    public void  testDel() throws IllegalArgumentException, IOException{
        boolean flag = fs.delete(new Path("/test"),true);
        System.out.println(flag);
    }
    @Test
    //创建目录
    public void testMkdir() throws IllegalArgumentException, IOException{
        boolean flag = fs.mkdirs(new Path("/Google3Papers"));
        System.out.println(flag);
    }
    @Test
    //上传文件
    public void testUpload() throws IllegalArgumentException, IOException{
        FSDataOutputStream out = fs.create(new Path("/Google3Papers/Google-Bigtable1.0.pdf"));
        FileInputStream in = new FileInputStream(new File("/Users/fengmingyue/Desktop/Google-Bigtable1.0.pdf"));
        IOUtils.copyBytes(in, out, 2048, true);
    }
    @Test
    //下载文件
    public void testDownload() throws IllegalArgumentException, IOException{
        InputStream in = fs.open(new Path("/test"));
        FileOutputStream out = new FileOutputStream(new File("/Users/fengmingyue/Desktop/ttt"));
        IOUtils.copyBytes(in, out, 2048, true);
    }
}
原文地址:https://www.cnblogs.com/fengmingyue/p/6347578.html