搭建一个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并不是什么新的框架技术 而是在 ...
随机推荐
- eclipse 中启动Tomcat超时了错误
修改E:\eclipse\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml 将 start-ti ...
- CSS个别属性
*{ scrollbar-3dlight-color:#fff; // 3d亮色阴影边框(threedlightshadow)的外观颜色 scrollbar-highlight-color:#fff; ...
- 基于etcd的Rabbitmq队列订阅负载均衡
go-qb Load balancer for rabbitmq queue subscribing Feature Rabbitmq queue subscription load balancin ...
- 直播-rtmp学习
RTMP(实时消息传输协议),官方介绍如下: Adobe’s Real Time Messaging Protocol (RTMP), an application-level protocol de ...
- ZFS建池建卷和格式化
建池 zpool create pool_name path -f (例如path=/dev/sdb) zfs set primarycache=metadata pool_name (关闭数据缓存 ...
- jar包和war包的介绍与区别
在学习maven的过程中接触到了jar包和war包.之前在写小项目的时候真的遇到过war包,当时为了找到jar包,把war包 的后缀名改成了.rar的压缩文件,在里面提取出来jar包来用.其实jar包 ...
- eclipse打包
- vue+webpack+element-ui+git
webpack.config.jsconst { resolve } = require('path') const webpack = require('webpack') const HtmlWe ...
- 学习ASP.NET Core Razor 编程系列三——创建数据表及创建项目基本页面
一.创建脚本工具并执行初始迁移 在本节中,您将使用包管理控制台(PMC)来更新数据库: •添加VisualStudio Web代码生成包.这个包是运行脚本引擎所必需的. • 执行Add-Migrati ...
- Spring Boot应用的后台运行配置
酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...