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上的目录内容

你可能感兴趣的:(hadoop)