??實(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沒開
echo $?
返回上一個(gè)命令的狀態(tài),0表示沒有錯(cuò)誤,其他任何值表明有錯(cuò)誤(這里顯然出錯(cuò),因?yàn)檫€沒有建text.txt文件夾),手動(dòng)建一個(gè)text.txt然后拖到/usr/local/hadoop。
用戶可以選擇追加到原來文件末尾或者覆蓋原來文件
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#覆蓋原來文件,第二種命令形式
這樣會(huì)自動(dòng)建一個(gè)local.txt文件
實(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í)現(xiàn)
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í)現(xiàn)
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
剛開始先跑shell,運(yùn)行不報(bào)錯(cuò),但無內(nèi)容輸出(但txt里是有內(nèi)容的)。編程實(shí)現(xiàn)跑了一遍,再回去跑shell就有輸出了(?
編程實(shí)現(xiàn)
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í)現(xiàn)
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í)現(xiàn)
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í)現(xiàn)
路徑存在的情況(以下代碼是路徑存在的情況)
路徑不存在的情況
目錄不存在的情況
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í)現(xiàn)
目錄不存在于是創(chuàng)建
目錄存在且為空,刪除
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í)現(xiàn)
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í)現(xiàn)
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í)現(xiàn)
(兩種情況)
文章來源:http://www.zghlxwxcb.cn/news/detail-430845.html
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)!