您好,登錄后才能下訂單哦!
小編給大家分享一下HDFS基本常用命令有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
一:查看HDFS文件的最后修改時(shí)間
public class Test6GetLTime { /** * 查看HDFS文件的最后修改時(shí)間 * */ public static void main(String[] args) { try { Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.226.129:9000"); FileSystem fs = FileSystem.get(uri, conf); Path dfs = new Path("hdfs://192.168.226.129:9000/"); FileStatus fileStatus = fs.getFileStatus(dfs); long modificationTime = fileStatus.getModificationTime(); System.out.println( "Modefication time is: " + modificationTime ); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
二:查找某個(gè)文件在HDFS集群的位置
public class Test7FileLocation { /** * 查找某個(gè)文件在HDFS集群的位置 * */ public static void main(String[] args) { try { Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.226.129:9000"); FileSystem fs = FileSystem.get(uri, conf); Path dfs = new Path("hdfs://192.168.226.129:9000/rootdir/ssh.txt"); FileStatus fileStatus = fs.getFileStatus(dfs); BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus,0, fileStatus.getLen() ); int blockLen = blkLocations.length; System.out.println("blockLen of length : " +blockLen ); for( int i=0;i<blockLen; i++){ String[] hosts = blkLocations[i].getHosts(); System.out.println("Block " + i +" Location: " + hosts[i]); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
三: 獲取HDFS集群上所有節(jié)點(diǎn)名稱
public class Test8GetList { /** * 獲取HDFS集群上所有節(jié)點(diǎn)名稱: * */ public static void main(String[] args) { try { Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.226.129:9000"); FileSystem fs = FileSystem.get(uri,conf); DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); String[] names = new String[dataNodeStats.length]; int dataNodeLen = dataNodeStats.length; for( int i=0; i<dataNodeLen;i++){ names[i] = dataNodeStats[i].getHostName(); System.out.println("Node " + i + " Name: "+ names[i] ); } } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
四: 上傳一個(gè)視頻文件至HDFS,( 非實(shí)時(shí)視頻流)
public class UploadLive { public static void main(String[] args) { try { Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.226.129:9000"); FileSystem fs = FileSystem.get(uri, conf); FileSystem local = FileSystem.getLocal(conf); //確定需要上傳視頻流路徑和接收視頻流路徑 Path inputDir = new Path("F:\\AHadoopTestFile"); Path hdfsFile = new Path("hdfs://192.168.226.129:9000/testhadoop/acceptLiveFile"); System.out.println( inputDir.toString()); //創(chuàng)建HDFS上 "acceptLiveFile" 目錄 用來接收視頻文件 boolean isExist = fs.exists( hdfsFile ); if( !isExist ){ fs.mkdirs(hdfsFile); System.out.println(" 創(chuàng)建新的目錄文件成功..."); } FileStatus[] inputFiles = local.listStatus(inputDir); FSDataOutputStream out; //通過OutputStream.write()來將視頻文件寫入HDFS下的指定目錄: int inputFileslen = inputFiles.length; for( int i=0;i<inputFileslen;i++){ System.out.println( inputFiles[i].getPath().getName() ); FSDataInputStream in = local.open(inputFiles[i].getPath() ); out = fs.create( new Path("hdfs://192.168.226.129:9000/testhadoop/acceptLiveFile/"+inputFiles[i].getPath().getName())); byte [] buffer = new byte[1024]; int byteRead = 0; while((byteRead = in.read(buffer))>0 ){ out.write(buffer,0,byteRead); } out.close(); in.close(); File file = new File( inputFiles[i].getPath().toString()); file.delete(); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上是“HDFS基本常用命令有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。