您好,登錄后才能下訂單哦!
這篇文章主要介紹了HDFS基本常用命令是什么,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一. 創建HDFS文件:
public class Test4CreateFile { /** * 創建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(); } } }
三:創建HDFS目錄
public class Test9Mkdir { /** * 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/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(); } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“HDFS基本常用命令是什么”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。