Springcloud集成hadoop HDFS,使用Feign-form实现文件上传、下载

构建基于SpringCloud分布式,使用hadoop HDFS实现文件的上传下载功能最近做springcloud项目开发,需要使用文件服务器HDFS进行文件的上传下载,所以将实现的方法步骤做个记录,分享出来,如有疑问,欢迎随时交流:项目结构如下项目使用到Eureka作为注册中心,通过Feign客户端进行服务调用,废话少说,直接上代码代码片.实现层(file_service_impl)...
摘要由CSDN通过智能技术生成

构建基于SpringCloud分布式,使用hadoop HDFS实现文件的上传下载功能

最近做springcloud项目开发,需要使用文件服务器HDFS进行文件的上传下载,所以将实现的方法步骤做个记录,分享出来,如有疑问,欢迎随时交流:

项目结构如下

在这里插入图片描述
项目使用到Eureka作为注册中心,通过Feign客户端进行服务调用,废话少说,直接上代码
代码片.

实现层(file_service_impl)

// 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);
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值