hdfs基本操作

@Test
public void test1() {
    System.out.println(System.getenv("HADOOP_HOME"));
}


@Test
//创建文件夹
public void listFiles() throws IOException {
    Configuration conf = new Configuration();
    conf.set("fs.default.name", "hdfs://ip);
    FileSystem fs = null;
    fs = FileSystem.get(conf);
    boolean result = fs.mkdirs(new Path("/tt1"));
    System.out.println(result);
    fs.close();
}

@Test
//读取文件
public void reatTxt() throws IOException {
    String uri = "hdfs://ip/test/hdfs/a.txt";
    Configuration cfg = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), cfg);
    InputStream in = null;
    try {
        in = fs.open(new Path(uri));
        IOUtils.copyBytes(in, System.out, 4096, false);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        IOUtils.closeStream(in);
    }
}


@Test
//上传文件
public void putFile() throws IOException {
    //本地文件路径
    String local = "G:/hdfs/a.txt";
    String dest = "hdfs://ip/test/a.txt";
    InputStream in = new BufferedInputStream(new FileInputStream(local));
    Configuration cfg = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(dest), cfg);
    OutputStream out = fs.create(new Path(dest));
    IOUtils.copyBytes(in, out, 4096, true);
    fs.close();
    IOUtils.closeStream(in);
}

@Test
//上传本地目录
public void putFiles() throws IOException {
    String hdfsPath = "hdfs://ip/user1";
    String localPath = "G:/hdfs";
    Configuration conf = new Configuration();
    try {
        FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
        Path hdfs_path = new Path(hdfsPath);
        Path local_path = new Path(localPath);
        fs.copyFromLocalFile(local_path, hdfs_path);
        fs.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Test
//下载文件
public void getFile() throws IOException {
    String hdfsPath = "hdfs://ip/test/test.txt";
    String localPath = "G:/hdfs";
    Configuration conf = new Configuration();
    conf.set("hadoop.home.dir", "G:/hadoop-3.0.3");
    FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
    Path hdfs_path = new Path(hdfsPath);
    Path local_path = new Path(localPath);
    fs.copyToLocalFile(hdfs_path, local_path);
    fs.close();
}

@Test
//下载目录
public void getFiles() throws IOException {
    String hdfsPath = "hdfs://ip/test";
    String localPath = "G:/hdfs";
    Configuration conf = new Configuration();
    conf.set("hadoop.home.dir", "G:/hadoop-3.0.3");
    FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
    Path hdfs_path = new Path(hdfsPath);
    Path local_path = new Path(localPath);
    fs.copyToLocalFile(hdfs_path, local_path);
    fs.close();
}

@Test
//删除文件
public void deleteFile() throws IOException {
    String uri = "hdfs://ip/user1";
    Path path = new Path(uri);
    Configuration conf = new Configuration();
    try {
        FileSystem fs = path.getFileSystem(conf);
        //递归删除文件夹及文件夹下的文件
        boolean b = fs.delete(path, true);
        System.out.println(b);
        fs.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Test
//输出HDFS指定目录下的文件和子目录
public void run1() throws IOException {
    String uri = "hdfs://ip/tt1";
    Configuration cfg = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), cfg);
    Path path = new Path(uri);
    FileStatus[] fss = fs.listStatus(path);
    for (FileStatus f : fss) {
        if (f.isFile())
            System.out.println("File:" + f.getPath().toString());
        else
            System.out.println("Dir:" + f.getPath().toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值