HDFS API编程之副本系数
生活随笔
收集整理的這篇文章主要介紹了
HDFS API编程之副本系数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Hadoop 版本:hadoop-2.6.0-cdh5.15.1
雖然在hdfs-site.xml中設(shè)置了副本數(shù)量:
但是我們在Java API代碼中創(chuàng)建文件:
public class HDFSAPP {public static final String HDFS_PATH = "hdfs://swarm-worker1:9000";FileSystem fileSystem = null;Configuration configuration = null;/*** 構(gòu)造一個訪問指定HDFS系統(tǒng)的客戶端對象* 第一個參數(shù):HDFS的URI* 第二個參數(shù):客戶端指定的配置參數(shù)* 第三個參數(shù):客戶端的身份,即用戶名* @throws URISyntaxException*/@Beforepublic void setUp() throws URISyntaxException, IOException, InterruptedException {System.out.println("--------setUp---------");configuration = new Configuration();fileSystem = FileSystem.get(new URI("hdfs://swarm-worker1:9000"), configuration, "iie4bu");}/*** 創(chuàng)建文件* @throws Exception*/@Testpublic void create() throws Exception {FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/a.txt"));out.writeUTF("hello world");out.flush();out.close();}@Afterpublic void tearDown() {configuration = null;fileSystem = null;System.out.println("--------tearDown---------");} }上面的代碼創(chuàng)建了一個文件/hdfsapi/test/a.txt,在瀏覽器中查看:
可以看到副本數(shù)量Replication為3,而我們通過命令創(chuàng)建的文件副本數(shù)量是1。
說明是在代碼層面有一個默認(rèn)設(shè)置。
上面的代碼輸出結(jié)果為3說明副本數(shù)量是3。
在這個路徑下有一個hdfs-default.xml文件,文件中指定了默認(rèn)的replication數(shù)量是3:
這就是為什么configuration默認(rèn)的數(shù)量是3的原因了。因為我們使用的configuration是默認(rèn)的,自動加載默認(rèn)的hdfs-default.xml文件中的數(shù)據(jù)。
所以我們可以通過手動設(shè)置這個參數(shù)來修改replication:
configuration.set("dfs.replication", "1");
此時再創(chuàng)建一個文件時,replication副本數(shù)量就是1了。
總結(jié)
以上是生活随笔為你收集整理的HDFS API编程之副本系数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hadoop报错AccessContro
- 下一篇: HDFS数据副本的摆放策略