配置集羣時,關於HDFS的配置都在/etc進行了相關配置,用eclipse客戶端進行開發時,可以用更高優先級的配置覆蓋掉集羣中的配置。下面以設置副本爲例。
在hadoop集羣中hdfs-site.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!--nodes-->
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<!-- 指定Hadoop輔助名稱節點主機配置 -->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>172.17.0.3:50090</value>
</property>
</configuration>
1、採用集羣中配置
集羣中配置的副本數爲3,下面在eclipse中執行上傳HDFS文本demo,觀察文件的副本數。
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.85.137:9000"), conf, "root");
fs.copyFromLocalFile(new Path("D:/tmp/myfile.txt"), new Path("/user/lzj"));
fs.close();
System.out.println("successfully!");
}
執行demo,發現上傳到HDFS中的myfile文件副本的數量爲3份。
2、採用classpath下的文件中配置的
把集羣配置中的hdfs-site.xml文件複製到src/main/resoures目錄下,只保留關於副本的配置即可,src/main/resoures目錄下的hdfs-site.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!--nodes-->
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
</configuration>
然後執行測試demo
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.85.137:9000"), conf, "root");
fs.copyFromLocalFile(new Path("D:/tmp/myfile1.txt"), new Path("/user/lzj"));
fs.close();
System.out.println("successfully!");
}
執行完畢後,發現上傳到hdfs中的myfile1.txt的副本數量爲2份。說明src/main/resoures目錄下的hdfs-site.xml下的配置覆蓋掉了集羣中副本的配置。
3、採用代碼中的配置
保留上面配置,然後在代碼中設置副本的數量,代碼如下,修改副本設置
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
conf.set("dfs.replication", "1");
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.85.137:9000"), conf, "root");
fs.copyFromLocalFile(new Path("D:/tmp/myfile2.txt"), new Path("/user/lzj"));
fs.close();
System.out.println("successfully!");
}
執行代碼,發現上傳到HDFS中myfile2.txt的副本數量爲1份,可見代碼中副本的設置覆蓋掉了src/main/resoures目錄下的hdfs-site.xml中副本的配置。
總結
代碼中配置優先級高於classpath下的配置,classpath下的配置優先級高於集羣中的配置。如果以上三者都沒有配置,則採用集羣默認的配置。