hadoop_java操作hdfs

导入lib包

public class HDFSDemo {

  FileSystem fs = null;

   @Before

  public void init() throws Exception{

    fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration(), "root");

  }

  @Test

  public void testUpload() throws Exception{

    InputStream in = new FileInputStream("/root/install.log");

    OutputStream out = fs.create(new Path("/log123.log")); IOUtils.copyBytes(in, out, 1024, true);

  }

  @Test

  public void testMkdir() throws IllegalArgumentException, IOException{

    boolean flag = fs.mkdirs(new Path("/a/aa")); System.out.println(flag);

  }

  @Test

  public void testDel() throws IllegalArgumentException, IOException{

    boolean flag = fs.delete(new Path("/a"), true); System.out.println(flag);

  }

  public static void main(String[] args) throws Exception {

    FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());

    InputStream in = fs.open(new Path("/jdk")); OutputStream out = new FileOutputStream("/home/jdk1.7.tar.gz");

    IOUtils.copyBytes(in, out, 4096, true);

  }

}

posted @ 2022-04-24 14:37  三重丶刘德华  阅读(32)  评论(0)    收藏  举报