溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HDFS基本常用命令是什么

發(fā)布時(shí)間:2021-11-08 15:03:23 來源:億速云 閱讀:145 作者:小新 欄目:云計(jì)算

這篇文章主要介紹了HDFS基本常用命令是什么,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一.    創(chuàng)建HDFS文件: 

public class Test4CreateFile {
	/**
	 * 創(chuàng)建HDFS文件:
	 * */
	public static void main(String[] args) {
		try {
			Configuration conf = new Configuration();
			URI uri = new URI("hdfs://192.168.226.129:9000");
			byte[] buff = "Hello Hadoop HDFS".getBytes();
			FileSystem fs = FileSystem.get(uri, conf);
			Path dfs = new Path("hdfs://192.168.226.129:9000/studyhadoop");
			FSDataOutputStream outputStream = fs.create(dfs);
			outputStream.write(buff,0,buff.length);
			FileStatus files[] = fs.listStatus( dfs );
			for( FileStatus file:files){
				System.out.println( "file:  " + file.getPath() );
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

二:刪除HDFS文件

public class Test5DeleteFile {
	/**
	 * 刪除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 delef = new Path("hdfs://192.168.226.129:9000/testhadoop1");
			boolean isDeleted = fs.delete(delef, false);
			System.out.println( "isDelete: " + isDeleted );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

三:創(chuàng)建HDFS目錄

public class Test9Mkdir {
	/**
	 * HDFS下 創(chuàng)建目錄文件
	 * */
	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/testhadoop");
			boolean isMkdirs = fs.mkdirs(dfs);
			if( isMkdirs ){
				System.out.println( " Make Dir Successful ! ");
			}else{
				System.out.println( " Make Dir Failure ! ");
			}
			fs.close();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

四:重命名HDFS文件

public class Test2Rename {

	/**
	 * 重命名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 oldpath = new Path("hdfs://192.168.226.129:9000/testhadoop");
			Path newpath = new Path("hdfs://192.168.226.129:9000/testhadoop1");
			//判斷該文件是否存在
			boolean isExists = fs.exists(oldpath);
			System.out.println( "isExists: " +isExists );
			//重命名文件
			fs.rename(oldpath, newpath);
			isExists = fs.exists(newpath);
			System.out.println( "newpathisExists: " +isExists );
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

}

五:上傳本地文件至HDFS

public class Test3CopyFile {
	/**
	 * 上傳本地文件到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 src = new Path("F:\\04-HadoopStudy\\mapreduce.txt");
			Path dst = new Path("hdfs://192.168.226.129:9000/rootdir");
			fs.copyFromLocalFile(src, dst);
			System.out.println("Upload " + conf.get("fs.default.name"));
			FileStatus files[] = fs.listStatus( dst );
			for( FileStatus file:files){
				System.out.println( file.getPath() );
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

}

六.    從HDFS下載文件至本地

public class Test10CopyToFile {
	/**
	 * 從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 src = new Path("F:\\");
			Path dst = new Path("hdfs://192.168.226.129:9000/studyhadoop");
			fs.copyToLocalFile(dst, src);
			System.out.println("DownLoad " + conf.get("fs.default.name"));
			FileStatus files[] = fs.listStatus( dst );
			for( FileStatus file:files){
				System.out.println( file.getPath() );
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“HDFS基本常用命令是什么”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI