0x00 教程内容
- 准备工作
- 编写代码
- 运行效果
- 实现说明
说明:本实现非常地粗糙,仅供参考。通过本教程,可以看到运行结果,并且实现跟 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(); } }