Spring boot项目整合Hadoop的HDFS
由于最近需要使用Spring boot整合Hadoop的HDFS,但是在整合的过程遇到了很多问题,网上也没有现成教程,都是自己摸索出来的,配置了很久都没能把项目搭建出来,希望对大家有帮助。
使用Spring boot整合HDFS主要是为了从数据库获取List,将List数据生产CSV文件,导入到HDFS进行机器学习。
本文主要讲解如何整合成功和如果将List数据变成CSV文件存进HDFS当中。
简单整理下会出现的问题:
1.使用过程使用了@Slf4j,但是使用了Hadoop自动会导入log4j,会出现日志冲突
2.整合后,会出现tomcat无法启动的问题
3.依赖经常没法下载完成(这个我不断地重复下载,就解决了)
下面我先放上Pom.xml文件,这个文件比较重要,主要解决整合也是Pom.xml文件
参考如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ratings</groupId>
<artifactId>ratings</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ratings</name>
<description>ratings</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<tomcat.version>8.0.9</tomcat.version> <dependency> <groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId> <version>${tomcat.version}</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 热部署 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
上面就是最重要的Pom.xml,也是我解决整合的关键,只要这个Pom.xml搭建出来了,问题就基本解决了。
我是用的是Spring boot和Mybatis再加上HDFS,关键的代码主要我放下HDFS的操作,供给大家参考。
生产CSV过程我是在ServiceImpl中将数据封装导入HDFS,所以你必须知道
1.如何在CSV中创建文件
2.如何将数据导入CSV文件
3.如何下载HDFS文件到本地。
所以先说说如何创建文件,以下是我的代码:
// 创建HDFS的文件夹包含创建csv文件,逻辑无误!,已经修正
public String mkdir(String filename,String filepath) throws IOException {
Configuration conf = new Configuration();
conf.set(name, url);
Path srcPath = new Path(filepath);
FileSystem fs = srcPath.getFileSystem(conf);
boolean ishere = fs.isDirectory(srcPath);
if (ishere) {
System.out.println("文件夹已经存在!");
byte[] content = "".getBytes();
String path = filepath + "/" + filename + ".csv";
Path filePath = new Path(path);
FSDataOutputStream outputStream = fs.create(filePath);
outputStream.write(content);
outputStream = fs.create(filePath);
outputStream.close();
System.out.println("CSV文件创建成功!");
return path;
} else {
boolean isok = fs.mkdirs(srcPath);
if (isok) {
System.out.println("创建文件夹成功!");
byte[] content = "".getBytes();
conf.set(name, url);
String path = filepath + "/" + filename + ".csv";
Path filePath = new Path(path);
FSDataOutputStream outputStream = fs.create(filePath);
outputStream.write(content);
outputStream = fs.create(filePath);
outputStream.close();
System.out.println("CSV文件创建成功!");
return path;
} else {
System.out.println("创建文件夹失败!");
return "500";
}
}
}
以上是创建文件的一个过程
下面是如何将数据导入CSV中(不管你CSV在HDFS还是本地的window都这么操作,亲测可行)
@Override
public String u_output(int userId, String initPath) {
// TODO Auto-generated method stub
HdfsFile hdfs = new HdfsFile();
if (baseMapper.u_output(userId) != null) {
List<Ratings> list = new ArrayList<Ratings>();
list = baseMapper.u_output(userId);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Ratings ratings = (Ratings) iterator.next();
ratings.setUserId(userId);
}
if (list.size() > 0) {
try {
DateUntil date = new DateUntil();
String filename = date.getDate() + userId;
System.out.println("文件名字:" + filename);
String filepath = hdfs.mkdir(filename, initPath);
System.out.println("文件地址:" + filepath);
CsvWriter csvWriter = null;
if (filepath != "500" && filepath != "") {
try {
csvWriter = new CsvWriter(filepath, ',', Charset.forName("UTF-8"));
String[] csvHeader = { "userId", "movieId" };
csvWriter.writeRecord(csvHeader);
for (int i = 0; i < list.size(); i++) {
Ratings data = list.get(i);
String uid = String.valueOf(data.getUserId());
String mid = String.valueOf(data.getMovieId());
String[] csvContent = { uid, mid };
csvWriter.writeRecord(csvContent);
}
} finally {
csvWriter.close();
System.out.println("--------CSV文件已经写入--------");
String path = initPath + "/." + filename + ".csv.crc";
System.out.println("crc的文件路径:" + path);
File fn = new File(path);
if (fn.exists()) {
fn.delete();
System.out.println("crc文件被删除");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "200";
} else
return "500";
}
以上是接口的实现方法,具体需要怎么改参数也就是userId和initPath是你们自己的需求。(可看Pom.xml导入了一个JavaCSV的依赖,就是这个可以帮我们快速地写入CSV文件!)
最后的一个是下载CSV文件
代码奉上:
// src是hdfs的,dstpath是本地
public void downloadFile(String dstPath, String srcPath) throws IOException {
Path path = new Path(srcPath);
Configuration conf = new Configuration();
FileSystem hdfs;
conf.set(name, url);
hdfs = path.getFileSystem(conf);
File rootfile = new File(dstPath);
if (!rootfile.exists()) {
rootfile.mkdirs();
}
try {
if (hdfs.isFile(path)) {
String fileName = path.getName();
if (fileName.toLowerCase().endsWith("csv")) {
FSDataInputStream in = null;
FileOutputStream out = null;
try {
in = hdfs.open(path);
File srcfile = new File(rootfile, path.getName());
if (!srcfile.exists())
srcfile.createNewFile();
out = new FileOutputStream(srcfile);
IOUtils.copyBytes(in, out, 4096, false);
System.out.println("下载成功!");
} finally {
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
} else if (hdfs.isDirectory(path)) {
File dstDir = new File(dstPath);
if (!dstDir.exists()) {
dstDir.mkdirs();
}
// 在本地目录上加一层子目录
String filePath = path.toString();// 目录
String subPath[] = filePath.split("/");
String newdstPath = dstPath + subPath[subPath.length - 1] + "/";
System.out.println("newdstPath=======" + newdstPath);
if (hdfs.exists(path) && hdfs.isDirectory(path)) {
FileStatus[] srcFileStatus = hdfs.listStatus(path);
if (srcFileStatus != null) {
for (FileStatus status : hdfs.listStatus(path)) {
// 下载子目录下文件
downloadFile(newdstPath, status.getPath().toString());
}
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Spring boot项目整合Hadoop的HDFS相关推荐
- Spring Boot项目整合Retrofit最佳实践,最优雅的HTTP客户端工具!
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达今日推荐:2020年7月程序员工资统计,平均14357元,又跌了,扎心个人原创100W+访问量博客:点击前往,查看更多 转自 ...
- Spring Boot项目 整合Swagger2
LoveEmperor-王子様 本人项目demo(spring boot多模块):https://github.com/LoveEmperor/iproject 转载:https://blog.csd ...
- Spring Security是什么,以及如何在Spring Boot项目中整合Spring Security并且使用它,下面我们通过一个登录案例简单介绍一下Spring Security。
1.什么是Spring Security? 在了解Spring Security之前,我们是不是应该先思考一个问题,我们自己写的web案例一般都需要先登录,之后登录之后才能访问其他页面,或者说我们不同 ...
- 关于Spring Boot WebSocket整合以及nginx配置详解
这篇文章主要给大家介绍了关于Spring Boot WebSocket整合以及nginx配置的相关资料,文中通过示例代码给大家介绍的非常详细,相信对大家的学习或者工作具有一定的参考学习价值,需要的朋友 ...
- Spring Boot 应用系列 5 -- Spring Boot 2 整合logback
上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对 ...
- Spring Boot项目(Maven\Gradle)三种启动方式及后台运行详解
Spring Boot项目三种启动方式及后台运行详解 1 Spring Boot项目三种启动方法 运行Application.java类中的Main方法 项目管理工具启动 Maven项目:mvn sp ...
- spring boot 项目打成war包部署到服务器
这是spring boot学习的第二篇了,在上一篇已经整合了spring boot项目了,如果还有小伙伴没有看得可以先去看第一篇 基础整合spring boot项目 到这里的小伙伴应该都是会整合基本的 ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- Spring boot Mybatis 整合(完整版)
Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...
最新文章
- 如何访问自定义键值的二维数组
- 如何从零开始学python_从零开始学Python【4】--numpy
- NOIP模拟测试16「Drink·blue·weed」
- 正则规则大全 JAVA
- ibatis的ibatorForEclipse的安装与配置和ibator的错误日志查看
- Python的Turtle绘制纳兹咩的娘口三三
- 高通工具QXDM、QCAT和QPST关系及功能
- 微信撤回软件安卓版_微信无限时间撤回软件
- Linux 硬盘挂载
- aide制作软件教程_AIDE开发教程合集
- 什么是单片机中的掉电复位(BOR)?如何防止错误掉电
- 集五福招数都在这了,2021 支付宝集五福全攻略
- [足式机器人]Part3机构运动微分几何学分析与综合Ch01-3 平面运动微分几何学——【读书笔记】
- 红石外汇|每日汇评:澳元空头等待突破上升趋势线- 100日SMA线下方
- 叶胜超:IOST ----基于分片技术的第四大公链项目!
- Vue 仿淘宝购物车
- 果蝇优化算法(FOA)原理
- 51单片机8通道自动温度检测系统仿真+ Proteus仿真
- c语言运行后电脑很卡,如何让电脑提速,电脑卡是什么原因?
- 安装windows Admin Center
热门文章
- python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...
- 介绍一些平时用得到的服务/组件
- 华为ensp小实验(路由下发+Easy IP+单臂路由+OSPF+Rip)
- 自动控制matlab实验,自动控制matlab实验.doc
- python字符串逆序输出代码_一行代码实现字符串逆序输出
- python自然语言处理评论_python自然语言处理——学习笔记:Chapter3纠错
- ldap基本dn_LDAP学习笔记 - 基础
- 两个gcc_KDD 2020 | GCC:图上的Contrastive Coding
- php清空html_php怎么清除html代码
- java改写模式_Java基于状态模式实现的文档编辑模式切换功能实例