HDFS操作實驗(hdfs檔案上傳、使用JavaAPI判斷檔案存在,檔案合併)
此部落格為博主學習總結,內容為博主完成本週大資料課程的實驗內容。實驗內容分為兩部分。
1. 在分散式檔案系統中建立檔案並用shell指令檢視;
2. 利用Java API程式設計實現判斷檔案是否存在和合並兩個檔案的內容成一個檔案。
感謝廈門大學資料庫實驗室的實驗教程,對博主的學習有很大的幫助。
現在,就讓我們一起完成實驗內容吧!
建立檔案
我們需要先啟動下Hadoop,【Ctrl】+【Alt】+【t】開啟終端,輸入一下命令:
$ cd /usr/local/hadoop/
$ ./sbin/start-dfs.sh
建立一個新使用者,在這裡我們起名為Hadoop,輸入一下指令:./bin/hdfs dfs –mkdir –p /user/hadoop
軟後在瀏覽器輸入:http://localhost:50070,進入‘Utilities’中的‘Browse the system’:
檢視剛才建立的user/haddop,點選uesr,檢視:
在本地Linux檔案系統的’/home/hadoop/‘目錄下建立兩個檔案AFile.txt、Bfile.txt:
touch /home/hadoop/AFile.txt
touch /home/hadoop/BFile.txt
裡面可以隨意輸入一些單詞,比如,第一個輸入姓名,第二個輸入一串號碼:
gedit /home/hadoop/AFile.txt
gedit /home/hadoop/BFile.txt
然後,可以使用如下命令把本地檔案系統的“/home/hadoop/AFile.txt”、“/home/hadoop/BFile.txt”上傳到HDFS中的當前使用者目錄的input目錄下,也就是上傳到HDFS的“/user/hadoop/”目錄下:
./bin/hdfs dfs -put /home/hadoop/AFile.txt
./bin/hdfs dfs -put /home/hadoop/BFile.txt
檢視剛才的兩個檔案上傳是否成功:
./bin/hdfs dfs -ls
同樣,我們也可以檢視http://localhost:50070中的‘Utilities’中的‘Browse the system’:
上傳成功後我們接下來就是檢視HDFS當前使用者中所擁有的檔案內容:
./bin/hdfs dfs –cat AFile.txt
./bin/hdfs dfs –cat BFile.txt
利用Java API程式設計實現
建立一個java Project,名字自擬。
右鍵單擊專案名稱,進入設定,
點選進入‘Add External JARs...’根據路徑加入以下的JAR包;
為了編寫一個能夠與HDFS互動的Java應用程式,一般需要向Java工程中新增以下JAR包:
(1)”/usr/local/hadoop/share/hadoop/common”目錄下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar;(2)/usr/local/hadoop/share/hadoop/common/lib”目錄下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/hdfs”目錄下的haoop-hdfs-2.7.1.jar和haoop-hdfs-nfs-2.7.1.jar;
(4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目錄下的所有JAR包。
新增完後應用並退出。
建立兩個class:
我們現在做第一個實驗,判斷檔案是否存在:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class cunzai{
public static void main(String[] args){
try{
String fileNameA = "AFile.txt";
String fileNameB = "BFile.txt";
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(fileNameA))){
System.out.println(fileNameA + " 檔案存在");
}else{
System.out.println(fileNameA + " 檔案不存在");
}
if(fs.exists(new Path(fileNameB))){
System.out.println(fileNameB + " 檔案存在");
}else{
System.out.println(fileNameB + " 檔案不存在");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
輸入以上程式碼後,然後右鍵程式碼部分空白處,選擇Debug As->Java Application
檢視結果:
執行成功後,我們下一步將將class檔案按一個可執行jar包的格式匯入到我們的hadoop路徑中,先建立一個路徑,在hadoop路徑下執行以下命令:
mkdir myapp
然後,我們回到eclipse中,右鍵單擊名為cunzai的class類:
點選export...;選擇Runnable JAR file:
選擇class檔名和檔案路徑:
點選ok,ok
我們回到終端檢查一下剛才匯出的jar包是否可以執行:
./bin/hadoop jar ./myapp/cunzai.jar
完成jar包讀取檔案任務。
=========================================================================
接下來我們來完成合並檔案的任務。
將以下程式碼複製到hebing.class中:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
public class hebing {
private static final String utf8 = "UTF-8";
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000");
conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
Path fileA = new Path("AFile.txt");
Path fileB = new Path("BFile.txt");
FSDataInputStream getItA = fs.open(fileA);
FSDataInputStream getItB = fs.open(fileB);
BufferedReader dA = new BufferedReader(new InputStreamReader(getItA));
BufferedReader dB = new BufferedReader(new InputStreamReader(getItB));
String contentA = dA.readLine(); //讀取檔案一行
System.out.println(contentA);
String contentB = dB.readLine(); //讀取檔案一行
System.out.println(contentB);
String mem = new String();
mem = contentA+contentB;
String filename = "hebing.txt"; //要寫入的檔名
FSDataOutputStream os = fs.create(new Path(filename));
os.writeUTF(mem);
System.out.println("Create: "+ filename);
os.close(); //關閉檔案os
dA.close(); //關閉檔案A
dB.close(); //關閉檔案B
fs.close(); //關閉hdfs
} catch (Exception e) {
e.printStackTrace();
}
}
}
依舊debug執行下,看下結果:
執行成功後我們接著將這個class匯出,路徑如下:
匯出成功後我們在終端執行下指令,執行下剛才匯出的jar包:
然後檢視一下我們剛才合成的新檔案內容:
檔案合成成功。
今日實驗到此完成,如若對實驗內容有異議,請評論留言,若有差池,環境大家批評指正,謝謝各位。