基于springboot,hdfs的网盘系统(查看,上传,下载,删除,新建文件)

2023-11-05

基于maven的springboot项目。项目目录如下

在这里插入图片描述
类路径如下:
在这里插入图片描述

HDFSREQUEST

package cn.object.demo02.controller;

import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
//@AllArgsConstructor
//@NoArgsConstructor
@Component
public class HDFSREQUEST {
    private String source;
    private String destination;
    private String content;

}

mainController

package cn.object.demo02.controller;

import cn.object.demo02.service.HDFS_SERVICE;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

//Restful风格
@RestController
@RequestMapping(value = "/hdfs"/*,method = RequestMethod.GET*/)
public class mainController {

    @GetMapping
    public String get(){
        System.out.println("Springboot is running");

        return "Springboot and hdfs is running";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public String delete(@RequestBody HDFSREQUEST hdfsrequest) throws Exception{
        System.out.println(hdfsrequest.getDestination());
        boolean isOk = false;
        isOk = HDFS_SERVICE.deleteFile(hdfsrequest);
        if(isOk)
            return "done";
        else
            return "failure";
    }

    @RequestMapping(value = "/download",method = RequestMethod.PUT)
    public String download(@RequestBody HDFSREQUEST hdfsrequest) throws Exception{
        System.out.println(hdfsrequest.getSource()+"\n"+hdfsrequest.getDestination());

        HDFS_SERVICE.copyToLocalFile(hdfsrequest);

        return "done";
    }


    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(@RequestBody HDFSREQUEST hdfsrequest) throws Exception {
        System.out.println(hdfsrequest.getSource()+"\n"+hdfsrequest.getDestination());

        HDFS_SERVICE.copyFromLocalFile(hdfsrequest);

        return "done";
    }

    @RequestMapping(value = "/mkdir",method = RequestMethod.POST)
    public String mkdir(@RequestBody HDFSREQUEST hdfsrequest) throws Exception {
        System.out.println(hdfsrequest.getDestination());

        HDFS_SERVICE.createFile(hdfsrequest);

        return "done";

    }


    @RequestMapping(value = "/getRootDirectory",method = RequestMethod.GET)
    public List<String> getDirectory() throws IOException {
        return HDFS_SERVICE.showRoot();

    }

    @RequestMapping(value = "/{str}",method = RequestMethod.DELETE)
    public String delete(@PathVariable String str){
        System.out.println(str);
        return str;
    }

}

HDFS_SERVICE

package cn.object.demo02.service;

import cn.object.demo02.controller.HDFSREQUEST;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

import static cn.object.demo02.HDFS_INIT.hdfs;
import static cn.object.demo02.HDFS_INIT.hdfsDirectories;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Service

public class HDFS_SERVICE {

    private String source;
    private String destination;

    HDFS_SERVICE(HDFSREQUEST hdfsrequest){
        source = hdfsrequest.getSource();
        destination = hdfsrequest.getDestination();
    }

    public static void createFile(HDFSREQUEST hdfsrequest) throws Exception {

        FSDataOutputStream outputStream = hdfs.create(new Path(hdfsrequest.getDestination())); // Files are overwritten by default
        outputStream.write(hdfsrequest.getContent().getBytes()); // enter the content
        outputStream.close();
        System.out.println("mkdir is done!");
    }

    public static boolean deleteFile(HDFSREQUEST hdfsrequest) throws Exception {

        Path path = new Path(hdfsrequest.getDestination());
//        boolean isok = true;
        boolean isok = hdfs.deleteOnExit(path);
        if (isok) {
            System.out.println(hdfsrequest.getDestination()+"删除成功!");
        } else {
            System.out.println("删除失败!");
        }
//        hdfs.close();
        return isok;
    }

    public static List<String> showRoot() throws IOException {
        hdfsDirectories.clear();
        FileStatus fs = hdfs.getFileStatus(new Path("hdfs:/"));

        showDir(fs);

        return hdfsDirectories;
    }

    private static void showDir(FileStatus fileStatus) throws IOException {

        Path path = fileStatus.getPath();
        hdfsDirectories.add(path.toString());

        if(fileStatus.isDirectory()){
            FileStatus[] f = hdfs.listStatus(path);
            if(f.length > 0)
                for(FileStatus file : f)
                    showDir(file);
        }
    }

    // 复制上传本地文件
    public static void copyFromLocalFile(HDFSREQUEST hdfsrequest) throws Exception {

        Path src = new Path(hdfsrequest.getSource()); // 本地目录/文件
        Path dst = new Path(hdfsrequest.getDestination()); // 目标目录/文件
        // 4.拷贝上传本地文件(本地文件,目标路径) 至HDFS文件系统中
        hdfs.copyFromLocalFile(src, dst);
        System.out.println("upload successful!");
    }

    public static void copyToLocalFile(HDFSREQUEST hdfsrequest) throws Exception {
        Path src = new Path(hdfsrequest.getSource());
        Path dst = new Path(hdfsrequest.getDestination()); // 本地目录/文件
        // 4.从HDFS文件系统中拷贝下载文件(目标路径,本地文件)至本地
//        hdfs.copyToLocalFile(src, dst);
        hdfs.copyToLocalFile(false, src, dst, true);//delSrc – whether to delete the src
        System.out.println("文件下载成功!");
    }


}

HDFS_INIT

package cn.object.demo02;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HDFS_INIT {
    public static FileSystem hdfs;
    public static List<String> hdfsDirectories = new ArrayList<>();

    public static void init() throws IOException {
        System.out.println("-----------hdfs-starting---------");
        // 1.创建配置器
        Configuration conf = new Configuration();

        // 2.取得FileSystem文件系统 实例
        conf.set("fs.defaultFS","hdfs://centos01:9000");

        // 3.创建可供hadoop使用的文件系统路径
        hdfs = FileSystem.get(conf);
        System.out.println("------------hdfs-running--------");
    }

//    public static void

}

Demo02Application

package cn.object.demo02;

import org.apache.hadoop.fs.FileSystem;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class Demo02Application {
	private static FileSystem hdfs;

	public static void main(String[] args) throws IOException {
		HDFS_INIT.init();

		SpringApplication.run(Demo02Application.class, args);
	}

}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.object</groupId>
	<artifactId>demo02</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo02</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.10.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.10.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

postman测试:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
json数据

{
    "destination": "hdfs://centos01:9000/input/dir01/dir02/dir03/dir04_05",
    "source": "null",
    "content": "null"
}

在这里插入图片描述
json数据

{
    "destination": "hdfs://centos01:9000/input/dir01",
    "source": "/home/chi/Bigdata/dir02_03",
    "content": "null"
}

在这里插入图片描述
json数据

{
    "destination": "hdfs://centos01:9000/input/dir01/dir02/dir03/dir04_05",
    "source": "source",
    "content": "hdfs://centos01:9000/input/dir01/dir02/dir03/dir04_05"
}

在这里插入图片描述
json数据

{
    "destination": "/home/chi/Bigdata",
    "source": "hdfs://centos01:9000/input/dir01/dir02/dir03/dir04_05",
    "content": "null"
}

注:
1.
开发环境为windows编译器IDEA。
部署环境为linux,centos。
过程中还应用了xftp,xshell,postman。
需要资源可以私信我。
2.
需要注意的是,删除文件后并不会被及时删除,用get还能看到对应文件,用hadoop fs -ls -R / 的命令也会查看到被删除的文件。当服务器停止时才会真正的删除文件,服务器停止时使用命令,或者重启服务器用get命令都可以观察到文件已被删除,由此推测,删除文件可能是有一种保护机制,先将操作存储在缓冲区内,可以在退出前回溯操作等,具体请查谷歌,stackoverflow,源码或官方文档(要写别的项目了,哪有时间查这种东西hhhh)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于springboot,hdfs的网盘系统(查看,上传,下载,删除,新建文件) 的相关文章

  • 使用 sh 运行 bash 脚本

    我有 bash 脚本 它需要 bash 另一个人尝试运行它 sh script name sh 它失败了 因为 sh 是他的发行版中 dash 的符号链接 ls la bin sh lrwxrwxrwx 1 root root 4 Aug
  • 有谁知道在哪里定义硬件、版本和序列号。 /proc/cpuinfo 的字段?

    我想确保我的 proc cpuinfo 是准确的 目前它输出 Hardware am335xevm Revision 0000 Serial 0000000000000000 我可以在代码中的哪里更改它以给出实际值 这取决于 Linux 的
  • HDFS 中的文件数量与块数量

    我正在运行单节点 hadoop 环境 当我跑的时候 hadoop fsck user root mydatadir block 我真的对它给出的输出感到困惑 Status HEALTHY Total size 998562090 B Tot
  • 为arm构建WebRTC

    我想为我的带有arm926ej s处理器的小机器构建webrtc 安装 depot tools 后 我执行了以下步骤 gclient config http webrtc googlecode com svn trunk gclient s
  • 在我的 index.php 中加载 CSS 和 JS 等资源时出现错误 403

    我使用的是 Linux Elementary OS 并在 opt 中安装了 lampp My CSS and JS won t load When I inspect my page through browser The console
  • jq中如何分组?

    这是 json 文档 name bucket1 clusterName cluster1 name bucket2 clusterName cluster1 name bucket3 clusterName cluster2 name bu
  • 如何使用 GOPATH 的 Samba 服务器位置?

    我正在尝试将 GOPATH 设置为共享网络文件夹 当我进入 export GOPATH smb path to shared folder I get go GOPATH entry is relative must be absolute
  • 如何更改 Apache 服务器的根目录? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 如何更改 Apache 服务器的文档根目录 我基本上想要localhost从 来 users spencer projects目录而不是
  • “make install”将库安装在 /usr/lib 而不是 /usr/lib64

    我正在尝试在 64 位 CentOS 7 2 上构建并安装一个库 为了这个目的我正在跑步 cmake DCMAKE BUILD TYPE Release DCMAKE INSTALL PREFIX usr DCMAKE C COMPILER
  • Linux - 从第二个选项卡获取文本

    假设我们有这样的文件 一些文本11 一些文本12 一些文本13 一些文本21 一些文本22 一些文本23 文本由制表符分隔 我们知道第 1 列中的一些文本 但希望从第 2 列中获取文本 我知道我可以通过以下方式获取线路 grep somet
  • waitpid() 的作用是什么?

    有什么用waitpid 它通常用于等待特定进程完成 或者如果您使用特殊标志则更改状态 基于其进程 ID 也称为pid 它还可用于等待一组子进程中的任何一个 无论是来自特定进程组的子进程还是当前进程的任何子进程 See here http l
  • 在两次之间每分钟执行一次 Cronjob

    我需要在 crontab 中每分钟运行一个 bash 脚本8 45am and 9 50am每天的 Code 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 8 home pull sh gt ho
  • Urwid:使光标不可见

    我正在使用 urwid 它是一个用于在 ncurses 中设计终端用户界面的 Python 框架 但有一件事我在 urwid 中无法做到 而这在 Curses 中很容易做到 使光标不可见 现在 选择按钮时光标是可见的 而且看起来很丑 有办法
  • NUMA 在虚拟内存中是如何表示的?

    有许多资源 https en wikipedia org wiki Non uniform memory access从硬件角度描述NUMA的架构性能影响 http practical tech com infrastructure num
  • 如何查询X11显示分辨率?

    这似乎是一个简单的问题 但我找不到答案 如何查询 通过 X11 存在哪些监视器及其分辨率 查看显示宏 http tronche com gui x xlib display display macros html and 屏幕宏 http
  • 监控子进程的内存使用情况

    我有一个 Linux 守护进程 它分叉几个子进程并监视它们是否崩溃 根据需要重新启动 如果父进程可以监视子进程的内存使用情况 以检测内存泄漏并在超出一定大小时重新启动子进程 那就太好了 我怎样才能做到这一点 您应该能够从 proc PID
  • 为什么C Clock()返回0

    我有这样的事情 clock t start end start clock something else end clock printf nClock cycles are d d n start end 我总是得到输出 时钟周期是 0
  • Capistrano 3 部署无法连接到 GitHub - 权限被拒绝(公钥)

    我使用 Capistrano v3 和 capistrano symfony gem 设置了以下部署脚本 我正在使用 Ubuntu 14 4 部署到 AWS EC2 实例 我正在连接从 AWS 下载的 pem 文件 我的deploy rb中
  • Python 脚本作为 Linux 服务/守护进程

    Hallo 我试图让 python 脚本作为服务 守护进程 在 ubuntu linux 上运行 网络上存在多种解决方案 例如 http pypi python org pypi python daemon http pypi python
  • 当用户按下打印时运行脚本,并且在脚本结束之前不开始假脱机(linux,cups)

    我需要做的是结合用户按下打印来执行 python 程序 脚本 并且在该程序退出之前不要让打印作业假脱机 原因是打印驱动程序不是开源的 我需要更改用户设置 在本例中是部门 ID 和密码 通常是每个用户 但因为这是一个信息亭 具有相同帐户的不同

随机推荐

  • Java Web 里Servlet的介绍与理解

    文章目录 目录 文章目录 前言 一 Servlet是什么 Servlet介绍 Servlet的工作流程可以用下面的图来表示 二 使用Servlet的步骤 1 创建一个Maven项目 然后在pom xml中导入所需的Jar包 2 编写继承了S
  • 练习题_进程

    1 一个正在运行的进程 当发生某一事件 将其挂在 A A 等待队列 B 运行队列 C 就绪队列 D 任意一个队列 解析 只有在分时系统时间片完时 进程由运行转为就绪状态 一 般来说 有事件发生时 进程会被挂在等待队列 2 下列选项中 导致创
  • MySQL用户管理和权限管理

    MySQL用户管理和权限管理 在项目中 一个数据库有很多人需要使用 不能所有的人都使用相同的权限 如果人比较多 一人一个用户也很难管理 一般来说 会分超级管理员权限 管理员权限 读写权限 只读权限等 这样方便管理 当然 具体怎么管理权限根据
  • SaaS架构实现理论(四)可伸缩多租户

    目录 1 伸缩性 Scalable 的概念 2 应用服务器层的水平扩展 2 1基于Session复制的水平扩展方式 2 2基于Session Sticky的水平扩展方式 2 3基于Cache的集中式Session实现水平扩展 2 4三种水平
  • ubuntu18.04安装RTX2080ti显卡驱动+cuda10.2+cudnn

    因为系统环境变量崩溃 进行重做了系统 全部还原 在本机重新安装了显卡驱动 cuda等 具体系统版本如下 系统环境 ubuntu18 04 显卡 rtx2080ti cuda版本 10 2 安装RTX2080ti显卡驱动 1 ubuntu 1
  • Go语言中字典树的实现

    写在前面 字典树在存储 查询方面应用广泛 所以特总结一下 利用GO语言实现字典树 具体实现 字典树的实现主要还是基于树形结构 如果只是小写字母的话 那其实字典树是一个26叉树 每个节点最多都可以有26个子节点 从而可以利用一个长度为26的数
  • 查看docker-compose --version报错syntax error near unexpected token `(‘‘usr/local/bin/docker-compose:

    问题 执行docker compose version查看版本是报错如下 usr local bin docker compose line 2 html No such file or directory usr local bin do
  • 【CV with Pytorch】第 6 章 :姿态估计

    人体姿势估计 HPE 是一项计算机视觉任务 它通过估计给定帧 视频中的主要关键点 例如眼睛 耳朵 手和腿 来检测人体姿势 图6 1显示了人体姿态估计的一个例子 图 6 1 HPE示例 人体姿势检测有助于跟踪人体部位和关节 在人体中识别的一些
  • Java:使用Iterator迭代器遍历集合数据

    1 使用迭代器遍历ArrayList集合 package com jredu oopch07 import java util ArrayList import java util Iterator import java util Lis
  • 在Spring 中元素的作用

    一 介绍 spring的配置文件中常包含如下元素
  • Liunx创建新用户登录异常:/usr/bin/xauth: error/timeout in locking authority file /home/liuqidong/.Xauthority

    Liunx创建新用户登录异常 usr bin xauth error timeout in locking authority file home liuqidong Xauthority 问题1 在服务器上创建新的用户userA 在使用s
  • c++ 中vector的count是unsigned int而C#中泛型的count为int

    需要注意的 在使用一个返回值之前 要知道这个返回值是什么类型的 不要根据自己的臆断来写 否则c 很多bug不知道原因 十分注意 对比的时候 是自动转成unsigned int类型进行比较的 Orz C 中的泛型 用count都是int类型
  • 连通图的桥(对桥和割点的理解)

    题目链接 https cn vjudge net problem UVA 796 顺便总结一下 对于连通图的桥和割点 首先 从tarjan的角度来说 dfn数组代表的是当前节点的编号 也就是时间戳 low数组代表的是当前节点能够到达的最早的
  • 利用SqlServer触发器自动更新表updatetime字段值

    本文主要记录了使用SqlServer数据库触发器自动更新表的 更新时间updatetime 字段 在 MySQL数据库中 某行数据创建时间字段 createtime 行最新更新时间字段updatetime 可建表时分别用 datetime
  • 【k8s集群管理工具篇】安装kubernetes集群管理工具 - Kuboard v3版本

    k8s集群管理工具篇 安装kubernetes集群管理工具 Kuboard v3版本 一 kuboard介绍 1 kuboard解释 2 kuboard的V3版本 二 安装kuboard 1 下载yaml文件 2 安装kuboard 三 查
  • 使用PIL实现图像的二值化和灰度化DIY

    使用PIL实现图像的二值化和灰度化并输出保存 精简版代码如下 图片二值化 from PIL import Image img Image open r W PY newpicpic bbvvasd jpg 模式L 为灰色图像 它的每个像素用
  • 编译KArchive在windows10下

    使用QT6和VS2019编译KArchive的简要步骤 安装 Qt 我是用源码自己编译的 F qtbuild 安装CMakefile并配置环境变量 安装Git 下载ECM源码 https github com KDE extra cmake
  • linux安装mysql的两种方式

    一 安装到linux 1 安装mysql server 1 在安装之前查看下系统是否已经安装了mysql ls usr share 2 安装mysql server sudo apt get install mysql server 3 再
  • Linux查看并杀死进程

    Linux查看并杀死进程 查看进程 方法一 lsof i 80 i lt 条件 gt 列出符合条件的进程 协议 端口 ip 方法二 netstat anp grep 进程ID a或 all 显示所有连线中的Socket n或 numeric
  • 基于springboot,hdfs的网盘系统(查看,上传,下载,删除,新建文件)

    基于maven的springboot项目 项目目录如下 类路径如下 HDFSREQUEST package cn object demo02 controller import lombok Data import lombok AllAr