HDFS中JAVA API的使用
HDFS是一个分布式文件系统,既然是文件系统,就可以对其文件进行操作,比如说新建文件、删除文件、读取文件内容等操作。下面记录一下使用JAVAAPI对HDFS中的文件进行操作的过程。
对分HDFS中的文件操作主要涉及一下几个类:
Configuration类:该类的对象封转了客户端或者服务器的配置。
FileSystem类:该类的对象是一个文件系统对象,可以用该对象的一些方法来对文件进行操作。FileSystemfs=FileSystem.get(conf);通过FileSystem的静态方法get获得该对象。
FSDataInputStream和FSDataOutputStream:这两个类是HDFS中的输入输出流。分别通过FileSystem的open方法和create方法获得。
具体如何对文件操作清下下面例子:
packagecom.hdfs;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FSDataOutputStream;
importorg.apache.hadoop.fs.FileStatus;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.IOUtils;
publicclassHdfsTest{
//创建新文件
publicstaticvoidcreateFile(Stringdst,byte[]contents)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
PathdstPath=newPath(dst);//目标路径
//打开一个输出流
FSDataOutputStreamoutputStream=fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("文件创建成功!");
}
//上传本地文件
publicstaticvoiduploadFile(Stringsrc,Stringdst)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
PathsrcPath=newPath(src);//原路径
PathdstPath=newPath(dst);//目标路径
//调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
fs.copyFromLocalFile(false,srcPath,dstPath);
//打印文件路径
System.out.println("Uploadto"+conf.get("fs.default.name"));
System.out.println("------------listfiles------------"+"\n");
FileStatus[]fileStatus=fs.listStatus(dstPath);
for(FileStatusfile:fileStatus)
{
System.out.println(file.getPath());
}
fs.close();
}
//文件重命名
publicstaticvoidrename(StringoldName,StringnewName)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
PatholdPath=newPath(oldName);
PathnewPath=newPath(newName);
booleanisok=fs.rename(oldPath,newPath);
if(isok){
System.out.println("renameok!");
}else{
System.out.println("renamefailure");
}
fs.close();
}
//删除文件
publicstaticvoiddelete(StringfilePath)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
Pathpath=newPath(filePath);
booleanisok=fs.deleteOnExit(path);
if(isok){
System.out.println("deleteok!");
}else{
System.out.println("deletefailure");
}
fs.close();
}
//创建目录
publicstaticvoidmkdir(Stringpath)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
PathsrcPath=newPath(path);
booleanisok=fs.mkdirs(srcPath);
if(isok){
System.out.println("createdirok!");
}else{
System.out.println("createdirfailure");
}
fs.close();
}
//读取文件的内容
publicstaticvoidreadFile(StringfilePath)throwsIOException{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
PathsrcPath=newPath(filePath);
InputStreamin=null;
try{
in=fs.open(srcPath);
IOUtils.copyBytes(in,System.out,4096,false);//复制到标准输出流
}finally{
IOUtils.closeStream(in);
}
}
publicstaticvoidmain(String[]args)throwsIOException{
//测试上传文件
//uploadFile("D:\\c.txt","/user/hadoop/test/");
//测试创建文件
/*byte[]contents="helloworld世界你好\n".getBytes();
createFile("/user/hadoop/test1/d.txt",contents);*/
//测试重命名
//rename("/user/hadoop/test/d.txt","/user/hadoop/test/dd.txt");
//测试删除文件
//delete("test/dd.txt");//使用相对路径
//delete("test1");//删除目录
//测试新建目录
//mkdir("test1");
//测试读取文件
readFile("test1/d.txt");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。