搭建一个web服务下载HDFS的文件
需求描述
为了能方便快速的获取HDFS中的文件,简单的搭建一个web服务提供下载很方便快速,而且在web服务器端不留临时文件,只做stream中转,效率相当高!
使用的框架是SpringMVC+HDFS API
关键代码
@Controller
@RequestMapping("/file")
public class FileDownloadController {
private static final String BASE_DIR = "/user/app/dump/";
@RequestMapping(value = "/download/{filename}", method = RequestMethod.GET)
@ResponseBody
public void fileDownload(@PathVariable("filename") String fileName, HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/octet-stream; charset=utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName + ".csv", "UTF-8"));
String path = BASE_DIR + fileName;
HdfsUtils.copyFileAsStream(path, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 加载要下载的文件都在 /user/app/dump/这个目录下
- 下载路径 http://ip:port/file/download/xxxfile
HdfsUtils.copyFileAsStream 实现
public class HdfsUtils {
private static FileSystem hdfs = null;
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
Configuration conf=new Configuration();
try {
hdfs = FileSystem.get(URI.create("hdfs://xxxxxxx"), conf, "app");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFileAsStream(String fpath, OutputStream out) throws IOException, InterruptedException {
org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(fpath);
FSDataInputStream fsInput = hdfs.open(path);
IOUtils.copyBytes(fsInput, out, 4096, false);
fsInput.close();
out.flush();
}
}
是不是非常简单? HDFS的文件流没落在web服务上,而是直接copy到了浏览器的OutputStream上
更进一步提升性能,压缩
修改 web端的代码, 用zip进行压缩,默认的压缩比例是1:5,大大减少了流在网络上传输量
@Controller
@RequestMapping("/file")
public class FileDownloadController {
private static final String BASE_DIR = "/user/app/dump/";
@RequestMapping(value = "/download/zip/{filename}", method = RequestMethod.GET)
@ResponseBody
public void hdfsDownload2(@PathVariable("filename") String fileName, HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName + ".zip", "UTF-8"));
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipOut.putNextEntry(new ZipEntry(fileName + ".csv"));
} catch (Exception e) {
e.printStackTrace();
}
String path = BASE_DIR + fileName;
HdfsUtils.copyFileAsStream(path, zipOut);
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
一些用的主要jar版本
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<hadoop.version>2.7.0</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
搭建一个web服务下载HDFS的文件的更多相关文章
- 使用nodejs和express搭建http web服务
目录 简介 使用nodejs搭建HTTP web服务 请求nodejs服务 第三方lib请求post 获取http请求的正文 Express和使用express搭建http web服务 express ...
- 搭建一个Web Server站点
题:搭建一个Web Server站点.安装web服务,并在本地创建index.html测试 1.安装http服务 yum -y install httpd 2.进入网站目录 cd /var/www/h ...
- 利用OpenStreetMap(OSM)数据搭建一个地图服务
http://www.cnblogs.com/LBSer/p/4451471.html 图 利用OSM数据简单发布的北京地图服务 一.OSM是什么 开放街道图(OpenStreetMap,简称O ...
- 通过express快速搭建一个node服务
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台.可以理解为是运行在服务端的 JavaScript.如果你是一个前端程序员,不太擅长像PHP.Python或Ruby等 ...
- 【LINUX】——linux如何使用Python创建一个web服务
问:linux如何使用Python创建一个web服务? 答:一句话,Python! 一句代码: /usr/local/bin/python -m SimpleHTTPServer 8686 > ...
- wsgiref手写一个web服务端
''' 通过wsgiref写一个web服务端先讲讲wsgiref吧,基于网络通信其根本就是基于socket,所以wsgiref同样也是通过对socket进行封装,避免写过多的代码,将一系列的操作封装成 ...
- 在Myeclipse中拷贝一个web项目,但是tomcat文件夹中没有更新,需要进行修改才能更新。
1.在Myeclipse中拷贝一个web项目,但是tocat文件夹中没有更新,需要进行修改才能更新. 2.方法:右键这个工程,然后Properties->MyEclipse->Projec ...
- 如何搭建一个WEB服务器项目(二)—— 对数据库表进行基本的增删改查操作
使用HibernateTemplate进行增删改查操作 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出 ...
- Spring Boot(一):如何使用Spring Boot搭建一个Web应用
Spring Boot Spring Boot 是Spring团队旗下的一款Web 应用框架 其优势可以更快速的搭建一个Web应用 从根本上上来讲 Spring Boot并不是什么新的框架技术 而是在 ...
随机推荐
- 毫秒级检测!你见过带GPU的树莓派吗?
树莓派3B+英特尔神经计算棒进行高速目标检测 转载请注明作者梦里茶 代码: 训练数据预处理: https://gist.github.com/ahangchen/ae1b7562c1f93fdad1d ...
- python爬微信公众号前10篇历史文章(6)-话说http cookies
早期Web开发面临的最大问题之一是如何管理状态.简言之,服务器端没有办法知道两个请求是否来自于同一个浏览器.这是cookies的起源. 什么是cookie? A cookie is a small s ...
- freemarker的classic_compatible设置,解决报空错误
前段时间接触freemaker时,本来后端写各接口运行正常,但加入了模板后,频繁报空指针问题,整了许久,最后还是请教了别人解决了这个问题,现在记录下来,方便以后碰到了可以查阅. 错误样例如下: ERR ...
- 戴尔R720xd服务器系统安装
型号:R720xd 开启服务器,Ctrl+R进入raid配置 配置完raid后F2对硬盘进行格式化 保存并重启 F11进入BIOS选项设置U盘启动 选择U盘启动 开始进行系统安装!
- WinSock 异步I/O模型
如果你想在Windows平台上构建服务器应用,那么I/O模型是你必须考虑的. Windows操作系统提供了五种I/O模型,分别是选择(select)模型,异步选择(WSAAsyncSelect)模型, ...
- MySQL基本语句与经典习题
[SQL语句大全] 本文用到的数据(5张表): customers: orders: orderitems: Products: Vendors: 一.检索数据-select语句select pro ...
- Dockerfile 指令 VOLUME 介绍
在介绍VOLUME指令之前,我们来看下如下场景需求: 1)容器是基于镜像创建的,最后的容器文件系统包括镜像的只读层+可写层,容器中的进程操作的数据持久化都是保存在容器的可写层上.一旦容器删除后,这些数 ...
- 在Node应用中避免“Dot Hell”
转载自:http://blog.leapoahead.com/2015/09/03/prevent-node-require-dot-hell/ 在Node应用中,我们使用require来加载模块.在 ...
- Algorithm --> 爬楼梯求最大分数
爬楼梯求最大分数 如下图,最大分数是: 10+20+25+20=75. 要求: 1.每次只能走一步或者两步: 2.不能连续三步走一样的,即最多连续走两次一步,或者连续走两次两步: 3.必 ...
- Java集合:TreeMap源码剖析
一.概念 TreeMap是基于红黑树结构实现的一种Map,要分析TreeMap的实现首先就要对红黑树有所了解. 要了解什么是红黑树,就要了解它的存在主要是为了解决什么问题,对比其他数据结构比如数组,链 ...