项目开发环境:Linux下Hadoop分布式系统
项目开发环境:Ubuntu11.04+Hadoop0.20.2+JDK1.6+Eclipse3.3.2。
使用技术:Hadoop + Java
作品展示地址:http://blog.csdn.net/jtlyuan/article/details/7980826
作品描述:1、个人独立完成,课余兴趣作品。包含全部设计、分析、编码、优化。
2、功能实现,文件上传、下载、删除、维护、文件夹的新建、文件路径跟踪、以及个人文件搜索功能实现和文件分类展现等。
3、基于Hadoop开发的分布式云平台的文件管理系统。
一、 概况:
1、这是个人的业余爱好项目,自己进行了全部的设计、分析、编码、和优化。
2、 根据现场需求进行流程分析与编写。
3、 根据所需求的业务进行开发,代码编写,实现功能。
4、 对程序业务逻辑进行优化,使其达到更高的性能。
项目难点:1、搜索的实现利用了回溯法进行所有文件的搜索,利用字符是否包含来判断是否进入结果容器中
2、目录跟踪显示的实现
二、这是本人仿照《百度网盘》,利用Hadoop技术进行开发的《Hadoop云盘系统》
如下图所示,《百度网盘》和自己做的《Hadoop云盘系统》进行了对比。

再看个人的《云盘》

总结:界面设计简洁,整齐,操作方面,用户体验良好。
三、Hadoop集群主要配置和启动操作操作过程
1、查看集群的主节点配置。先在Linux中启动Hadoop,如下:查看现在JPS运行的进程,检查系统是否正常启动

2、查看 core-site.xml文件查看主节点的配置。

3、并在web中 查看 http://192.168.236.132:50030/ 和http://192.168.236.132:50070/ web界面集群启动情况,确定
无误后,可以利用Eclipse 启动程序运行程序了。
四、系统部分测试和主要代码解析
1、上传文件,是从本地文件系统中上传到HDFS中,上传到当前进入的目录当中

主要代码分析:
- JFileChooser chooser = new JFileChooser();
- chooser.setVisible(true);
- int returnVal = chooser.showOpenDialog(null);
- if (returnVal == JFileChooser.APPROVE_OPTION) {// 为确定或OK是
- String localPath = chooser.getSelectedFile()
- .getPath();
- String filename = chooser.getSelectedFile()
- .getName();
- InputStream in = null;
- try {
- in = new BufferedInputStream(
- new FileInputStream(localPath));//本地文件输入流
- } catch (FileNotFoundException e3) {
- e3.printStackTrace();
- }
- OutputStream out = null;
- try {
- out = hdfs.create(new Path(currentPath
- + "/" + filename),
- new Progressable() {
- public void progress() {
- System.out.print(".");
- }
- });//HDFS路径的输出流抽象
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- try {
- IOUtils.copyBytes(in, out, 4096, true);//利用IOUtils工具类实现上传
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- showTable(currentPath);//上传完毕就刷新当前路径的文件表格
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
2、文件下载分析:从在HDFS中下载到本地文件系统中,
主要代码分析:
- if (e.getSource() == deleItem) {
- int ensuce = JOptionPane.showConfirmDialog(new MainWindow(),
- "确定删除所选文件吗", "确认对话框", JOptionPane.YES_NO_OPTION);
- if (ensuce == JOptionPane.NO_OPTION) {
- return;
- }
- if (ensuce == JOptionPane.YES_OPTION) {
- if (fileList.getSelectedRow() >= 0) {
- String temp = currentPath
- + "/"
- + fileList.getValueAt(
- fileList.getSelectedRow(), 0);//获取要删掉文件的路径
- try {
- hdfs.delete(new Path(temp), true);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- showTable(currentPath);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
3、文件表格展现
主要代码:
- /*-------------------------------------把currentPath路径下的文件和文件夹属性全都显示表格中----------------------------------------------------------*/
- private void showTable(String currentPath) throws IOException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- Path inputDir = new Path(currentPath);/* 获取文件的路径*/
- /* FileStatue类*/
- FileStatus[] status = hdfs.listStatus(inputDir);/* 得到文件路径目录下文件列表*/
- DefaultTableModel model = (DefaultTableModel) fileList.getModel(); // 获取表格模型
- if (fileList.getRowCount() != 0) { // 当表格中有数据
- ((DefaultTableModel) fileList.getModel()).setRowCount(0);// 将表格置空
- }
- for (int i = 0; i < status.length; i++) {
- String filename = null;
- String lenStr = null;
- String modifDate = null;
- filename = status[i].getPath().getName();
- String length = null; // 获取文件大小信息
- DecimalFormat df = new DecimalFormat("#.00");
- if (status[i].isDir()) {
- lenStr = "-";
- } else {
- if (status[i].getLen() > (1024 * 1024 * 1024)) {
- length = df.format(status[i].getLen()
- / (1024.0 * 1024 * 1024));
- lenStr = " " + length + "G";
- } else if (status[i].getLen() > (1024 * 1024)) {
- length = df.format(status[i].getLen() / (1024.0 * 1024));
- lenStr = " " + length + "M";
- } else if (status[i].getLen() > 1024) {
- length = df.format(status[i].getLen() / 1024.0);
- lenStr = " " + length + "KB";
- } else {
- length = df.format(status[i].getLen());
- lenStr = " " + length + "B";
- }
- }
- modifDate = sdf.format(status[i].getModificationTime());
- model.addRow(new Object[] { filename, lenStr, modifDate }); // 将文件名、文件大小、文件创建日期添加到表格
- }
- }
4、文件搜索功能实现(利用回溯算法+字符匹配来实现)
先测试一下功能吧!我们输入“Hadoop”关键字搜索比配的文件
再入:输入“数学”搜索一下结果
主要实现代码
- showAllResult(target);//具体实现
- /*--------------------获取所有要搜索到的文件路径---------------------------------*/
- private List<String> findAllFile(String target) {
- List<String> result = new ArrayList<String>();
- char[] tar = target.toCharArray();
- int count = 0;
- String findPath = currentPath;//默认搜索的路径是目前打开目录下为根的目录树
- getAllFile(tar, result, findPath, count);
- return result;
- }
- /*-----------------------------回溯检测树形下的文件---------------------------------------------------------*/
- private void getAllFile(char[] tar, List<String> result, String findPath,
- int count) {
- conf = new Configuration();
- try {
- hdfs = FileSystem.get(URI.create(findPath), conf);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (hdfs.isFile(new Path(findPath))) {
- String name = hdfs.getFileStatus(new Path(findPath)).getPath()
- .getName();
- if (isFind(tar, name.toCharArray())) {//检测是否字符匹配,匹配为找到
- result.add(findPath);// 搜索到加入数组
- }
- return;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- FileStatus[] sta = null;
- try {
- sta = hdfs.listStatus(new Path(findPath));
- } catch (IOException e) {
- e.printStackTrace();
- }
- for (int i = 0; i < sta.length; i++) {//回溯法实现循环递归遍历
- getAllFile(tar, result, sta[i].getPath().toString(), count++);
- }
- }
- /*-----------------------查看字符串是否包含--------------------------------------------*/
- private boolean isFind(char[] tar, char[] sour) {
- int all=0.0;for (int i = 0; i < tar.length; i++) {
- int j = 0;
- for (; j < sour.length; ++j) {
- if (tar[i] == sour[j]) {
- all++;break;
- }
- }
- if (j == sour.length&&all/sour.length<0.75) {//概率匹配
- return false;
- }
- }
- return true;
- }
5、文件分类实现查询
a、文件分类管理查看,查看“文档”列出系统中所有的文档
b、“图片”
c、“音乐”
等等 。。。 。。。
6、其他的实现,目录文件跟踪、文件维护等:
*最后说说本系统的信息处理的实现---MapReduce解决
首先说下其实登陆不只是利用数据库来实现的,解决方法有如下几种:
方案一:用另外一台机器专门用于对数据库操作的。要是在一个Hadoop中集群中安装一个数据库,我们不应该把数据库放在namenode中,而是放到另外的一台机,因
为namenode的任务已经够多了,安装在namenode上,多用户登陆时对数据库操作会消耗namenode的内存,会影响namenode对datanode的管理和调度等。
所以我们应该放到指定的一台机器上。
方案二:利用利用Hadoop中分布式数据库HBase解决。这个毫无疑问是最好的解决方案,是针对云技术分布式的数据库。不过本人对HBase还是处于了解阶段,所以没
有用上它。
最终方案:HBase用不上,不过没关系,因为现在只是对一个用户信息处理实现,业务量很少,我可以仿照Hive那样,在数据进行的时候转化为MapReduce进行,利用MapReduce来进行表与表的关联。
例如如下表: 信息表:
登陆表:
有待扩展的功能模块:我的分享----可以做成分享一个文件提供所有人下载,也可以做成分享给具体的某个用户。
实现方式:做一个分享表,记录了 分享人,被分享人,文件获取的路径(或者获取文件所需的参数参数)
如下: