搭建一个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并不是什么新的框架技术 而是在 ...
随机推荐
- Kon-boot v2.5介绍与使用方法总结(支持win10)
Kon-boot这个工具相信大家都不陌生,这是一款专门针对Windows.Linux.MAC登陆密码破解工具,他能绕过系统所设有的登陆密码,让你的登陆畅通无阻.KON-Boot的原理是在于处理BIOS ...
- MMORPG中的相机跟随算法
先上代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cam ...
- Ext简单demo示例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Problems at works
1.ssh无法连接服务器:因部署ftp服务,误将/var目录的所有者和所属组改成了ftp,以致于SSH无法二次连接: 2.集群服务器的hadoop的datanode节点死亡,在对应节点拉起即可.若无法 ...
- nodejs批量导入数据eventproxy(回调函数嵌套解决方案)使用实例
回调函数嵌套解决方案——eventProxy API地址:https://github.com/JacksonTian/eventproxy 1.安装eventproxy 执行npm install ...
- UWP:可滚动的PivotHeader
UWP开发里,Pivot真是个令人又爱又恨的控件.为了实现某些可滚动Header的效果,有些大佬甚至去掉了原本的Header,使用一个ListView或者ListBox自己画Header,不过这样会让 ...
- 笔记:Eclipse 安装 Hibernate Tools
在线安装,要求 Eclipse Neon 4.6 版本和Java 8,在 Help > Install New Software,在打开的窗体中的 Work with 中输入URL地址" ...
- Algorithm --> 最长公共子序列(LCS)
一.什么是最长公共子序列 什么是最长公共子序列呢?举个简单的例子吧,一个数列S,若分别是两个或多个已知序列的子序列,且是所有符合条件序列中最长的,则S称为已知序列的最长公共子序列. 举例如 ...
- [poj2923]Relocation_状压dp_01背包
Relocation poj-2923 题目大意:给出n个物品,有两辆车,两辆车必须一起出动并且每辆车有单独的容量.问最少需要运输多少次才能运走所有货物. 注释:n<=10,容量,物品代价< ...
- IE11,Chrome65.0.3325.146,Firefox58的webdriver驱动下载,并用selenium驱动来实现自动化测试
各浏览器版本: python版本: selenium版本: IE11的Webdriver下载: http://dl.pconline.com.cn/download/771640-1.html ...