Java代码操作HDFS(在/user/root/下面創建目錄)

时间:2021-05-12 06:40:32

1.创建HDFS目录并打成jar包

 package Hdfs;

 import java.io.IOException;
import java.net.URI; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 创建HDFS目录
* @author SimonsZhao
*
*/
public class CreateDir {
public static void main(String[] args) {
String uri = "hdfs://neusoft-master:9000/user/root/test1";
Configuration conf = new Configuration();
try {
FileSystem fs = FileSystem.get(URI.create(uri),conf);
Path dfs = new Path("hdfs://neusoft-master:9000/user/root/test1");
fs.mkdirs(dfs);
} catch (IOException e) {
e.printStackTrace();
}
}
}

JAVA代码创建HDFS目录

2.执行主文件

[root@neusoft-master hadoop]# hadoop jar /usr/local/software/JarCreateDir.jar

Java代码操作HDFS(在/user/root/下面創建目錄)

3.查看HDFS中是否存在创建的test1文件

[root@neusoft-master hadoop]# hadoop dfs -ls /user/root/

Java代码操作HDFS(在/user/root/下面創建目錄)