栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

大家好,我是邵奈一,一个不务正业的程序猿、正儿八经的斜杠青年。
1、世人称我为:被代码耽误的诗人、没天赋的书法家、五音不全的歌手、专业跑龙套演员、不合格的运动员…
2、这几年,我整理了很多IT技术相关的教程给大家,爱生活、爱分享。
3、如果您觉得文章有用,请收藏,转发,评论,并关注我,谢谢!
博客导航跳转(请收藏):邵奈一的技术博客导航
| 公众号 | 微信 | CSDN | 掘金 | 51CTO | 简书 | 微博 |


教程目录
  • 0x00 教程内容
  • 0x01 基于Hadoop分布式存储的网盘系统实现
        • 1. 准备工作
        • 2. 编写代码
        • 3. 运行效果
        • 4. 实现说明
  • 0xFF 总结

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 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 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 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 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 hdfsDir = HDFSUtil.listRemoteDirAndFiles("/");
//			List hdfsDir = HDFSUtil.listRemoteDir("/");
			List 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();
    }

}

(2)编写HDFS代码: HDFSUtil

package com.bigdata.mapreduce.utils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.* ;
public class HDFSUtil {
	private static String hdfsURL="hdfs://localhost:9000";
	private static Configuration conf;
	static {
		conf = new Configuration();
		conf.set("fs.defaultFS", hdfsURL);
	}

	// 从HDFS上下载文件
	public static void downloadFromHDFS(String remoteFile,String localFile) throws Exception {
			FileSystem fs = FileSystem.get(conf);
			Path remotePath = new Path(remoteFile);
			Path localPath = new Path(localFile);
			fs.copyToLocalFile(remotePath, localPath);
			fs.close();
	}

	// 上传文件到HDFS
	public static void uploadToHDFS(String localfile,String remotefile) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path remotePath = new Path(remotefile);
		Path localPath = new Path(localfile);
		fs.copyFromLocalFile(localPath, remotePath);
		fs.close();
	}

	// 创建HDFS文件夹
	public static void createDirFromHDFS(String remoteDir) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path remotePath = new Path(remoteDir);
		//调用mkdirs函数创建目录
		fs.mkdirs(remotePath);
		fs.close();
	}

	// 删除文件和文件夹
	public static void deleteFromHDFS(String remoteDir) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path remotePath = new Path(remoteDir);
		//调用mkdirs函数创建目录,true表示循环递归删除
		fs.delete(remotePath, true);
		fs.close();
	}

	// 重命名文件
	public static void renameFromHDFS(String oldName, String newName) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path hdfsOldName = new Path(oldName);
		Path hdfsNewName = new Path(newName);
		fs.rename(hdfsOldName, hdfsNewName);
		fs.close();
	}

	// 查看文件夹
	public static void ListHDFSDir(String remoteDir) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path dirPath = new Path(remoteDir);
		
		RemoteIterator remoteIterator = fs.listFiles(dirPath, true);
		
		while (remoteIterator.hasNext()) {
			FileStatus s = remoteIterator.next();
			System.out.println("路径: " + s.getPath().toString());
			System.out.println("权限: " + s.getPermission().toString());
			System.out.println("大小: " + s.getLen());
			
			Long timeStamp = s.getModificationTime();
			SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
			String date = format.format(timeStamp);
			System.out.println("时间: " + date);
			System.out.println();
		}
		fs.close();
	}

	// 列出文件
	public static List listRemoteDirAndFiles(String remoteDir) throws Exception {
		List remoteDirList = new ArrayList<>();
		FileSystem fs = FileSystem.get(conf);
		Path dirPath = new Path(remoteDir);
		
		RemoteIterator remoteIterator = fs.listFiles(dirPath, true);
		
		while (remoteIterator.hasNext()) {
			FileStatus s = remoteIterator.next();
			String myPath = s.getPath().toString().substring(21);
			remoteDirList.add(myPath);
		}
		fs.close();
		return remoteDirList;
	}

	// 列出文件夹
	public static List listRemoteDir(String remoteDir) throws Exception {
		List remoteDirList = new ArrayList<>();
		FileSystem fs = FileSystem.get(conf);
		Path dirPath = new Path(remoteDir);
		FileStatus[] fileStatus = fs.listStatus(dirPath);
		for (FileStatus file : fileStatus) {
			if (file.isDirectory()) {
				listRemoteDir(file.getPath().toString());
				remoteDirList.add(file.getPath().toString().substring(21));
			} else {
				remoteDirList.add(file.getPath().toString().substring(21));
			}
		}
		fs.close();
		return remoteDirList;
	}

	// 移动文件夹
	public static void moveDirFromHDFS(String oldPath, String newPath) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path hdfsOldName = new Path(oldPath);
		Path hdfsNewName = new Path(newPath);
		fs.rename(hdfsOldName, hdfsNewName);
		fs.close();
	}

	// 测试
//	public static void main(String[] args) {
//		try {
			// 1、列出文件
//			List list = HDFSUtil.listRemoteDirAndFiles("/");
//			for (int i = 0; i < list.size(); i++) {
//				System.out.println(list.get(i));
//			}

			// 2、重名文件
//			HDFSUtil.renameFromHDFS("/start-dfs.cmd", "/start-dfs.sh");

			// 3、创建文件夹
//			HDFSUtil.createDirFromHDFS("/hive");

			// 4、移动文件夹(把/路径下的hive文件夹,放到/user路径)
//			HDFSUtil.moveDirFromHDFS("/hive", "/user");

			// 5、删除文件或者文件夹
//			HDFSUtil.deleteFromHDFS("/user");
//			HDFSUtil.deleteFromHDFS("/user/hive/start-dfs.sh");


//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
3. 运行效果

很多的功能都已经实现,虽然不是特别地实用。

直接运行MainFram类就可以看到运行效果:

4. 实现说明

(1)如果需要修改HDFS的链接,可以修改代码的值:

private static String hdfsURL="hdfs://localhost:9000";

(2)目前目录树其实是还不规范的,只是站在零基础同学角度的一个实现方案,需要自行完善。

0xFF 总结
  1. 通过本教程,可以扩展大家的思维,其实我们学了HDFS的API操作,并不是一无是处的,我们其实是可以自己写一个简单的小工具来使用的。
  2. 关注本博主,学习更多有趣的知识。

邵奈一 原创不易,如转载请标明出处,教育是一生的事业。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号