1. 程式人生 > 實用技巧 >2020年暑假(4)

2020年暑假(4)

1.Hadoop常用命令

單程序啟動 start-dfs.sh
檢視指定目錄下內容 hdfs dfs -ls [目錄]
建立目錄 hdfs dfs -mkdir [新目錄]
開啟檔案 hdfs dfs -cat [檔案路徑]
重新命名檔案 hdfs dfs -mv [原檔名] [新檔名]
上傳檔案或資料夾 hdfs dfs -put [本地目錄] [hadoop目錄]
下載檔案或資料夾 hdfs dfs -get [檔案目錄] [本地目錄]
刪除Hadoop上的檔案或資料夾 hdfs dfs -rm [檔案地址]

2.Hadoop的一些API的使用

package hdfs24;

import
java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator; import org.junit.Before; import org.junit.Test; public class HdfsClientDemo { public static void main(String[] args) throws Exception { /** * Configuration引數物件的機制: * 構造時,會載入jar包中的預設配置 xx-default.xml * 再載入 使用者配置xx-site.xml ,覆蓋掉預設引數 * 構造完成之後,還可以conf.set("p","v"),會再次覆蓋使用者配置檔案中的引數值
*/ // new Configuration()會從專案的classpath中載入core-default.xml hdfs-default.xml core-site.xml hdfs-site.xml等檔案 Configuration conf = new Configuration(); // 指定本客戶端上傳檔案到hdfs時需要儲存的副本數為:2 conf.set("dfs.replication", "2"); // 指定本客戶端上傳檔案到hdfs時切塊的規格大小:64M conf.set("dfs.blocksize", "64m"); // 構造一個訪問指定HDFS系統的客戶端物件: 引數1:——HDFS系統的URI,引數2:——客戶端要特別指定的引數,引數3:客戶端的身份(使用者名稱) FileSystem fs = FileSystem.get(new URI("hdfs://hdp-01:9000/"), conf, "root"); // 上傳一個檔案到HDFS中 fs.copyFromLocalFile(new Path("D:/install-pkgs/hbase-1.2.1-bin.tar.gz"), new Path("/aaa/")); fs.close(); } FileSystem fs = null; @Before public void init() throws Exception{ Configuration conf = new Configuration(); conf.set("dfs.replication", "2"); conf.set("dfs.blocksize", "64m"); fs = FileSystem.get(new URI("hdfs://hdp-01:9000/"), conf, "root"); } /** * 從HDFS中下載檔案到客戶端本地磁碟 * @throws IOException * @throws IllegalArgumentException */ @Test public void testGet() throws IllegalArgumentException, IOException{ fs.copyToLocalFile(new Path("/hdp20-05.txt"), new Path("f:/")); fs.close(); } /** * 在hdfs內部移動檔案\修改名稱 */ @Test public void testRename() throws Exception{ fs.rename(new Path("/install.log"), new Path("/aaa/in.log")); fs.close(); } /** * 在hdfs中建立資料夾 */ @Test public void testMkdir() throws Exception{ fs.mkdirs(new Path("/xx/yy/zz")); fs.close(); } /** * 在hdfs中刪除檔案或資料夾 */ @Test public void testRm() throws Exception{ fs.delete(new Path("/aaa"), true); fs.close(); } /** * 查詢hdfs指定目錄下的檔案資訊 */ @Test public void testLs() throws Exception{ // 只查詢檔案的資訊,不返回資料夾的資訊 RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true); while(iter.hasNext()){ LocatedFileStatus status = iter.next(); System.out.println("檔案全路徑:"+status.getPath()); System.out.println("塊大小:"+status.getBlockSize()); System.out.println("檔案長度:"+status.getLen()); System.out.println("副本數量:"+status.getReplication()); System.out.println("塊資訊:"+Arrays.toString(status.getBlockLocations())); System.out.println("--------------------------------"); } fs.close(); } /** * 查詢hdfs指定目錄下的檔案和資料夾資訊 */ @Test public void testLs2() throws Exception{ FileStatus[] listStatus = fs.listStatus(new Path("/")); for(FileStatus status:listStatus){ System.out.println("檔案全路徑:"+status.getPath()); System.out.println(status.isDirectory()?"這是資料夾":"這是檔案"); System.out.println("塊大小:"+status.getBlockSize()); System.out.println("檔案長度:"+status.getLen()); System.out.println("副本數量:"+status.getReplication()); System.out.println("--------------------------------"); } fs.close(); } }