import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.net.URI;
/**
* @Author SunBingRui
* @Date 2020/7/7 16:27
*/
public class TestHDFS {
@Test
public void testMkdir() throws Exception {
Configuration conf = new Configuration();
// 在shell中创建目录:hadoop fs -mkdir /xxx
//FileSystem fs = FileSystem.get(conf);//这种方式需要准备好core-site.xml文件
//即创建一个客户端对象 ,调用创建目录的方法,路径作为方法的参数掺入
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"),conf,"sun");
//URI不能为null,由于有了URI,core-site.xml文件则不需要,需要声明用户身份
System.out.println(fs.getClass().getName());
fs.mkdirs(new Path("/testHDFS"));//在HDFS的根目录下创建一个testHDFS目录
fs.close();
}
}
FileSystem
: 文件系统的抽象基类
FileSystem的实现取决于fs.defaultFS
的配置!
有两种实现:
LocalFileSystem
: 本地文件系统 fs.defaultFS=file:///
DistributedFileSystem
: 分布式文件系统 fs.defaultFS=hdfs://xxx:9000
声明用户身份:
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"), conf, "username");
Configuration
: 功能是读取配置文件中的参数
Configuration在读取配置文件的参数时,根据文件名,从类路径按照顺序读取配置文件!
先读取 xxx-default.xml,再读取xxx-site.xml
Configuration类一加载,就会默认读取8个配置文件!
将8个配置文件中所有属性,读取到一个Map集合中!
也提供了set(name,value),来手动设置用户自定义的参数!
原文:https://www.cnblogs.com/sunbr/p/13262325.html