1.上传文件到HDFS
通过"FileSystem.copyFromLocalFile(String src,String dst)"可将本地文件上传到HDFS的制定位置上,其中src和dst均为文件的完整路径。具体事例如下:
public static void uploadFile(String src,String dst) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(src); //原路径
Path dstPath = new Path(dst); //目标路径
//调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
fs.copyFromLocalFile(false,srcPath, dstPath);
//打印文件路径
System.out.println("Upload to "+conf.get("fs.default.name"));
System.out.println("------------list files------------"+"\n");
FileStatus [] fileStatus = fs.listStatus(dstPath);
for (FileStatus file : fileStatus)
{
System.out.println(file.getPath());
}
fs.close();
}
2.创建HDFS文件
通过"FileSystem.create(,String dst)"可创建指定HDFS文件,其中dst为文件的完整路径。具体事例如下:
public static void createFile(String dst , byte[] contents) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst); //目标路径
//打开一个输出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("文件创建成功!");
}
3.查看HDFS文件的最后修改时间
通过"FileSystem.getModificationTime()"可查看指定HDFS文件的修改时间。具体实现如下:
public class GetLTime {
public static void getLTime () throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path fpath =new Path("/user/hadoop/test/file1.txt");
FileStatus fileStatus=hdfs.getFileStatus(fpath);
long modiTime=fileStatus.getModificationTime();
System.out.println("file1.txt的修改时间是"+modiTime);
}
}
4.删除HDFS文件
通过"FileSystem.deleteOnExit(String filePath)"可重命名指定的HDFS文件。filePath为hdfs文件的完整路径。具体实现如下:
public static void delete(String filePath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean isok = fs.deleteOnExit(path);
if(isok){
System.out.println("delete ok!");
}else{
System.out.println("delete failure");
}
fs.close();
}
5.HDFS文件重命名
通过"FileSystem.rename(String oldName,String newName)"可重命名指定的HDFS文件。oldName和newName都为hdfs文件的完整路径。具体实现如下:
public static void rename(String oldName,String newName) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean isok = fs.rename(oldPath, newPath);
if(isok){
System.out.println("rename ok!");
}else{
System.out.println("rename failure");
}
fs.close();
}
6.创建HDFS目录
通过"FileSystem.mkdirs(String path)"可创建指定的HDFS目录。path为hdfs目录的完整路径。具体实现如下:
public static void mkdir(String path) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(path);
boolean isok = fs.mkdirs(srcPath);
if(isok){
System.out.println("create dir ok!");
}else{
System.out.println("create dir failure");
}
fs.close();
}
7.读取HDFS文件的内容
通过"FileSystem.open(String path)"可读取指定的HDFS文件。path为hdfs文件的完整路径。具体实现如下:
public static void readFile(String filePath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(filePath);
InputStream in = null;
try {
in = fs.open(srcPath);
IOUtils.copyBytes(in, System.out, 4096, false); //复制到标准输出流
} finally {
IOUtils.closeStream(in);
}
}
8.根据filter获取HDFS目录下的文件
通过"FileSystem.listStatus(String path,PathFilter pathFilter)"可读取指定的HDFS文件。path为hdfs目录的完整路径。具体实现如下:
public static String[] ListFile(String path,PathFilter pathFilter) {
String[] files = new String[0];
try {
// 返回FileSystem对象
FileSystem fs = getFileSystem();
String hdfsUri = HDFSUri;
if(StringUtils.isNotBlank(hdfsUri)){
path = hdfsUri + path;
}
FileStatus[] status;
if(pathFilter != null){
// 根据filter列出目录内容
status = fs.listStatus(new Path(path),pathFilter);
}else{
// 列出目录内容
status = fs.listStatus(new Path(path));
}
// 获取目录下的所有文件路径
Path[] listedPaths = FileUtil.stat2Paths(status);
// 转换String[]
if (listedPaths != null && listedPaths.length > 0){
files = new String[listedPaths.length];
for (int i = 0; i < files.length; i++){
files[i] = listedPaths[i].toString();
}
}
// 释放资源
fs.close();
} catch (IllegalArgumentException | IOException e) {
logger.error("", e);
}
return files;
}
9.从 HDFS 下载文件
通过"FileSystem.copyToLocalFile(String srcFile,String destPath)"可读取指定的HDFS文件。srcFile为hdfs文件的完整路径,destPath为下载目标路径。具体实现如下:
public static void getFile(String srcFile,String destPath) {
// 源文件路径
String hdfsUri = HDFSUri;
if(StringUtils.isNotBlank(hdfsUri)){
srcFile = hdfsUri + srcFile;
}
Path srcPath = new Path(srcFile);
// 目的路径是Linux下的路径,如果在 windows 下测试,需要改写为Windows下的路径,比如D://hadoop/djt/
Path dstPath = new Path(destPath);
try {
// 获取FileSystem对象
FileSystem fs = getFileSystem();
// 下载hdfs上的文件
fs.copyToLocalFile(srcPath, dstPath);
// 释放资源
fs.close();
} catch (IOException e) {
logger.error("", e);
}
}
10.判断目录是否存在
通过"FileSystem.exists(String srcFile)"可读取指定的HDFS文件。srcFile为hdfs目录的完整路径。具体实现如下:
public boolean existDir(String filePath, boolean create){
boolean flag = false;
if (StringUtils.isEmpty(filePath)){
return flag;
}
try{
Path path = new Path(filePath);
// FileSystem对象
FileSystem fs = getFileSystem();
if (create){
if (!fs.exists(path)){
fs.mkdirs(path);
}
}
if (fs.isDirectory(path)){
flag = true;
}
}catch (Exception e){
logger.error("", e);
}
return flag;
}