赞
踩
最近做springcloud项目开发,需要使用文件服务器HDFS进行文件的上传下载,所以将实现的方法步骤做个记录,分享出来,如有疑问,欢迎随时交流:
项目使用到Eureka作为注册中心,通过Feign客户端进行服务调用,废话少说,直接上代码
代码片
.
// application.yml配置文件 server: port: 8020 undertow: direct-buffers: true ### hadoop配置 hdfs: path: hdfs://Hadoop的ip:9000 username: Lee ###服务别名 spring: application: name: file-server servlet: multipart: enabled: true max-file-size: 50MB max-request-size: 50MB ###注册到eureka地址 eureka: client: service-url: defaultZone: http://127.0.0.1:8100/eureka #####是否自己注册 register-with-eureka: true ###是否需要从eureka上获取注册信息 fetch-registry: true instance: # ip-address: prefer-ip-address: true
// HDFSUtil工具类 package com.shyikun.impl; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import javax.annotation.PostConstruct; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.apache.hadoop.io.IOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class HDFSUtil { @Value("${hdfs.path}") private String path; private static String hdfsPath; /** * 获取HDFS配置信息 * @return */ private static Configuration getConfiguration() { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", hdfsPath); configuration.set("hadoop.home.dir", "D:/Hadoop/winutils-master/hadoop-3.0.0"); return configuration; } /** * 判断路径是否存在 * * @param conf * @param path * @return * @throws IOException */ public static boolean exits(String path) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); return fs.exists(new Path(path)); } /** * 判断文件是否存在 * @param hdfsPath * @return */ public static boolean checkFileExist(String hdfsPath) { FileSystem fs; boolean isExists = false; try { fs = FileSystem.get(URI.create(hdfsPath), getConfiguration()); Path delPath = new Path(hdfsPath); isExists = fs.exists(delPath); } catch (IOException e) { e.printStackTrace(); } return isExists; } /** * 删除目录或文件 * * @param conf * @param remoteFilePath * @param recursive * @return * @throws IOException */ public static boolean deleteFile(String remoteFilePath, boolean recursive) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); boolean result = fs.delete(new Path(remoteFilePath), recursive); fs.close(); return result; } /** * 删除目录或文件(如果有子目录,则级联删除) * * @param conf * @param remoteFilePath * @return * @throws IOException */ public static boolean deleteFile(String remoteFilePath) throws IOException { return deleteFile(remoteFilePath, true); } /** * 文件重命名 * * @param conf * @param oldFileName * @param newFileName * @return * @throws IOException */ public static boolean renameFile(String oldFileName, String newFileName) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); Path oldPath = new Path(oldFileName); Path newPath = new Path(newFileName); boolean result = fs.rename(oldPath, newPath); fs.close(); return result; } /** * 创建目录 * * @param conf * @param dirName * @return * @throws IOException */ public static boolean createDirectory(String dirName) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); Path dir = new Path(dirName); boolean result = false; if (!fs.exists(dir)) { result = fs.mkdirs(dir); } fs.close(); return result; } /** * 列出指定路径下的所有文件(不包含目录) * * @param conf * @param basePath * @param recursive */ public static RemoteIterator<LocatedFileStatus> listFiles(String basePath, boolean recursive) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); RemoteIterator<LocatedFileStatus> fileStatusRemoteIterator = fs.listFiles(new Path(basePath), recursive); return fileStatusRemoteIterator; } /** * 列出指定路径下的文件(非递归) * * @param conf * @param basePath * @return * @throws IOException */ public static RemoteIterator<LocatedFileStatus> listFiles(String basePath) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles( new Path(basePath), false); fs.close(); return remoteIterator; } /** * 列出指定目录下的文件\子目录信息(非递归) * * @param conf * @param dirPath * @return * @throws IOException */ public static FileStatus[] listStatus(String dirPath) throws IOException { FileSystem fs = FileSystem.get(getConfiguration()); FileStatus[] fileStatuses = fs.listStatus(new Path(dirPath)); fs.close(); return fileStatuses; } /** * 上传文件到hdfs * * @param fs * @param localFileName * @param hdfsFileName * @throws IOException */ public static void upload(FileSystem fs,String localFilePath,String hdfsFilePath) throws IOException { fs.copyFromLocalFile(new Path(localFilePath), new Path(hdfsFilePath)); } /** * File对象上传到hdfs * @param conf * @param uri * @param remote * @param local * @throws IOException */ public static void createFile(File localPath, String hdfsPath) throws IOException { InputStream in = null; try { FileSystem fs = FileSystem.get(getConfiguration()); FSDataOutputStream out = fs.create(new Path(hdfsPath)); in = new BufferedInputStream(new FileInputStream(localPath)); IOUtils.copyBytes(in, out, 4096, false); out.hsync(); out.close(); System.out.println("create file in hdfs:" + hdfsPath); } finally { IOUtils.closeStream(in); } } /** * 下载 hdfs上的文件 * * @param conf * @param uri * @param remote * @param local * @throws IOException */ public static void download(String remote, String local) throws IOException { FileSystem fs = FileSystem.get(URI.create(remote), getConfiguration()); Path path = new Path(remote); fs
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。