基于Hadoop分布式存储的网盘系统实现(简易粗糙版)(上)

简介: 基于Hadoop分布式存储的网盘系统实现(简易粗糙版)(上)

0x00 教程内容


  1. 准备工作
  2. 编写代码
  3. 运行效果
  4. 实现说明

说明:本实现非常地粗糙,仅供参考。通过本教程,可以看到运行结果,并且实现跟 HDFS 交互的最基本的功能。


0x01 基于Hadoop分布式存储的网盘系统实现


1. 准备工作

(1)需要自行安装好 Hadoop

(2)启动好 Hadoop

2. 编写代码

注意:这里必须要引入Hadoop的依赖,可以参考教程:Java API实现HDFS的相关操作 新建一个Maven项目,并引入依赖。

(1)编写页面代码: MainFram


package com.bigdata.mapreduce.swing;
import com.bigdata.mapreduce.utils.HDFSUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class MainFram extends JFrame{
  private JTree tree=null;
  private JButton btnUpload = new JButton("上传");
  private JButton btnDownload = new JButton("下载");
  private JButton btnCreateDir = new JButton("创建文件夹");
  private JButton btndeleteDirOrFiles = new JButton("删除文件或者文件夹");
  private JButton btnRenameDirOrFiles = new JButton("重命名文件或者文件夹");
  private JButton btnMoveDir = new JButton("移动文件/文件夹");
  private JButton btnListDir = new JButton("列出文件");
  private DefaultMutableTreeNode root=new DefaultMutableTreeNode("我的网盘");
  private JPanel jp_center=new JPanel();
  // 初始化函数
  private void initTree() {
      tree=new JTree(root);
      tree.addTreeSelectionListener(new TreeSelectionListener() {
      @Override
      public void valueChanged(TreeSelectionEvent e) {
        tree_ValueChanged(e);
      }       
      });
    }
  private void tree_ValueChanged(TreeSelectionEvent e) {
//      JOptionPane.showMessageDialog(this,
//      tree.getSelectionPath().getLastPathComponent().toString());
    btnListDir_Clicked();
    }
    public MainFram() {
      JPanel jp=(JPanel)this.getContentPane();
      initTree();
      JScrollPane jsp_tree=new JScrollPane(tree);
      // 创建Panel
        JPanel jp_top=new JPanel();
        // 添加三个按钮
      jp_top.add(btnUpload);
      jp_top.add(btnDownload);
      jp_top.add(btnCreateDir);
      jp_top.add(btndeleteDirOrFiles);
      jp_top.add(btnRenameDirOrFiles);
      jp_top.add(btnMoveDir);
//      jp_top.add(btnListDir);
      JSplitPane splitPane_right=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jp_top,jp_center);
      splitPane_right.setDividerLocation(100);
      splitPane_right.setDividerSize(1);
      JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp_tree,splitPane_right);
      splitPane.setDividerLocation(300);
      splitPane.setDividerSize(2);
      jp.add(splitPane);
      btnUpload.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnUpload_Clicked();
      }
      });
      btnDownload.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnDownload_Clicked();
      }
      });
    btnCreateDir.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnCreateDir_Clicked();
      }
    });
    btnListDir.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnListDir_Clicked();
      }
    });
    btndeleteDirOrFiles.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btndeleteDirOrFiles_Clicked();
      }
    });
    btnRenameDirOrFiles.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnRenameDirOrFiles_Clicked();
      }
    });
    btnMoveDir.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        btnMoveDir_Clicked();
      }
    });
      this.setTitle("我的云盘");
    this.setSize(1200, 800);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void btnDownload_Clicked() {
    System.out.println("download");
    String message = "请输入需要下载的文件,HDFS上的文件:\n";
    try {
      List<String> hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
      for (int k = 0; k < hdfsDir.size(); k++) {
        message += hdfsDir.get(k) + "\n";
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    String srcPath = JOptionPane.showInputDialog(null, message);
    System.out.println(srcPath);
    JFileChooser jf = new JFileChooser();
    jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jf.setDialogTitle("请选择要上传的文件夹...");
    jf.showDialog(null, null);
    String destPath = jf.getSelectedFile().getAbsolutePath() + "/";
//    String[] name = jf.getSelectedFile().getAbsolutePath().split("/");
//    String destPath = "/user";
    if (destPath.isEmpty()) {
      System.out.println("请选择本地路径!");
    } else {
      try {
        HDFSUtil.downloadFromHDFS(srcPath, destPath);
      } catch (Exception e1) {
        e1.printStackTrace();
      }
    }
    System.out.println("下载成功!");
    }
    private void btnUpload_Clicked() {
    System.out.println("upload");
    JFileChooser jf = new JFileChooser();
    jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
    jf.setDialogTitle("请选择要上传的文件...");
    jf.showDialog(null, null);
    String srcPath = jf.getSelectedFile().getAbsolutePath() + "/";
//    String destPath = "/";
    if (srcPath.isEmpty()) {
      System.out.println("本地文件路径不能为空!");
    } else {
      String destPath = JOptionPane.showInputDialog(null, "请输入需要上传到HDFS的路径:");
      if (!destPath.isEmpty()) {
        try {
          HDFSUtil.uploadToHDFS(srcPath, destPath);
        } catch (Exception e1) {
          e1.printStackTrace();
        }
      } else {
        System.out.println("HDFS路径不能为空!");
      }
    }
    System.out.println("上传成功!");
    }
  private void btnMoveDir_Clicked() {
    String message1 = "请输入需要移动的文件或者文件夹名:\n";
    String message2 = "请输入想要移动后的文件夹名:\n";
    try {
      List<String> hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
      for (int k = 0; k < hdfsDir.size(); k++) {
        message1 += hdfsDir.get(k) + "\n";
        message2 += hdfsDir.get(k) + "\n";
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    String oldName = JOptionPane.showInputDialog(null, message1);
    String newName = JOptionPane.showInputDialog(null, message2);
    System.out.println(oldName);
    if (oldName.isEmpty() || newName.isEmpty()) {
      System.out.println("参数错误,请重试!");
    } else {
      try {
        HDFSUtil.moveDirFromHDFS("/" + oldName, "/" + newName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    System.out.println("移动文件/文件夹成功!");
  }
  private void btnRenameDirOrFiles_Clicked() {
    String message1 = "请输入需要重命名的文件或者文件夹名:\n";
    String message2 = "请输入想要重命后的文件或者文件夹名:\n";
    try {
      List<String> hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
      for (int k = 0; k < hdfsDir.size(); k++) {
        message1 += hdfsDir.get(k) + "\n";
        message2 += hdfsDir.get(k) + "\n";
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    String oldName = JOptionPane.showInputDialog(null, message1);
    String newName = JOptionPane.showInputDialog(null, message2);
    System.out.println(oldName);
    if (oldName.isEmpty() || newName.isEmpty()) {
      System.out.println("参数错误,请重试!");
    } else {
      try {
        HDFSUtil.renameFromHDFS("/" + oldName, "/" + newName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    System.out.println("重命名文件/文件夹成功!");
  }
  private void btndeleteDirOrFiles_Clicked() {
    String message = "请输入需要删除的文件或者文件夹名:\n";
    try {
      List<String> hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
      for (int k = 0; k < hdfsDir.size(); k++) {
        message += hdfsDir.get(k) + "\n";
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    String deleteName = JOptionPane.showInputDialog(null, message);
    System.out.println(deleteName);
    if (deleteName.isEmpty()) {
      System.out.println("请输入文件或者文件夹名!");
    } else {
      try {
        HDFSUtil.deleteFromHDFS("/" + deleteName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    System.out.println("删除文件/文件夹成功!");
  }
  private void btnListDir_Clicked() {
    int flag = 0;
    System.out.println("listDir");
    try {
      List<String> hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
//      List<String> hdfsDir = HDFSUtil.listRemoteDir("/");
      List<String> oneDirList = new ArrayList<>();
      DefaultMutableTreeNode oneDir = new DefaultMutableTreeNode();
      DefaultMutableTreeNode twoDir = new DefaultMutableTreeNode();
      for (int i = 0; i < hdfsDir.size(); i++) {
        System.out.println(hdfsDir.get(i));
        String[] arr = hdfsDir.get(i).split("/");
        int length = arr.length;
        // 目前只支持而级目录
        if (length == 2) { // length为2表示只有一个文件或者空文件夹,如:/user
          oneDir = new DefaultMutableTreeNode(arr[1]);
          root.add(oneDir);
        } else if (length == 3) { // length为3表示有两个文件或者两个空文件夹,如:/user/hello.txt
          // 如果一级目录里已经有这个目录,则不添加这个目录
          for (int j = 0; j < oneDirList.size(); j++) {
            if (oneDirList.get(j).equals(arr[1])) {
              flag = 1;
            }
          }
          if (flag == 0) {
            oneDir = new DefaultMutableTreeNode(arr[1]);
            twoDir = new DefaultMutableTreeNode(arr[2]);
            oneDirList.add(arr[1]);
            oneDir.add(twoDir);
            root.add(oneDir);
          } else if (flag == 1) {
            twoDir = new DefaultMutableTreeNode(arr[2]);
            oneDir.add(twoDir);
          }
        }
      }
      System.out.println("列出文件成功!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void btnCreateDir_Clicked() {
    System.out.println("createDir");
    String remoteDirName = JOptionPane.showInputDialog(null, "请输入需要创建的文件夹名:");
    System.out.println(remoteDirName);
    if (remoteDirName.isEmpty()) {
      System.out.println("请输入文件名!");
    } else {
      try {
        HDFSUtil.createDirFromHDFS("/" + remoteDirName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    System.out.println("新建文件夹成功!");
  }
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    new MainFram();
    }
}
相关文章
|
6天前
|
存储 安全 数据管理
新型数据库技术:基于区块链的分布式数据存储系统
传统数据库系统面临着中心化管理、数据安全性和可信度等方面的挑战。本文介绍了一种基于区块链技术的新型数据库系统,通过分布式存储和去中心化的特性,提高了数据的安全性和可信度,同时实现了高效的数据管理和共享。该系统在多个领域如金融、医疗和物联网等具有广阔的应用前景。
|
6天前
|
分布式计算 Hadoop 大数据
大数据技术与Python:结合Spark和Hadoop进行分布式计算
【4月更文挑战第12天】本文介绍了大数据技术及其4V特性,阐述了Hadoop和Spark在大数据处理中的作用。Hadoop提供分布式文件系统和MapReduce,Spark则为内存计算提供快速处理能力。通过Python结合Spark和Hadoop,可在分布式环境中进行数据处理和分析。文章详细讲解了如何配置Python环境、安装Spark和Hadoop,以及使用Python编写和提交代码到集群进行计算。掌握这些技能有助于应对大数据挑战。
|
6天前
|
存储 分布式计算 Hadoop
Hadoop【基础知识 01】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)
【4月更文挑战第3天】Hadoop【基础知识 01】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)
105 3
|
6天前
|
存储 分布式计算 监控
Hadoop【基础知识 01+02】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
【4月更文挑战第3天】【分布式文件系统HDFS设计原理+特点+存储原理】(部分图片来源于网络)【分布式计算框架MapReduce核心概念+编程模型+combiner&partitioner+词频统计案例解析与进阶+作业的生命周期】(图片来源于网络)
142 2
|
5天前
|
存储 分布式计算 Hadoop
Hadoop分布式文件系统(HDFS)
【5月更文挑战第10天】
20 3
|
6天前
|
存储 SQL 分布式计算
Hadoop分布式架构
【5月更文挑战第8天】Hadoop分布式架构
12 4
|
6天前
|
分布式计算 Ubuntu Hadoop
【分布式计算框架】hadoop全分布式及高可用搭建
【分布式计算框架】hadoop全分布式及高可用搭建
27 1
|
6天前
|
存储 分布式计算 Hadoop
【分布式计算框架】Hadoop伪分布式安装
【分布式计算框架】Hadoop伪分布式安装
9 2
|
6天前
|
传感器 存储 SQL
LabVIEW使用ModbusTCP协议构建分布式测量系统
LabVIEW使用ModbusTCP协议构建分布式测量系统
17 4
|
6天前
|
存储 安全 区块链
一文说清楚IPFS分布式存储系统
一文说清楚IPFS分布式存储系统
427 1