国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

這篇具有很好參考價(jià)值的文章主要介紹了大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

??實(shí)驗(yàn)?zāi)康?/h2>

1)理解 HDFS 在 Hadoop 體系結(jié)構(gòu)中的角色。

2)熟練使用 HDFS 操作常用的 shell 命令。

3)熟悉 HDFS 操作常用的 Java API。

??實(shí)驗(yàn)平臺(tái)

1)操作系統(tǒng):Linux;

2)Hadoop 版本:3.2.2;

3)JDK 版本:1.8;

4)Java IDE:Eclipse。

??實(shí)驗(yàn)內(nèi)容

編程實(shí)現(xiàn)以下功能,并利用 Hadoop 提供的 Shell 命令完成相同任務(wù)

??HDFSApi

1)向 HDFS 中上傳任意文本文件。如果指定的文件在 HDFS 中已經(jīng)存在,則由用戶來指定是追加到原有文件末尾還是覆蓋原有的文件;

Shell命令

檢查文件是否存在,可以使用如下命令:

cd /usr/local/hadoop
./bin/hdfs dfs -test -e text.txt
#執(zhí)行完上述命令不會(huì)輸出結(jié)果,需要繼續(xù)輸入以下命令查看結(jié)果
echo $?

重啟了虛擬機(jī)開始做這個(gè)實(shí)驗(yàn),一開始出現(xiàn)報(bào)錯(cuò),搜索后發(fā)現(xiàn)原來是hadoop沒開

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

echo $? 返回上一個(gè)命令的狀態(tài),0表示沒有錯(cuò)誤,其他任何值表明有錯(cuò)誤(這里顯然出錯(cuò),因?yàn)檫€沒有建text.txt文件夾),手動(dòng)建一個(gè)text.txt然后拖到/usr/local/hadoop。

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


用戶可以選擇追加到原來文件末尾或者覆蓋原來文件

cd /usr/local/hadoop
./bin/hdfs dfs -appendToFile local.txt text.txt #追加到原文件末尾
#touch local.txt
./bin/hdfs dfs -copyFromLocal -f local.txt text.txt #覆蓋原來文件,第一種命令形式
./bin/hdfs dfs -cp -f file:///usr/local/hadoop/local.txt text.txt#覆蓋原來文件,第二種命令形式

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

這樣會(huì)自動(dòng)建一個(gè)local.txt文件

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

實(shí)際上,也可以不用上述方法,而是采用如下命令來實(shí)現(xiàn)

if $(hdfs dfs -test -e text.txt);
then $(hdfs dfs -appendToFile local.txt text.txt);
else $(hdfs dfs -copyFromLocal -f local.txt text.txt);
fi

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*; 
public class HDFSApi 
{ 
    /*
	 判斷路徑是否存在
	*/
    public static boolean test(Configuration conf, String path) throws IOException 
    {         
        FileSystem fs = FileSystem.get(conf);       
        return fs.exists(new Path(path)); 
    } 
    /*
	 復(fù)制文件到指定路徑
	 若路徑已存在,則進(jìn)行覆蓋
	 */
    public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException 
    { 
        FileSystem fs = FileSystem.get(conf); 
        Path localPath = new Path(localFilePath); 
        Path remotePath = new Path(remoteFilePath); 
        /* fs.copyFromLocalFile 第一個(gè)參數(shù)表示是否刪除源文件,第二個(gè)參數(shù)表示是否覆蓋 */
        fs.copyFromLocalFile(false, true, localPath, remotePath);      
        fs.close(); 
    } 
    /*
	 追加文件內(nèi)容
	 */
    public static void appendToFile(Configuration conf, String  localFilePath, String remoteFilePath) throws IOException 
    { 
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        /* 創(chuàng)建一個(gè)文件讀入流 */
        FileInputStream in = new FileInputStream(localFilePath); 
        /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */
        FSDataOutputStream out = fs.append(remotePath); 
        /* 讀寫文件內(nèi)容 */
   		byte[] data = new byte[1024];      
   		int read = -1;         
		while ( (read = in.read(data)) > 0 ) 
        {           
            out.write(data, 0, read); 
        }         
		out.close();         
        in.close();         
        fs.close(); 
    } 
    /*
	主函數(shù)
	*/
	public static void main(String[] args) 
    { 
    	Configuration conf = new Configuration();
        conf.set("fs.default.name","hdfs://localhost:9000"); 
    	String localFilePath = "/usr/local/hadoop/local.txt";   
    	String remoteFilePath = "/usr/local/hadoop/text.txt";    
   	 	String choice = "append";
   		String choice2 = "overwrite"; 
   		Scanner in=new Scanner(System.in);
        String a=in.nextLine();
        boolean a1= a.contentEquals(choice2);
        boolean a2=a.contentEquals(choice);
   		//System.out.println(a.contentEquals(choice));//
    	try 
        {         
            /* 判斷文件是否存在 */
			Boolean fileExists = false;       
			if (HDFSApi.test(conf, remoteFilePath)) 
			{         
				fileExists = true; 
				System.out.println(remoteFilePath + " 已存在.");   
   			} 
			else 
			{ 
       			System.out.println(remoteFilePath + " 不存在."); 
      		} 
      		if ( !fileExists) 
      		{//文件不存在,則上傳 
        		HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
        		System.out.println(localFilePath + " 已上傳至 " + remoteFilePath); 
      		} 
      		else if (a2) 
      		{//選擇覆蓋 
        		HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
        		System.out.println(localFilePath + " 已覆蓋 " + remoteFilePath); 
      		}
   			else if(a1) 
   			{//選擇追加 
        		HDFSApi.appendToFile(conf, localFilePath, remoteFilePath); 
        		System.out.println(localFilePath + " 已追加至 " + remoteFilePath); 
      		} 
		}
    	catch (Exception e) 
        { 
      		e.printStackTrace(); 
    	} 
  	} 
}

??HDFSApi2

2)從 HDFS 中下載指定文件。如果本地文件與要下載的文件名稱相同,則自動(dòng)對(duì)下載的文件重命名;

Shell命令

if $(./bin/hdfs dfs -test -e file:///usr/local/hadoop/text.txt); 
then $(./bin/hdfs dfs -copyToLocal text.txt ./text2.txt); 
else $(./bin/hdfs dfs -copyToLocal text.txt ./text.txt);
fi

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.*; 
import java.io.*; 
public class HDFSApi2 
{ 
    /*
	 下載文件到本地
	 判斷本地路徑是否已存在,若已存在,則自動(dòng)進(jìn)行重命名
	*/
	public  static  void  copyToLocal(Configuration  conf,  String  remoteFilePath, String localFilePath) throws IOException 
    { 
      	FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        File f = new File(localFilePath); 
		if(f.exists()) 
        { //如果文件名存在,自動(dòng)重命名(在文件名后面加上 _0, _1 ...) 
          	System.out.println(localFilePath + " 已存在.");           
			Integer i = 0;           
			while (true) 
            {             
				f = new File(localFilePath + "_" + i.toString());             
				if (!f.exists()) 
                {               
					localFilePath = localFilePath + "_" + i.toString(); 
         			break; 
        		}
			} 
			System.out.println("將重新命名為: " + localFilePath);  
 		}        
        // 下載文件到本地
        Path localPath = new Path(localFilePath);       
 		fs.copyToLocalFile(remotePath, localPath);       
 		fs.close(); 
    } 
    /*
	主函數(shù)
	*/
  	public static void main(String[] args) 
    { 
    	Configuration conf = new Configuration(); 
        conf.set("fs.default.name","hdfs://localhost:9000");
   		String localFilePath = "/usr/local/hadoop/local.txt";    
		String remoteFilePath = "/usr/local/hadoop/text.txt";   
        try 
        { 
            HDFSApi2.copyToLocal(conf, remoteFilePath, localFilePath); 
            System.out.println("下載完成"); 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        }
	}
}

??HDFSApi3

3)將 HDFS 中指定文件的內(nèi)容輸出到終端中;

Shell命令

hdfs dfs -cat text.txt

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


剛開始先跑shell,運(yùn)行不報(bào)錯(cuò),但無內(nèi)容輸出(但txt里是有內(nèi)容的)。編程實(shí)現(xiàn)跑了一遍,再回去跑shell就有輸出了(?

編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
public class HDFSApi3 
{
    public static void cat(Configuration conf, String remoteFilePath) throws IOException 
    {
        /*
	 	讀取文件內(nèi)容
	 	*/
		 FileSystem fs = FileSystem.get(conf);
		 Path remotePath = new Path(remoteFilePath);
		 FSDataInputStream in = fs.open(remotePath);
		 BufferedReader d = new BufferedReader(new InputStreamReader(in));
		 String line = null;
		 while ( (line = d.readLine()) != null ) 
         {
			 System.out.println(line);
		 }
		 d.close();
		 in.close();
		 fs.close();
	 }
	 
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000");
		String remoteFilePath = "/usr/local/hadoop/text.txt"; // HDFS 路徑
		try 
        {
			System.out.println("讀取文件: " + remoteFilePath);
			HDFSApi3.cat(conf, remoteFilePath);
			System.out.println("\n 讀取完成");
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
	}
}

??HDFSApi4

4)顯示 HDFS 中指定的文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息;

Shell命令

hdfs dfs -ls -h text.txt

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class HDFSApi4 
{
	 /*
	 顯示指定文件的信息
	 */
	 public static void ls(Configuration conf, String remoteFilePath) throws IOException 
     {
		 FileSystem fs = FileSystem.get(conf);
		 Path remotePath = new Path(remoteFilePath);
		 FileStatus[] fileStatuses = fs.listStatus(remotePath);
		 for (FileStatus s : fileStatuses) 
         {
			 System.out.println("路徑: " + s.getPath().toString());
			 System.out.println("權(quán)限: " + s.getPermission().toString());
			 System.out.println("大小: " + s.getLen());
			 /* 返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式 */
			 Long timeStamp = s.getModificationTime();
			 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String date = format.format(timeStamp); 
			 System.out.println("時(shí)間: " + date);
		 }
		 fs.close();
	 }
	 
	/**
	* 主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
        conf.set("fs.default.name","hdfs://localhost:9000");
		String remoteFilePath = "/usr/local/hadoop/text.txt"; // HDFS 路徑
		try 
        {
			System.out.println("讀取文件信息: " + remoteFilePath);
			HDFSApi4.ls(conf, remoteFilePath);
			System.out.println("\n 讀取完成");
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
	}
}

??HDFSApi5

5)給定 HDFS 中某一個(gè)目錄,遞歸輸出該目錄下的所有文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息;

Shell命令

cd /usr/local/hadoop
./bin/hdfs dfs -ls -R -h /usr/hadoop

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class HDFSApi5
{
	 /*
	  顯示指定文件夾下所有文件的信息(遞歸)
	 */
	public static void lsDir(Configuration conf, String remoteDir) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path dirPath = new Path(remoteDir);
		 /* 遞歸獲取目錄下的所有文件 */
		 RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(dirPath, true);
		 /* 輸出每個(gè)文件的信息 */
		while (remoteIterator.hasNext()) 
        {
			 FileStatus s = remoteIterator.next();
			 System.out.println("路徑: " + s.getPath().toString());
			 System.out.println("權(quán)限: " + s.getPermission().toString());
			 System.out.println("大小: " + s.getLen());
			 /* 返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式 */
			 Long timeStamp = s.getModificationTime();
			 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String date = format.format(timeStamp); 
			 System.out.println("時(shí)間: " + date);
			 System.out.println();
		}
		 fs.close();
	} 
	 
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000");
		String remoteDir = "/usr/hadoop"; // HDFS 路徑
		try 
        {
			System.out.println("(遞歸)讀取目錄下所有文件的信息: " + remoteDir);
			HDFSApi5.lsDir(conf, remoteDir);
			System.out.println("讀取完成");
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
	}
}

??HDFSApi6

6)提供一個(gè) HDFS 內(nèi)的文件的路徑,對(duì)該文件進(jìn)行創(chuàng)建和刪除操作。如果文件所在目錄不存在,則自動(dòng)創(chuàng)建目錄;

Shell命令

if $(hdfs dfs -test -d dir1/dir2);
then $(hdfs dfs -touchz dir1/dir2/filename);
else $(hdfs dfs -mkdir -p dir1/dir2 && hdfs dfs -touchz dir1/dir2/filename);
fi
hdfs dfs -rm dir1/dir2/filename #刪除文件

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

路徑存在的情況(以下代碼是路徑存在的情況)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

路徑不存在的情況

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

目錄不存在的情況

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
public class HDFSApi6 
{
    /*
	 判斷路徑是否存在
	*/
	public static boolean test(Configuration conf, String path) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 return fs.exists(new Path(path));
	}
	/*
	 創(chuàng)建目錄
	*/
	public static boolean mkdir(Configuration conf, String remoteDir) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path dirPath = new Path(remoteDir);
		 boolean result = fs.mkdirs(dirPath);
		 fs.close();
		 return result;
	}
	/*
	 創(chuàng)建文件
	*/
	public static void touchz(Configuration conf, String remoteFilePath) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path remotePath = new Path(remoteFilePath);
		 FSDataOutputStream outputStream = fs.create(remotePath);
		 outputStream.close();
		 fs.close();
	} 
	/*
	刪除文件
	*/
	public static boolean rm(Configuration conf, String remoteFilePath) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path remotePath = new Path(remoteFilePath);
		 boolean result = fs.delete(remotePath, false);
		 fs.close();
		 return result;
	}
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000");
		String remoteFilePath = "/usr/local/hadoop/text.txt"; // HDFS 路徑
		String remoteDir = "/usr/hadoop/input"; // HDFS 路徑對(duì)應(yīng)的目錄
		try {
			/* 判斷路徑是否存在,存在則刪除,否則進(jìn)行創(chuàng)建 */
			if ( HDFSApi6.test(conf, remoteFilePath) ) 
            {
				HDFSApi6.rm(conf, remoteFilePath); // 刪除
				System.out.println("刪除路徑: " + remoteFilePath);
			} 
            else 
            {
				if ( !HDFSApi6.test(conf, remoteDir) ) 
                { // 若目錄不存在,則進(jìn)行創(chuàng)建
					HDFSApi6.mkdir(conf, remoteDir);
					System.out.println("創(chuàng)建文件夾: " + remoteDir);
				}
				HDFSApi6.touchz(conf, remoteFilePath);
				System.out.println("創(chuàng)建路徑: " + remoteFilePath);
			}
		} 
        catch (Exception e) 
        {
				e.printStackTrace();
		}
	}
}

??HDFSApi7

7)提供一個(gè) HDFS 的目錄的路徑,對(duì)該目錄進(jìn)行創(chuàng)建和刪除操作。創(chuàng)建目錄時(shí),如果目錄文件所在目錄不存在,則自動(dòng)創(chuàng)建相應(yīng)目錄;刪除目錄時(shí),當(dāng)該目錄為空時(shí)刪除,當(dāng)該目錄不為空時(shí)不刪除該目錄;

Shell命令

cd /usr/local/hadoop
./bin/hdfs dfs -mkdir -p dir1/dir2
./bin/hdfs dfs -rmdir dir1/dir2
#若為非空目錄,強(qiáng)制刪除語句如下
./bin/hdfs dfs -rm -R dir1/dir2

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

目錄不存在于是創(chuàng)建

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

目錄存在且為空,刪除

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
public class HDFSApi7 
{
	/*
	 判斷路徑是否存在
	*/
	public static boolean test(Configuration conf, String path) throws IOException 
    {
        //訪問獲取hdfs文件系統(tǒng)數(shù)據(jù)
		 FileSystem fs = FileSystem.get(conf);
		 return fs.exists(new Path(path));
	}
	/*
	 判斷目錄是否為空
	 true: 空,false: 非空
	*/
	public static boolean isDirEmpty(Configuration conf, String remoteDir) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path dirPath = new Path(remoteDir);//獲取hdfs文件路徑
		 RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(dirPath, true);
        //獲取當(dāng)前路徑下的文件,得到file類型的數(shù)組
		 return !remoteIterator.hasNext();//hasNext表示文件有內(nèi)容,用這個(gè)判斷目錄是否為空
	}
	/*
	創(chuàng)建目錄
	*/
	public static boolean mkdir(Configuration conf, String remoteDir) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path dirPath = new Path(remoteDir);
		 boolean result = fs.mkdirs(dirPath);//mkdirs,創(chuàng)建
		 fs.close();
		 return result;
	}
	/*
	 刪除目錄
	*/
	public static boolean rmDir(Configuration conf, String remoteDir) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);//訪問獲取hdfs文件系統(tǒng)數(shù)據(jù)
		 Path dirPath = new Path(remoteDir);//獲取hdfs文件路徑
		 /* 第二個(gè)參數(shù)表示是否遞歸刪除所有文件 */
		 boolean result = fs.delete(dirPath, true);//delete,刪除
		 fs.close();
		 return result;
	}
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000"); 
		String remoteDir = "/usr/hadoop/test/dir"; 
		Boolean forceDelete = false; 
		try 
        {
			/* 判斷目錄是否存在,不存在則創(chuàng)建,存在則刪除 */
			if ( !HDFSApi7.test(conf, remoteDir) ) 
            {
				HDFSApi7.mkdir(conf, remoteDir); //創(chuàng)建
				System.out.println("創(chuàng)建目錄: " + remoteDir);
			} 
            else 
            {
				if ( HDFSApi7.isDirEmpty(conf, remoteDir) || forceDelete ) 
                { // 目錄為空或強(qiáng)制刪除
					HDFSApi7.rmDir(conf, remoteDir);
					System.out.println("刪除目錄: " + remoteDir);
				} 
                else 
                { // 目錄不為空
					System.out.println("目錄不為空,不刪除: " + remoteDir);
				}
			}
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
	}
}

??HDFSApi8

8)向 HDFS 中指定的文件追加內(nèi)容,由用戶指定內(nèi)容追加到原有文件的開頭或結(jié)尾;

Shell命令

#追加到原文件末尾
cd /usr/local/hadoop
hdfs dfs -appendToFile local.txt text.txt
#追加到原文件開頭
hdfs dfs -get text.txt
cat text.txt >> local.txt
hdfs dfs -copyFromLocal -f local.txt text.txt

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
import java.util.Scanner;
public class HDFSApi8 
{
	/*
	 判斷路徑是否存在
	*/
	public static boolean test(Configuration conf, String path) throws IOException 
   	{
		 FileSystem fs = FileSystem.get(conf);
		 return fs.exists(new Path(path));
	}
	/*
	追加文本內(nèi)容
	*/
	public static void appendContentToFile(Configuration conf, String content, String remoteFilePath) throws IOException 
   	{
		 FileSystem fs = FileSystem.get(conf);//獲取數(shù)據(jù)
		 Path remotePath = new Path(remoteFilePath);//獲取路徑
		 /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */
		 FSDataOutputStream out = fs.append(remotePath);//追加文件內(nèi)容
		 out.write(content.getBytes());//把字符串轉(zhuǎn)化為字節(jié)數(shù)組
		 out.close();
		 fs.close();
    }
	/*
	 追加文件內(nèi)容
	*/
	public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException 
   	{
		 FileSystem fs = FileSystem.get(conf);//獲取數(shù)據(jù)
		 Path remotePath = new Path(remoteFilePath);//獲取路徑
		 /* 創(chuàng)建一個(gè)文件讀入流 */
		 FileInputStream in = new FileInputStream(localFilePath);
		 /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */
		 FSDataOutputStream out = fs.append(remotePath);
		 /* 讀寫文件內(nèi)容 */
		 byte[] data = new byte[1024];
		 int read = -1;
		 if(in!=null)
         {
			while ( (read = in.read(data)) > 0 ) 
            {
                out.write(data, 0, read);
			}
		 }
		 out.close();
		 in.close();
		 fs.close();
	}
	/*
	 移動(dòng)文件到本地
	 移動(dòng)后,刪除源文件
	*/
	public static void moveToLocalFile(Configuration conf, String remoteFilePath, String localFilePath) throws IOException 
   	{
		 FileSystem fs = FileSystem.get(conf);//獲取數(shù)據(jù)
		 Path remotePath = new Path(remoteFilePath);//獲取路徑
		 Path localPath = new Path(localFilePath);//獲取路徑
		 fs.moveToLocalFile(remotePath, localPath);//移動(dòng)文件到本地
    }
	/*
	 創(chuàng)建文件
	*/
	public static void touchz(Configuration conf, String remoteFilePath) throws IOException 
   	{ 
		 FileSystem fs = FileSystem.get(conf);//獲取數(shù)據(jù)
		 Path remotePath = new Path(remoteFilePath);//獲取路徑
		 FSDataOutputStream outputStream = fs.create(remotePath);//創(chuàng)建輸出文件
		 outputStream.close();
		 fs.close();
	 }
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
   	{
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000"); 
        conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
		String remoteFilePath = "/usr/local/hadoop/text.txt"; // HDFS 文件,output file
		String content = "新追加的內(nèi)容\n";
		String choice = "after"; //追加到文件末尾
		String choice2 = "before"; //追加到文件開頭
		Scanner in=new Scanner(System.in);//輸入流
		String a=in.nextLine();
		boolean a1= a.contentEquals(choice2);//判斷內(nèi)容是否相等
		boolean a2=a.contentEquals(choice);//判斷內(nèi)容是否相等
		try 
       	{
			/* 判斷文件是否存在 */
			if ( !HDFSApi8.test(conf, remoteFilePath) ) 
           	{//路徑不存在
				System.out.println("文件不存在: " + remoteFilePath);
			} 
           else 
           {//有這個(gè)文件
				if ( a2 ) 
               	{ // 追加在文件末尾
					HDFSApi8.appendContentToFile(conf, content, remoteFilePath);
					System.out.println("已追加內(nèi)容到文件末尾" + remoteFilePath);
				} 
               	else if ( a1 ) 
               	{ 	//追加到文件開頭
					/* 沒有相應(yīng)的 api 可以直接操作,因此先把文件移動(dòng)到本地*/
					/*創(chuàng)建一個(gè)新的 HDFS,再按順序追加內(nèi)容 */
					String localTmpPath = "/usr/local/hadoop/tmp.txt";
					// 移動(dòng)到本地
					HDFSApi8.moveToLocalFile(conf, remoteFilePath, localTmpPath);
					// 創(chuàng)建一個(gè)新文件
					HDFSApi8.touchz(conf, remoteFilePath); 
					// 先寫入新內(nèi)容
					HDFSApi8.appendContentToFile(conf, content, remoteFilePath);
					// 再寫入原來內(nèi)容
					HDFSApi8.appendToFile(conf, localTmpPath, remoteFilePath); 
					System.out.println("已追加內(nèi)容到文件開頭: " + remoteFilePath);
				}
			}
		} 
       	catch (Exception e) 
       	{
			e.printStackTrace();
		}
	}
}

??HDFSApi9

9)刪除 HDFS 中指定的文件;

Shell命令

hdfs dfs -rm text.txt

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
public class HDFSApi9 
{
	/*
	刪除文件
	*/
	public static boolean rm(Configuration conf, String remoteFilePath) throws IOException 
    {
		 FileSystem fs = FileSystem.get(conf);
		 Path remotePath = new Path(remoteFilePath);
		 boolean result = fs.delete(remotePath, false);
		 fs.close();
		 return result;
	}
	 
	/*
	主函數(shù)
	*/
	public static void main(String[] args) 
    {
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000"); 
		String remoteFilePath = "/usr/local/hadoop/text.txt"; // HDFS 文件
		try 
        {
			if ( HDFSApi9.rm(conf, remoteFilePath) ) 
            {
				System.out.println("文件刪除: " + remoteFilePath);
			} 
            else 
            {
				System.out.println("操作失敗(文件不存在或刪除失?。?);
			}
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
	}
}

??HDFSApi10

10)在 HDFS 中,將文件從源路徑移動(dòng)到目的路徑。

Shell命令

第九題剛好把text.txt刪了,這里用的是上頭建的dir(shell和編程實(shí)現(xiàn)移來移去了屬于是

hdfs dfs -mv /usr/dir /usr/hadoop/test

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作


編程實(shí)現(xiàn)

(兩種情況)

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.util.Scanner;

public class HDFSApi10 
{
	public static void main(String[] args) 
	{
        try 
        {
            Scanner in = new Scanner(System.in);
            System.out.println("請(qǐng)輸入你要移動(dòng)的文件地址");
            Path srcPath = new Path(in.next());
            System.out.println("請(qǐng)輸入你的文件移動(dòng)目的地址");
            Path dstPath = new Path(in.next());

            Configuration conf = new Configuration();     
    		conf.set("fs.default.name","hdfs://localhost:9000"); 
            FileSystem fs = FileSystem.get(conf);

            if (!fs.exists(srcPath)) 
            {
                System.out.println("操作失敗(源文件不存在或移動(dòng)失敗)");
            } 
            else 
            {
                fs.rename(srcPath, dstPath);
                System.out.println(" 將文件 " + srcPath + " 移動(dòng)到 " +dstPath);
            }
            in.close();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

把HDFSApi(就第一題)編程部分重新跑一邊后邊的就能重新調(diào)試。文章來源地址http://www.zghlxwxcb.cn/news/detail-430845.html

到了這里,關(guān)于大數(shù)據(jù) | 實(shí)驗(yàn)一:大數(shù)據(jù)系統(tǒng)基本實(shí)驗(yàn) | 熟悉常用的HDFS操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 大數(shù)據(jù)實(shí)驗(yàn) 實(shí)驗(yàn)二:熟悉HDFS常用操作

    大數(shù)據(jù)實(shí)驗(yàn) 實(shí)驗(yàn)二:熟悉HDFS常用操作

    附件中有word版本的實(shí)驗(yàn)報(bào)告 理解HDFS在Hadoop體系結(jié)構(gòu)中的角色。 熟練使用HDFS操作常用的Shell命令。 熟悉HDFS操作常用的Java API。 Oracle VM VirtualBox虛擬機(jī) 系統(tǒng)版本centos7 JDK1.8版本 Hadoop-3.1.3 Windows11 Java IDE:IDEA 1.向HDFS中上傳任意文本文件,如果指定的文件在HDFS中已經(jīng)存在,由用戶

    2024年04月12日
    瀏覽(27)
  • 實(shí)驗(yàn)二、熟悉常用的HDFS操作(HDFS JavaAPI之讀取/上傳/刪除文件)

    實(shí)驗(yàn)二、熟悉常用的HDFS操作(HDFS JavaAPI之讀取/上傳/刪除文件)

    理解HDFS在Hadoop體系結(jié)構(gòu)中的角色 熟練使用HDFS操作常用的shell命令 熟悉HDFS操作常用的Java API 操作系統(tǒng):CentOS 8 Hadoop版本:3.3.1 jdk版本:1.8 Java IDE:Eclipse 1. 使用Hadoop命令操作分布式文件系統(tǒng)。 新建目錄 在本地和hadoop中分別創(chuàng)建文件夾: 在本地創(chuàng)建目錄: Hadoop創(chuàng)建目錄: 上

    2023年04月08日
    瀏覽(29)
  • Hadoop 使用Linux操作系統(tǒng)與Java熟悉常用的HDFS操作

    Hadoop 使用Linux操作系統(tǒng)與Java熟悉常用的HDFS操作

    注意看評(píng)論區(qū)獲取完整代碼資料 目錄 一、實(shí)驗(yàn)?zāi)康?二、實(shí)驗(yàn)平臺(tái) 三、實(shí)驗(yàn)步驟 理解HDFS在Hadoop體系結(jié)構(gòu)中的角色; 熟練使用HDFS操作常用的Shell命令; 熟悉HDFS操作常用的Java API。 操作系統(tǒng):Linux(建議Ubuntu16.04); Hadoop版本:2.7.1; JDK版本:1.8或以上版本; Java IDE:Eclipse。

    2024年02月03日
    瀏覽(22)
  • 大數(shù)據(jù)編程實(shí)驗(yàn)一:HDFS常用操作和Spark讀取文件系統(tǒng)數(shù)據(jù)

    大數(shù)據(jù)編程實(shí)驗(yàn)一:HDFS常用操作和Spark讀取文件系統(tǒng)數(shù)據(jù)

    這是我們大數(shù)據(jù)專業(yè)開設(shè)的第二門課程——大數(shù)據(jù)編程,使用的參考書是《Spark編程基礎(chǔ)》,這門課跟大數(shù)據(jù)技術(shù)基礎(chǔ)是分開學(xué)習(xí)的,但這門課是用的我們自己在電腦上搭建的虛擬環(huán)境進(jìn)行實(shí)驗(yàn)的,不是在那個(gè)平臺(tái)上,而且搭建的還是偽分布式,這門課主要偏向于有關(guān)大數(shù)據(jù)

    2024年04月10日
    瀏覽(26)
  • 熟悉常用的HDFS操作(附錄HDFS常用命令)

    熟悉常用的HDFS操作(附錄HDFS常用命令)

    1、理解HDFS在Hadoop體系結(jié)構(gòu)中的角色; 2、熟練使用HDFS操作常用的Shell命令; 3、熟悉HDFS操作常用的Java API 1、編程實(shí)現(xiàn)指定功能,并利用Hadoop提供的Shell命令完成相同任務(wù): 2、編程實(shí)現(xiàn)一個(gè)類“MyFSDataInputStream”,該類繼承“org.apache.hadoop.fs.FSDataInputStream”。 編程實(shí)現(xiàn)以下指定

    2023年04月09日
    瀏覽(30)
  • 云計(jì)算于大數(shù)據(jù)入門實(shí)驗(yàn)三——熟悉常用的 HBase 操作

    云計(jì)算于大數(shù)據(jù)入門實(shí)驗(yàn)三——熟悉常用的 HBase 操作

    理解HBase在Hadoop體系結(jié)構(gòu)中的角色 熟練使用HBase操作常用的shell命令 熟悉HBase操作常用的Java API 保存程序,并自行存檔 最終的程序都必須經(jīng)過測(cè)試,驗(yàn)證是正確的 按照實(shí)驗(yàn)報(bào)告格式,認(rèn)真記錄實(shí)驗(yàn)過程及結(jié)果,回答實(shí)驗(yàn)報(bào)告中的問題。實(shí)驗(yàn)報(bào)告模板在學(xué)習(xí)通的資料里面下載。

    2024年02月05日
    瀏覽(19)
  • 實(shí)驗(yàn)三:熟悉常用的HBase操作

    實(shí)驗(yàn)三:熟悉常用的HBase操作

    完整原版實(shí)驗(yàn)報(bào)告word文件:實(shí)驗(yàn)三:熟悉常用的HBase操作 ————————————————————————————————— \\\"大數(shù)據(jù)技術(shù)原理與應(yīng)用\\\"課程實(shí)驗(yàn)報(bào)告 題目:實(shí)驗(yàn)三:熟悉常用的HBase操作 姓名:朱小凡 日期:2022/3/29 1、實(shí)驗(yàn)環(huán)境: 設(shè)備名稱 LAPTOP-9KJS8HO

    2023年04月08日
    瀏覽(72)
  • 實(shí)驗(yàn)03熟悉常用的HBase操作

    實(shí)驗(yàn)03熟悉常用的HBase操作

    ?? Hbase相關(guān)配置參考網(wǎng)址: ??http://dblab.xmu.edu.cn/blog/install-hbase/ 1?? 理解HBase在Hadoop體系結(jié)構(gòu)中的角色; 2?? 熟練使用HBase操作常用的Shell命令; 3?? 熟悉HBase操作常用的Java API; 操作系統(tǒng):Linux 虛擬機(jī) :ubuntu Hadoop版本:2.7.1 HBase版本:1.1.2 JDK版本:1.8版本 Java IDE:ID

    2023年04月12日
    瀏覽(25)
  • 實(shí)驗(yàn)六:熟悉Hive的基本操作

    由于CSDN上傳md文件總是會(huì)使圖片失效 完整的實(shí)驗(yàn)文檔地址如下: https://download.csdn.net/download/qq_36428822/85709631?spm=1001.2014.3001.5501 題目:實(shí)驗(yàn)六:熟悉Hive的基本操作 姓名:小豬豬 日期:2022/5/15 設(shè)備名稱 LAPTOP-9KJS8HO6 處理器 Intel? Core? i5-10300H CPU @ 2.50GHz 2.50 GHz 機(jī)帶 RAM 16.0 GB (1

    2024年02月08日
    瀏覽(27)
  • 實(shí)驗(yàn)五 熟悉 Hive 的基本操作

    實(shí)驗(yàn)五 熟悉 Hive 的基本操作

    實(shí)驗(yàn)環(huán)境: 1.操作系統(tǒng):CentOS 7。 2.Hadoop 版本:3.3.0。 3.Hive 版本:3.1.2。 4.JDK 版本:1.8。 實(shí)驗(yàn)內(nèi)容與完成情況: (1)創(chuàng)建一個(gè)內(nèi)部表 stocks,字段分隔符為英文逗號(hào),表結(jié)構(gòu)如表 A-6 所示。 打開數(shù)據(jù)庫 (2)創(chuàng)建一個(gè)外部分區(qū)表 dividends(分區(qū)字段為 exchange 和 symbol),字段分

    2024年02月05日
    瀏覽(90)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包