配置文件优先级问题
配置文件分为好多种,分别是服务端配置文件,服务端默认配置文件,代码配置文件,api
端配置文件
package hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class HDFSConfigurationTest {
private FileSystem fileSystem;
/**
* 如歌我们不设置 {@code configuration} 也可以通过 hadoop 系统默认的读取配置文件的机制来实现配置
* 它默认会读取 {@file hdfs-site.xml} 我们可以配置
* <br>
* 注意:
* JAVA 代码操作
* 代码的优先级 > 配置文件的优先级 > 服务器端的配置文件(xxx-default.xml)优先级
* 在这里服务器端配置文件(xxx-site.xml)是不起作用的只有在 hadoop shell 操作的时候服务器端配置文件(xxx-site.xml)才会起作用
* Hadoop Shell 操作
* 服务器端配置文件(xxx-site.xml) > 服务器端默认配置文件(xxx-default.xml)
*/
@Before
public void init() throws IOException, URISyntaxException, InterruptedException {
Configuration configuration = new Configuration();
// configuration.set("dfs.replication", "6");
fileSystem = FileSystem.get(new URI("hdfs://192.168.11.132:8020"), configuration, "xing");
}
/**
* 类似于
* <code>
* hdfs dfs -mkdir /replication
* hadoop fs -copyFromLocal url /replication
* 或者
* hdfs dfs -copyFromLocal url /replication
* </code>
*/
@Test
public void replication() throws Exception {
String url = "D:\\code\\resources\\file\\replication.txt";
boolean mkdirs = fileSystem.mkdirs(new Path("/replication"));
if (mkdirs) {
fileSystem.copyFromLocalFile(false, true, new Path(url), new Path("/replication"));
}
}
@After
public void after() throws IOException {
if (fileSystem != null) fileSystem.close();
}
}