美文网首页玩转大数据大数据大数据 爬虫Python AI Sql
hadoop JAVA API 创建,删除,上传,下载

hadoop JAVA API 创建,删除,上传,下载

作者: Java程序员YY | 来源:发表于2019-08-14 11:26 被阅读0次

    Mkdir.java:创建

    import java.net.URI;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    public class Mkdir {

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

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

    Boolean flag = fs.mkdirs(new Path("/aa"));

    if (flag) {

    System.out.println("创建目录成功");

    } else {

    System.out.println("创建目录失败");

    }

    System.out.println("ok");

    }

    }

    Delete.java:删除

    import java.net.URI;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    public class Delete {

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

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

    Boolean flag = fs.delete(new Path("hdfs://192.168.205.129:9000/aa"), true);

    if (flag) {

    System.out.println("删除成功");

    } else {

    System.out.println("删除失败");

    }

    }

    }

    Upload.java:上传

    import java.io.FileInputStream;

    import java.io.InputStream;

    import java.io.OutputStream;

    import java.net.URI;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    import org.apache.hadoop.io.IOUtils;

    public class Upload {

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

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

    InputStream in = new FileInputStream("/home/hong/test.txt");

    OutputStream out = fs.create(new Path("/aa/test1.txt"));

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

    System.out.println("上传成功。。。");

    }

    }

    Download.java:下载

    import java.io.FileOutputStream;

    import java.io.InputStream;

    import java.io.OutputStream;

    import java.net.URI;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    import org.apache.hadoop.io.IOUtils;

    public class Download {

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

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

    InputStream in = fs.open(new Path("/aa/test.txt"));

    OutputStream out = new FileOutputStream("/home/hong/111.txt");

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

    System.out.println("下载成功。。。");

    }

    }

    最新免费java,架构,大数据AI编程资料获取添加

    薇信:18410263200

    通过验证填写“111”(备注必填)

    相关文章

      网友评论

        本文标题:hadoop JAVA API 创建,删除,上传,下载

        本文链接:https://www.haomeiwen.com/subject/sbfgjctx.html