java调用hadoop接口,实现文件的上传、下载、删除、新建文件夹

hadoop的安装参照:https://blog.csdn.net/u010476739/article/details/86647585
hadoop的dfs命令参照:https://blog.csdn.net/u010476739/article/details/86686725

  1. 打开eclipse 新建工程demo
    将hadoop安装包下的jar文件加入buildpath

hadoop-3.2.0\share\hadoop\common\*.jar
hadoop-3.2.0\share\hadoop\common\lib\*.jar
hadoop-3.2.0\share\hadoop\hdfs\*.jar
hadoop-3.2.0\share\hadoop\hdfs\lib\*.jar

  1. 新建类demo
package demo;

import java.io.FileInputStream;
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 Demo {

	//文件的下载
	public static void main1(String[] args) throws Exception {
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.114.134:9000"), new Configuration());
		InputStream in = fs.open(new Path("/user/root/demo/ok"));
		OutputStream out = new FileOutputStream("c:/ok");
		IOUtils.copyBytes(in, out, 4096, true);
		System.out.println("ok");
	}

	//文件的上传
	public static void main2(String[] args) throws Exception {
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.114.134:9000"), new Configuration(), "root");
		InputStream in = new FileInputStream("F:\\testjar\\common\\hadoop-common-3.2.0.jar");
		OutputStream out = fs.create(new Path("/user/root/test-hadoop-jar"));
		IOUtils.copyBytes(in, out, 4096, true);
		System.out.println("ok");
	}

	//文件的删除
	public static void main3(String[] args) throws Exception {
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.114.134:9000"), new Configuration(), "root");
		//delete的第二个参数表示是否递归删除,遇到删除文件夹时设为true
		Boolean flag = fs.delete(new Path("hdfs://192.168.114.134:9000/user/root/test-hadoop-jar"),true);
		if (flag) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
		System.out.println("ok");
	}
	
	//新建文件夹
	public static void main(String[] args) throws Exception {
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.114.134:9000"), new Configuration(), "root");
		Boolean flag = fs.mkdirs(new Path("demo22"));
		if (flag) {
			System.out.println("创建目录成功");
		} else {
			System.out.println("创建目录失败");
		}
		System.out.println("ok");
	}
}
  1. 依次运行每个main方法,运行完后查看hdfs上的目录内容
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值