Java API操作HA方式下的Hadoop

來源:互聯網
上載者:User

標籤:上傳   let   on()   blog   put   ast   tar   添加   load   

通過java api串連Hadoop叢集時,如果叢集支援HA方式,那麼可以通過如下方式設定來自動切換到活動的master節點上。其中,ClusterName 是可以任意指定的,跟叢集配置無關,dfs.ha.namenodes.ClusterName也可以任意指定名稱,有幾個master就寫幾個,後面根據相應的設定添加master節點地址即可。

private static String ClusterName = "nsstargate";    private static final String HADOOP_URL = "hdfs://"+ClusterName;    public static Configuration conf;    static {        conf = new Configuration();        conf.set("fs.defaultFS", HADOOP_URL);        conf.set("dfs.nameservices", ClusterName);        conf.set("dfs.ha.namenodes."+ClusterName, "nn1,nn2");        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn1", "172.16.50.24:8020");        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn2", "172.16.50.21:8020");        //conf.setBoolean(name, value);        conf.set("dfs.client.failover.proxy.provider."+ClusterName,                 "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");    }

上傳檔案到HDFS的代碼如下,至於讀取等其他動作,可以參考網路上其他文章。

/**     * 上傳檔案到HDFS上去     */    private static void uploadToHdfs() throws IOException {        String localSrc = "E:\\test\\article01.txt";        String dst = "/user/test/article04.txt";        FileSystem fs = FileSystem.get(URI.create(HADOOP_URL), conf);        long start = new Date().getTime();       /* InputStream in = new FileInputStream(localSrc);        InputStreamReader isr = new InputStreamReader(in, "GBK");        OutputStream out = fs.create(new Path(HADOOP_URL+dst), true);        IOUtils.copy(isr, out, "UTF8");*/                //該方法更快        FSDataOutputStream outputStream=fs.create(new Path(dst));        String fileContent = FileUtils.readFileToString(new File(localSrc), "GBK");        outputStream.write(fileContent.getBytes());        outputStream.close();                long end = new Date().getTime();        System.out.println("use:"+(end-start));            }

 

Java API操作HA方式下的Hadoop

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.