前言

近期在公司接到一个任务。是关于数据採集方面的。

需求主要有3个:

  • 通过web端上传文件到HDFS;
  • 通过日志採集的方式导入到HDFS;
  • 将数据库DB的表数据导入到HDFS。

正好近期都有在这方面做知识储备。正所谓养兵千日,用兵一时啊。

学习到的东西仅仅有应用到真实的环境中才有意义不是么。

环境

这里仅仅做模拟环境。而不是真实的线上环境,所以也非常easy。假设要使用的话还须要优化优化。

说明一下,这个系统OS最好使用Linux的。然后Hadoop也推荐使用CDH发行版的,由于在兼容性、安全性、稳定性都要好于开源的版本号。

比方说CDH的易于升级维护,已解决好Hadoop生态其它产品的版本号兼容问题,补丁更新比开源要及时(毕竟商业公司支持)等等

还有之所以使用SpringBoot是由于快捷,方便,不用做一大堆的配置,无论是作为演示还是生产开发都挺好的。

项目搭建

这里仅仅是做一个非常easy的演示,就是在Web页面提供一个上传button,使用户能够将本地文件上传至Hadoop集群平台。

pom.xml

首先看下pom文件的依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.infosys.hadoop</groupId>
  7. <artifactId>upload</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>upload</name>
  10. <packaging>jar</packaging>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.1.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <hadoop.version>2.6.5</hadoop.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>javax.servlet</groupId>
  29. <artifactId>javax.servlet-api</artifactId>
  30. <version>3.1.0</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.hadoop</groupId>
  34. <artifactId>hadoop-client</artifactId>
  35. <version>${hadoop.version}</version>
  36. <exclusions>
  37. <exclusion>
  38. <groupId>org.slf4j</groupId>
  39. <artifactId>slf4j-log4j12</artifactId>
  40. </exclusion>
  41. </exclusions>
  42. </dependency>
  43. <!-- Test -->
  44. <dependency>
  45. <groupId>junit</groupId>
  46. <artifactId>junit</artifactId>
  47. <version>4.12</version>
  48. <scope>test</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.apache.mrunit</groupId>
  52. <artifactId>mrunit</artifactId>
  53. <version>1.1.0</version>
  54. <classifier>hadoop2</classifier>
  55. <scope>test</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.apache.hadoop</groupId>
  59. <artifactId>hadoop-minicluster</artifactId>
  60. <version>${hadoop.version}</version>
  61. <scope>test</scope>
  62. </dependency>
  63. </dependencies>
  64. <build>
  65. <finalName>${project.artifactId}</finalName>
  66. <plugins>
  67. <plugin>
  68. <groupId>org.apache.maven.plugins</groupId>
  69. <artifactId>maven-archetype-plugin</artifactId>
  70. <version>2.2</version>
  71. </plugin>
  72. <plugin>
  73. <groupId>org.apache.maven.plugins</groupId>
  74. <artifactId>maven-resources-plugin</artifactId>
  75. <configuration>
  76. <encoding>UTF-8</encoding>
  77. </configuration>
  78. </plugin>
  79. <plugin>
  80. <groupId>org.apache.maven.plugins</groupId>
  81. <artifactId>maven-compiler-plugin</artifactId>
  82. <version>3.1</version>
  83. <configuration>
  84. <source>1.8</source>
  85. <target>1.8</target>
  86. </configuration>
  87. </plugin>
  88. <plugin>
  89. <groupId>org.apache.maven.plugins</groupId>
  90. <artifactId>maven-jar-plugin</artifactId>
  91. <version>2.5</version>
  92. <configuration>
  93. <outputDirectory>${basedir}</outputDirectory>
  94. </configuration>
  95. </plugin>
  96. <plugin>
  97. <groupId>org.springframework.boot</groupId>
  98. <artifactId>spring-boot-maven-plugin</artifactId>
  99. </plugin>
  100. </plugins>
  101. </build>
  102. </project>

我们就是加入了一个SpringBootHadoop Client的依赖。其它的是一些測试相关的。

关于这个Hadoop Client它提供了一些开发Hadoop应用所需的全部依赖,能够參考之前的一篇博客:Hadoop 2.x Maven开发环境搭建

首页

首页界面就仅仅是提供一个上传表单button:

index.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Upload</title>
  9. </head>
  10. <body>
  11. <form action="/upload" method="post" enctype="multipart/form-data">
  12. <p>
  13. 文件:<input type="file" name="file">
  14. </p>
  15. <p>
  16. <input type="submit" value="上传">
  17. </p>
  18. </form>
  19. </body>
  20. </html>

然后在Controller提供一个接口进行訪问首页:

HomeController.java

  1. @Controller
  2. @RequestMapping(value = "/")
  3. public class HomeController {
  4. public ModelAndView home() {
  5. return new ModelAndView("index");
  6. }
  7. }

上传

上传的逻辑也非常easy,就是使用SpringBoot上传文件的形式先将文件接收到后台。然后调用Hadoop提供的接口API运行上传。

上传接口UploadController.java

  1. @Controller
  2. public class UploadController {
  3. @PostMapping("/upload")
  4. @ResponseBody
  5. public String handleFileUpload(@RequestParam("file") MultipartFile file) {
  6. if (!file.isEmpty()) {
  7. try {
  8. String originalFilename = file.getOriginalFilename();
  9. BufferedOutputStream out = new BufferedOutputStream(
  10. new FileOutputStream(
  11. new File(originalFilename)
  12. )
  13. );
  14. out.write(file.getBytes());
  15. out.flush();
  16. out.close();
  17. String destFileName = "/user/hadoop/" + originalFilename;
  18. Upload.main(new String[]{originalFilename, destFileName});
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. return "上传失败," + e.getMessage();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. return "上传失败, " + e.getMessage();
  25. }
  26. return "上传成功";
  27. } else {
  28. return "上传失败。文件为空。
  29. ";
  30. }
  31. }
  32. }

最后我们在提供一个类来操作Hadoop接口。

Upload.java

  1. public class Upload {
  2. public static final String FS_DEFAULT_FS = "fs.defaultFS";
  3. public static final String HDFS_HOST = "hdfs://192.168.1.2:9000";
  4. public static final String CROSS_PLATFORM = "mapreduce.app-submission.cross-platform";
  5. public static void main(String[] args) throws IOException {
  6. Configuration conf = new Configuration();
  7. conf.setBoolean(CROSS_PLATFORM, true);
  8. conf.set(FS_DEFAULT_FS, HDFS_HOST);
  9. GenericOptionsParser optionsParser = new GenericOptionsParser(conf, args);
  10. String[] remainingArgs = optionsParser.getRemainingArgs();
  11. if (remainingArgs.length < 2) {
  12. System.err.println("Usage: upload <source> <dest>");
  13. System.exit(2);
  14. }
  15. Path source = new Path(args[0]);
  16. Path dest = new Path(args[1]);
  17. FileSystem fs = FileSystem.get(conf);
  18. fs.copyFromLocalFile(true, false, source, dest);
  19. }
  20. }

当中的fs.defaultFS属性须要与集群Master NameNode节点中配置的一直。该属性配置一般在etc/hadoop/core-site.xml文件里进行定义。

能够看到我们实际的操作非常easy,就仅仅是调用Hadoop的FileSystem接口中的copyFromLocalFile方法。该方法參数说明:

  • 第一个參数:表示是否删除本地的源文件。也就是上传文件后是否保留原文件。这里为了避免兴许文件越来越多,就直接採用上传成功就删除的方式。
  • 第二个參数:表示是否覆盖已存在的文件,这里false表示不覆盖,假设HDFS集群中已存在该文件,就提示上传失败。
  • 第三个參数:源文件路径
  • 第四个參数:上传到HDFS指定的路径

后记

当然上传的方式肯定不止这一种,比方:通过Hadoop的rest接口调用PUT也能够上传,还有Python等其它语言也有对应的API接口等等

假设是要做成平台的话,这样肯定是远远不够的,每一个用户都能够上传就须要做好隔离措施,我们能够採用HDFS文件夹隔离的方式,只是我认为这样不够好,最好採用CDH支持的kerberos进行授权认证的方式比較好。

开源的Hadoop默认仅仅支持Simple的形式,也就是与操作系统一致的用户验证。

数据採集之Web端上传文件到Hadoop HDFS的更多相关文章

  1. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  2. 演示如何通过 web api 上传文件MVC40

    演示如何通过 web api 上传文件WebApiWebFormHost/UploadFileController.cs /* * 通过 web api 上传文件 */ using System; u ...

  3. 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

    原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...

  4. 编写Java程序,实现客户端向服务端上传文件的功能

    查看本章节 查看作业目录 需求说明: 实现客户端向服务端上传文件的功能 当启动服务端后,运行客户端程序,系统提示客户在客户端输入上传文件的完整路径.当客户在客户端输入完成后,服务端实现文件上传 实现思 ...

  5. 大数据学习——点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上

    点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上 1需求说明 点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上 2需求分 ...

  6. 前端AngularJS后端ASP.NET Web API上传文件

    本篇体验使用AngularJS向后端ASP.NET API控制器上传文件.    首先服务端: public class FilesController : ApiController { //usi ...

  7. web前端:上传文件夹(需支持多浏览器)

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  8. angulaijs中的ng-upload-file与阿里云oss服务的结合,实现在浏览器端上传文件到阿里云(速度可以达到1.5M)

    2015-10-26 angularjs结合aliyun浏览器端oos文件上传加临时身份验证例子 在服务端获取sts 源码: public class StsServiceSample { // 目前 ...

  9. C# Web Api 上传文件

    一. 使用默认方法上传文件: 1.Action: /// <summary> /// 上传文件 使用上传后的默认文件名称 /// 默认名称是BodyPart_XXXXXX,BodyPart ...

随机推荐

  1. Python endswith() 函数

    函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一.函数说明语法:string.endswith(st ...

  2. 浅谈Mysql 表设计规范(转)

    本文首先探讨下数据库设计的三大范式,因为范式只是给出了数据库设计的原则,并没有告诉我们实际操作中应该怎样操作,应该注意什么,所以我们还会谈下实际工作中需要注意的具体操作问题. 三大范式 首先放出三大范 ...

  3. MySql计算两个日期的时间差函数

    MySql计算两个日期时间的差函数: 第一种:TIMESTAMPDIFF函数,需要传入三个参数,第一个是比较的类型,可以比较FRAC_SECOND.SECOND. MINUTE. HOUR. DAY. ...

  4. Android开源之BaseRecyclerViewAdapterHelper(持续更新!)

    官方地址:http://www.recyclerview.org/ 文档 v1.9.8 English 中文 v2.0.0 English 中文 Extension library PinnedSec ...

  5. [svc]centos6使用chkconfig治理服务和其原理

    centos6开机启动级别 $ cat /etc/inittab ... # 0 - halt (Do NOT set initdefault to this) # 1 - Single user m ...

  6. sql的split()函数

    ALTER function [dbo].[StrToList_Test](@Str varchar()) returns @table table( value nvarchar(max) ) as ...

  7. Fluent UDF【5】:第一个UDF

    这里以一个简单的初始化案例来描述UDF的使用过程. 0 Fluent中的Patch Fluent中提供了全域初始化以及局部Patch功能.对于整体区域的全局初始化可以采用starndard及hybri ...

  8. adb -s

    当已经有多个设备连接到主机时,可以使用-s参数进行选择. 设备为adb devices列出的内容. 除此之外,-d表示只通过USB连接,-e表示只连接仿真器.

  9. [Windows Azure] Walkthrough to Configure System Center Management Pack for Windows Azure Fabric Preview for SCOM 2012 SP1 (with a MetricsHub Bonus)

    The wait is finally over. This is a huge update to the Azure Management Pack over the one that was r ...

  10. Android 编程下将 Bitmap 转为 InputStream

    某些情况下会用到这种非主流的转换方式,最近项目中用到,记录下. ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compres ...