實驗二HDFS程式設計實踐
一、程式設計實現以下指定功能,並利用Hadoop提供的Shell命令完成相同的任務。
(1)向HDFS中上傳任意文字檔案,如果指定的檔案在HDFS中已經存在,由使用者指定是追加到原有檔案末尾還是覆蓋原有的檔案;
if $(hadoop fs -test -e text.txt);
then $(hadoop fs -appendToFile local.txt /Test/text.txt);
else $(hadoop fs -copyFromLocal -f local.txt /Test/text.txt);
fi
Java程式碼:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFromLocalFile {
/**
* 判斷路徑是否存在
*/
public static boolean test(Configuration conf, String path) {
try (FileSystem fs = FileSystem.get(conf)) {
return fs.exists(new Path(path));
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 複製檔案到指定路徑 若路徑已存在,則進行覆蓋
*/
public static void copyFromLocalFile(Configuration conf,
String localFilePath, String remoteFilePath) {
Path localPath = new Path(localFilePath);
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
/* fs.copyFromLocalFile 第一個引數表示是否刪除原始檔,第二個引數表示是否覆蓋 */
fs.copyFromLocalFile(false, true, localPath, remotePath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 追加檔案內容
*/
public static void appendToFile(Configuration conf, String localFilePath,
String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf);
FileInputStream in = new FileInputStream(localFilePath);) {
FSDataOutputStream out = fs.append(remotePath);
byte[] data = new byte[1024];
int read = -1;
while ((read = in.read(data)) > 0) {
out.write(data, 0, read);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String localFilePath = "/usr/local/hadoop/text.txt"; // 本地路徑
String remoteFilePath = "/user/tiny/text.txt"; // HDFS路徑
// String choice = "append"; // 若檔案存在則追加到檔案末尾
String choice = "overwrite"; // 若檔案存在則覆蓋
try {
/* 判斷檔案是否存在 */
boolean fileExists = false;
if (CopyFromLocalFile.test(conf, remoteFilePath)) {
fileExists = true;
System.out.println(remoteFilePath + " 已存在.");
} else {
System.out.println(remoteFilePath + " 不存在.");
}
/* 進行處理 */
if (!fileExists) { // 檔案不存在,則上傳
CopyFromLocalFile.copyFromLocalFile(conf, localFilePath,
remoteFilePath);
System.out.println(localFilePath + " 已上傳至 " + remoteFilePath);
} else if (choice.equals("overwrite")) { // 選擇覆蓋
CopyFromLocalFile.copyFromLocalFile(conf, localFilePath,
remoteFilePath);
System.out.println(localFilePath + " 已覆蓋 " + remoteFilePath);
} else if (choice.equals("append")) { // 選擇追加
CopyFromLocalFile.appendToFile(conf, localFilePath,
remoteFilePath);
System.out.println(localFilePath + " 已追加至 " + remoteFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2)從HDFS中下載指定檔案,如果本地檔案與要下載的檔名稱相同,則自動對下載的檔案重新命名;
if $(hadoop fs -test -e /usr/local/hadoop/text.txt);
then $(hadoop fs -copyToLocal /Test/text.txt ./text.txt);
else $(hadoop fs -copyToLocal /Test/text.txt ./text2.txt);
fi
Java程式碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;
import java.io.*;
public class CopyToLocal {
/**
* 下載檔案到本地 判斷本地路徑是否已存在,若已存在,則自動進行重新命名
*/
public static void copyToLocal(Configuration conf, String remoteFilePath,
String localFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
File f = new File(localFilePath);
/* 如果檔名存在,自動重新命名(在檔名後面加上 _0, _1 ...) */
if (f.exists()) {
System.out.println(localFilePath + " 已存在.");
Integer i = Integer.valueOf(0);
while (true) {
f = new File(localFilePath + "_" + i.toString());
if (!f.exists()) {
localFilePath = localFilePath + "_" + i.toString();
break;
} else {
i++;
continue;
}
}
System.out.println("將重新命名為: " + localFilePath);
}
// 下載檔案到本地
Path localPath = new Path(localFilePath);
fs.copyToLocalFile(remotePath, localPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String localFilePath = "/usr/local/hadoop/text.txt"; // 本地路徑
String remoteFilePath = "/user/tiny/text.txt"; // HDFS路徑
try {
CopyToLocal.copyToLocal(conf, remoteFilePath, localFilePath);
System.out.println("下載完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
(3)將HDFS中指定檔案的內容輸出到終端中;
hadoop fs -cat /Test/text.txt
Java程式碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;
import java.io.*;
public class Cat {
/**
* 讀取檔案內容
*/
public static void cat(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(remotePath);
BufferedReader d = new BufferedReader(new InputStreamReader(in));) {
String line;
while ((line = d.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteFilePath = "/user/tiny/input/text.txt"; // HDFS路徑
try {
System.out.println("讀取檔案: " + remoteFilePath);
Cat.cat(conf, remoteFilePath);
System.out.println("\n讀取完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
(4)顯示HDFS中指定的檔案的讀寫許可權、大小、建立時間、路徑等資訊;
hadoop fs -ls -h /Test/text.txt
Java程式碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;
import java.io.*;
import java.text.SimpleDateFormat;
public class List {
/**
* 顯示指定檔案的資訊
*/
public static void ls(Configuration conf, String remoteFilePath) {
try (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("許可權: " + s.getPermission().toString());
System.out.println("大小: " + s.getLen());
/* 返回的是時間戳,轉化為時間日期格式 */
long timeStamp = s.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String date = format.format(timeStamp);
System.out.println("時間: " + date);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteFilePath = "/user/tiny/text.txt"; // HDFS路徑
try {
System.out.println("讀取檔案資訊: " + remoteFilePath);
List.ls(conf, remoteFilePath);
System.out.println("\n讀取完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
(5)給定HDFS中某一個目錄,輸出該目錄下的所有檔案的讀寫許可權、大小、建立時間、路徑等資訊,如果該檔案是目錄,則遞迴輸出該目錄下所有檔案相關資訊;
hadoop fs -ls -R -h /Test
Java程式碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;
import java.io.*;
import java.text.SimpleDateFormat;
public class ListDir {
/**
* 顯示指定資料夾下所有檔案的資訊(遞迴)
*/
public static void lsDir(Configuration conf, String remoteDir) {
try (FileSystem fs = FileSystem.get(conf)) {
Path dirPath = new Path(remoteDir);
/* 遞迴獲取目錄下的所有檔案 */
RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(
dirPath, true);
/* 輸出每個檔案的資訊 */
while (remoteIterator.hasNext()) {
FileStatus s = remoteIterator.next();
System.out.println("路徑: " + s.getPath().toString());
System.out.println("許可權: " + s.getPermission().toString());
System.out.println("大小: " + s.getLen());
/* 返回的是時間戳,轉化為時間日期格式 */
Long timeStamp = s.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String date = format.format(timeStamp);
System.out.println("時間: " + date);
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteDir = "/user/tiny"; // HDFS路徑
try {
System.out.println("(遞迴)讀取目錄下所有檔案的資訊: " + remoteDir);
ListDir.lsDir(conf, remoteDir);
System.out.println("讀取完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
(6)提供一個HDFS內的檔案的路徑,對該檔案進行建立和刪除操作。如果檔案所在目錄不存在,則自動建立目錄;
#!/bin/bash
if $(hadoop fs -test -d /Test/test1);
then $(hadoop fs -touchz /Test/test1/text1.txt);
else $(hadoop fs -mkdir -p /Test/test1 && hdfs dfs -touchz /Test/test1/text1.txt);
fi
刪除檔案:hdfs dfs -rm dir1/dir2/filename
Java程式碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;
public class RemoveOrMake {
/**
* 判斷路徑是否存在
*/
public static boolean test(Configuration conf, String path) {
try (FileSystem fs = FileSystem.get(conf)) {
return fs.exists(new Path(path));
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 建立目錄
*/
public static boolean mkdir(Configuration conf, String remoteDir) {
try (FileSystem fs = FileSystem.get(conf)) {
Path dirPath = new Path(remoteDir);
return fs.mkdirs(dirPath);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 建立檔案
*/
public static void touchz(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
FSDataOutputStream outputStream = fs.create(remotePath);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 刪除檔案
*/
public static boolean rm(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
return fs.delete(remotePath, false);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 主函式
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteFilePath = "/user/tiny/input/text.txt"; // HDFS路徑
String remoteDir = "/user/tiny/input"; // HDFS路徑對應的目錄
try {
/* 判斷路徑是否存在,存在則刪除,否則進行建立 */
if (RemoveOrMake.test(conf, remoteFilePath)) {
RemoveOrMake.rm(conf, remoteFilePath); // 刪除
System.out.println("刪除檔案: " + remoteFilePath);
} else {
if (!RemoveOrMake.test(conf, remoteDir)) { // 若目錄不存在,則進行建立
RemoveOrMake.mkdir(conf, remoteDir);
System.out.println("建立資料夾: " + remoteDir);
}
RemoveOrMake.touchz(conf, remoteFilePath);
System.out.println("建立檔案: " + remoteFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
(7)提供一個HDFS的目錄的路徑,對該目錄進行建立和刪除操作。建立目錄時,如果目錄檔案所在目錄不存在則自動建立相應目錄;刪除目錄時,由使用者指定當該目錄不為空時是否還刪除該目錄;
#!/bin/bash
if $(hadoop fs -test -d /Test/test1);
then $(hadoop fs -touchz /Test/test1/text1.txt);
else $(hadoop fs -mkdir -p /Test/test1 && hdfs dfs -touchz /Test/test1/text1.txt);
fi
建立目錄:hdfs dfs -mkdir -p dir1/dir2
刪除目錄(如果目錄非空則會提示not empty,不執行刪除):hdfs dfs -rmdir dir1/dir2
強制刪除目錄:hdfs dfs -rm -R dir1/dir2
(8)向HDFS中指定的檔案追加內容,由使用者指定內容追加到原有檔案的開頭或結尾;
追加到檔案末尾:hdfs dfs -appendToFile local.txt text.txt
追加到檔案開頭(先移動到本地進行操作,再進行上傳覆蓋):
hdfs dfs -get text.txt
cat text.txt >> local.txt
hdfs dfs -copyFromLocal -f text.txt text.txt
(9)刪除HDFS中指定的檔案;
hdfs dfs -rm text.txt
(10)刪除HDFS中指定的目錄,由使用者指定目錄中如果存在檔案時是否刪除目錄;
刪除目錄(如果目錄非空則會提示not empty,不執行刪除):hdfs dfs -rmdir dir1/dir2
強制刪除目錄:hdfs dfs -rm -R dir1/dir2
(11)在HDFS中,將檔案從源路徑移動到目的路徑。
hdfs dfs -mv text.txt text2.txt
二、程式設計實現一個類“MyFSDataInputStream”,該類繼承“org.apache.hadoop.fs.FSDataInputStream”,要求如下:實現按行讀取HDFS中指定檔案的方法“readLine()”,如果讀到檔案末尾,則返回空,否則返回檔案一行的文字。
三、檢視Java幫助手冊或其它資料,用“java.net.URL”和“org.apache.hadoop.fs.FsURLStreamHandlerFactory”程式設計完成輸出HDFS中指定檔案的文字到終端中。
Hadoop介面實現