实验3熟悉常用的HDFS操作答案Word格式.docx
《实验3熟悉常用的HDFS操作答案Word格式.docx》由会员分享,可在线阅读,更多相关《实验3熟悉常用的HDFS操作答案Word格式.docx(37页珍藏版)》请在冰点文库上搜索。
![实验3熟悉常用的HDFS操作答案Word格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/7/3b4e482a-6f09-4d46-9998-c02659681abc/3b4e482a-6f09-4d46-9998-c02659681abc1.gif)
(1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;
Shell命令:
检查文件是否存在:
./hdfsdfs-test-etext.txt(执行完这一句不会输出结果,需要继续输入命令
"
echo$?
)
追加命令:
./hdfsdfs-appendTotext.txt
覆盖命令1:
./hdfsdfs-copyFromLocal-flocal.txttext.txt
覆盖命令2:
./hdfsdfs-cp-ftext.txt
也可以使用如下命令实现:
(如下代码可视为一行代码,在终端中输入第一行代码后,直到输入fi才会真正执行):
if$(./hdfsdfs-test-etext.txt);
then$(./hdfsdfs-appendTotext.txt);
else$(./hdfsdfs-copyFromLocal-flocal.txttext.txt);
fi
Java代码:
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.*;
importjava.io.*;
publicclassHDFSApi{
/**
*判断路径是否存在
*/
publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{
fs=(conf);
returnfs.exists(newPath(path));
}
*复制文件到指定路径
*若路径已存在,则进行覆盖
publicstaticvoidcopyFromLocalconf,Stringlocal,Stringremote)throwsIOException{
PathlocalPath=newPath(local);
PathremotePath=newPath(remote);
/*fs.copyFromLocalFile第一个参数表示是否删除源文件,第二个参数表示是否覆盖*/
fs.copyFromLocal,true,localPath,remotePath);
fs.close();
*追加文件内容
publicstaticvoidappendToconf,Stringlocal,Stringremote)throwsIOException{
/*创建一个文件读入流*/
in=new(local);
/*创建一个文件输出流,输出的内容将追加到文件末尾*/
FSDataOutputStreamout=fs.append(remotePath);
/*读写文件内容*/
byte[]data=newbyte[1024];
intread=-1;
while((read=in.read(data))>
0){
out.write(data,0,read);
out.close();
in.close();
*主函数
publicstaticvoidmain(String[]args){
Configurationconf=newConfiguration();
conf.set("
fs.default.name"
"
hdfs:
//localhost:
9000"
);
Stringlocal="
/home/hadoop/text.txt"
;
//本地路径
Stringremote="
/user/hadoop/text.txt"
//HDFS路径
Stringchoice="
append"
//若文件存在则追加到文件末尾
//Stringchoice="
overwrite"
//若文件存在则覆盖
try{
/*判断文件是否存在*/
Boolean=false;
if(HDFSApi.test(conf,remote)){
=true;
System.out.println(remote+"
已存在."
}else{
不存在."
}
/*进行处理*/
if(!
){//文件不存在,则上传
HDFSApi.copyFromLocal,local,remote);
System.out.println(local+"
已上传至"
+remote);
}elseif(choice.equals("
)){//选择覆盖
已覆盖"
)){//选择追加
HDFSApi.appendTo,local,remote);
已追加至"
}catch(Exceptione){
e.printStackTrace();
}
}
(2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
Shell命令:
if$(./hdfsdfs-test-e);
then$(./hdfsdfs-copyToLocaltext.txt./text2.txt);
else$(./hdfsdfs-copyToLocaltext.txt./text.txt);
*下载文件到本地
*判断本地路径是否已存在,若已存在,则自动进行重命名
publicstaticvoidcopyToLocal(Configurationconf,Stringremote,Stringlocal)throwsIOException{
Filef=new);
/*如果文件名存在,自动重命名(在文件名后面加上_0,_1...)*/
if(f.exists()){
System.out.println(local+"
Integeri=0;
while(true){
f=new+"
_"
+i.toString());
if(!
f.exists()){
local=local+"
+i.toString();
break;
System.out.println("
将重新命名为:
"
+local);
//下载文件到本地
fs.copyToLocal,localPath);
HDFSApi.copyToLocal(conf,remote,local);
System.out.println("
下载完成"
(3)将HDFS中指定文件的内容输出到终端中;
./hdfsdfs-cattext.txt
*读取文件内容
publicstaticvoidcat(Configurationconf,Stringremote)throwsIOException{
FSDataInputStreamin=fs.open(remotePath);
BufferedReaderd=newBufferedReader(newInputStreamReader(in));
Stringline=null;
while((line=d.readLine())!
=null){
System.out.println(line);
d.close();
读取文件:
HDFSApi.cat(conf,remote);
\n读取完成"
(4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;
./hdfsdfs-ls-htext.txt
importjava.text.SimpleDateFormat;
*显示指定文件的信息
publicstaticvoidls(Configurationconf,Stringremote)throwsIOException{
[]=fs.listStatus(remotePath);
for(s:
){
路径:
+s.getPath().toString());
权限:
+s.getPermission().toString());
大小:
+s.getLen());
/*返回的是时间戳,转化为时间日期格式*/
LongtimeStamp=s.getModificationTime();
SimpleDateFormatformat=newSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
Stringdate=format.format(timeStamp);
时间:
+date);
读取文件信息:
HDFSApi.ls(conf,remote);
(5)给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;
./hdfsdfs-ls-R-h/user/hadoop
*显示指定文件夹下所有文件的信息(递归)
publicstaticvoidlsDir(Configurationconf,StringremoteDir)throwsIOException{
PathdirPath=newPath(remoteDir);
/*递归获取目录下的所有文件*/
RemoteIterator<
Located>
remoteIterator=fs.listFiles(dirPath,true);
/*输出每个文件的信息*/
while(remoteIterator.hasNext()){
s=remoteIterator.next();
System.out.println("
System.out.println();
}
StringremoteDir="
/user/hadoop"
(递归)读取目录下所有文件的信息:
+remoteDir);
HDFSApi.lsDir(conf,remoteDir);
读取完成"
(6)提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。
如果文件所在目录不存在,则自动创建目录;
if$(./hdfsdfs-test-ddir1/dir2);
then$(./hdfsdfs-touchzdir1/dir2/);
else$(./hdfsdfs-mkdir-pdir1/dir2&
&
hdfsdfs-touchzdir1/dir2/);
删除文件:
./hdfsdfs-rmdir1/dir2/
*创建目录
publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException{
booleanresult=fs.mkdirs(dirPath);
returnresult;
*创建文件
publicstaticvoidtouchz(Configurationconf,Stringremote)throwsIOException{
FSDataOutputStreamoutputStream=fs.create(remotePath);
outputStream.close();
*删除文件
publicstaticbooleanrm(Configurationconf,Stringremote)throwsIOException{
booleanresult=fs.delete(remotePath,false);
/user/hadoop/input/text.txt"
/user/hadoop/input"
//HDFS路径对应的目录
/*判断路径是否存在,存在则删除,否则进行创建*/
if(HDFSApi.test(conf,remote)){
HDFSApi.rm(conf,remote);
//删除
System.out.println("
删除路径: