hadoop 將檔案上傳到指定的datanode 檢視檔案所在的塊
package hgs.dfsclient.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CreateFlag; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.permission.FsCreateModes; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.hdfs.DFSClient; import org.apache.hadoop.hdfs.DFSOutputStream; import org.apache.hadoop.hdfs.protocol.*; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; import java.util.EnumSet; import java.util.List; import java.util.Random; public class MainTest { public static void main(String[] args) throws IOException, URISyntaxException { Configuration conf = new Configuration(); conf.addResource(new File("D://hdfsconf/core-site.xml").toURI().toURL()); conf.addResource(new File("D://hdfsconf/hdfs-site.xml").toURI().toURL()); String url = conf.get("fs.defaultFS"); System.out.println(url); Path path = new Path("/user/test.txt"); DFSClient client = new DFSClient(new URI("hdfs://192.168.0.191:9000/"),conf); //favor datanode InetSocketAddress [] favor = {new InetSocketAddress("192.168.0.191", 50012)}; createAndFillFile(client,conf,favor,"/user/test.txt4"); listBlockLocation(client, conf, "/user/test.txt4",true ); client.close(); } //將資料傳輸到指定的datanode public static void createAndFillFile(DFSClient client,Configuration conf, InetSocketAddress [] favor,String src) throws IOException { DFSOutputStream dfsOutputStream = client.create(src, FsCreateModes.applyUMask( FsPermission.getFileDefault(), FsPermission.getUMask(conf)), EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), true, (short) 1, 32 * 1024 * 1024, null, 8192, null, favor, null); byte[] b = new byte[1024*3*3]; for(int i = 0 ;i<100000;i++){ fileByte(b); dfsOutputStream.write(b); } dfsOutputStream.close(); } static Random rand = new Random(System.nanoTime()); public static void fileByte(byte[] b){ if(b != null ){ rand.nextBytes(b); } } //獲取檔案的block位置 public static void listBlockLocation(DFSClient client,Configuration conf,String src,boolean needLocation) throws IOException { DirectoryListing directoryListing = client.listPaths(src, HdfsFileStatus.EMPTY_NAME, needLocation); HdfsFileStatus[] fileStatuses = directoryListing.getPartialListing(); for(int i=0 ; i <fileStatuses.length ; i++){ HdfsLocatedFileStatus hl = (HdfsLocatedFileStatus)fileStatuses[i]; LocatedBlocks locatedBlocks = hl.getLocatedBlocks(); List<LocatedBlock> locatedBlocks1 = locatedBlocks.getLocatedBlocks(); for(LocatedBlock lb : locatedBlocks1 ){ DatanodeInfo[] locations = lb.getLocations(); for(DatanodeInfo li : locations){ System.out.println("addr:"+li.getIpAddr()+"--port:"+li); } } } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2698379/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Hadoop檢視檔案///hadoop 清洗檔案出現亂碼Hadoop
- git檢視指定提交檔案的內容Git
- Git檢視指定檔案的sha-1值Git
- 使用SecureCRT的SFTP將檔案上傳到Liunx系統上SecurecrtFTP
- 寫給自己的分塊上傳檔案
- 檔案上傳之三基於flash的檔案上傳
- 檔案上傳漏洞防範-檔案型別檢測型別
- 【大資料】【hadoop】檢視hdfs檔案命令大資料Hadoop
- 將本地檔案傳輸到GitHubGithub
- 上傳檔案的陷阱
- Hadoop hdfs上傳檔案報錯解決Hadoop
- 將 nuxt 打包檔案上傳到騰訊雲 cosUX
- 檢視檔案
- 單個檔案上傳和批量檔案上傳
- 檔案上傳
- 幾種windows到linux上傳檔案的方式WindowsLinux
- Spring Boot的檔案上傳Spring Boot
- Laravel 中的檔案上傳Laravel
- SpringMVC檔案上傳下載(單檔案、多檔案)SpringMVC
- PHP上傳檔案到七牛(Qiniu)PHP
- java檔案上傳到伺服器Java伺服器
- 上傳執行sql檔案到linuxSQLLinux
- Python 搭配 Automator 上傳檔案到 GithubPythonGithub
- Linux伺服器上傳檔案傳送檔案Linux伺服器
- Mac檢視隱藏的檔案Mac
- Azure Terraform(十二)利用 Terraform 將檔案上傳到 Azure Blob StorageORM
- 【Hadoop踩坑】HDFS上傳、刪除檔案失敗Hadoop
- 【Hadoop踩雷】無法上傳檔案?有辦法!Hadoop
- PHP上傳檔案PHP
- 檔案上傳概述
- ajaxfileupload 檔案上傳
- Flask——檔案上傳Flask
- Linux上傳檔案Linux
- 檔案上傳漏洞
- minio上傳檔案
- SpringBoot上傳檔案Spring Boot
- JavaScript 檔案上傳JavaScript
- Git上傳檔案Git