基于HBase的云盘项目复制功能总结
2017-08-21 17:12
274 查看
表结构:
(parentId为父文件的ID)
设计思想:
从前台获取的参数:ids(所选中的文件或文件夹的id数组),dst(目的地文件的id)。步骤:1.把ids的文件在HDFS中移动到dst 2.将复制文件添加到file与user_file表中。
难点解析 :复制的难点在于需要将文件信息添加入file表,并且需要将父子文件的关系传到user_file表中。我是这样实现的,递归执行复制文件的函数,但是在递归过程中要先将这一级的文件复制,然后在复制文件夹,具体的实现将在下面的代码中体现,注释写的也很详细。
public List<FileBean> getByParentId(UserBean userBean, @RequestParam(value = "parentId", required = true, defaultValue = "0") long parentId) { List<FileBean> list = new ArrayList<FileBean>(); try { Table table = HBaseDB.getConn().getTable(TableName.valueOf(Constants.HBASE_TABLE_USER_FILE)); //前缀过滤器:筛选出具有特定前缀的行键的行 Scan scan = new Scan(); Filter filter = new PrefixFilter(Bytes.toBytes(userBean.getId()+"_"+parentId+"_")); scan.setFilter(filter); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner){ if(!result.isEmpty()){ long id = Bytes.toLong(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_USER_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_USER_FILE_FILE_ID))); if(id > 0){ //file表中获取文件的详细信息 list.add(getById(id)); } } } resultScanner.close(); table.close(); } catch (IOException e) { e.printStackTrace(); } return list; } private void copyDir(UserBean userBean,FileBean srcBean,FileBean dstBean,String parentPath,long parentId){ //获取父文件下一级的所有文件 List<FileBean> fileBeans = getByParentId(userBean, srcBean.getId()); for(FileBean fileBean : fileBeans){ FileBean fileBean2 = new FileBean(); fileBean2.setDate(fileBean.getDate()); fileBean2.setLength(fileBean.getLength()); fileBean2.setName(fileBean.getName()); fileBean2.setOrginalName(fileBean.getOrginalName()); fileBean2.setPath(fileBean.getPath()); fileBean2.setDir(fileBean.isDir()); fileBean2.setFile(fileBean.isFile()); fileBean2.setType(); fileBean2.setViewflag(); System.out.println("副本:"+fileBean2); addFile(fileBean2); } //优先让该层的文件执行完 //对文件的操作:添加进user_file表 // 更改路径 for(FileBean fileBean : fileBeans){ if(fileBean.isFile()){ try { Table table = HBaseDB.getConn().getTable(TableName.valueOf(Constants.HBASE_TABLE_FILE)); Scan scan = new Scan(); Filter filter = new SingleColumnValueFilter(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH), CompareOp.EQUAL, Bytes.toBytes(fileBean.getPath())); ((SingleColumnValueFilter) filter).setFilterIfMissing(true); scan.setFilter(filter); ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { //获取副本 if(Bytes.toLong(result.getRow()) != fileBean.getId()){ System.out.println("(文件)获取的副本行号:"+Bytes.toLong(result.getRow())); FileBean fileBean2 = new FileBean(); fileBean2.setId(Bytes.toLong(result.getRow())); fileBean2.setDate(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_DATE)))); fileBean2.setLength(Bytes.toLong(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_LENGTH)))); fileBean2.setName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_NAME)))); fileBean2.setOrginalName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_ORIGINAL_NAME)))); fileBean2.setPath(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH)))); fileBean2.setFile(fileBean.isFile()); fileBean2.setDir(fileBean.isDir()); fileBean2.setViewflag(); fileBean2.setType(); resultScanner.close(); table.close(); System.out.println("(文件)移动后文件的父id:" + parentId); addUserFile(userBean, fileBean2, parentId); System.out.println("(文件)副本行被取出: "+"Name:"+fileBean2.getName()+fileBean2.getId()); String path =null; path = fileBean2.getPath().substring(fileBean2.getPath().indexOf("/"+parentPath+"/")+1, fileBean2.getPath().length())+"/"; System.out.println("(文件)被截取后的路径:"+path); System.out.println("(文件)拼出的目标路径:"+dstBean.getPath()+path); setPath(fileBean2,dstBean.getPath()+path); } } } catch (IOException e) { e.printStackTrace(); } } } //然后再让目录进行操作 // 操作:添加进user_file表 for(FileBean fileBean : fileBeans){ if(fileBean.isDir()){ try { Table table = HBaseDB.getConn().getTable(TableName.valueOf(Constants.HBASE_TABLE_FILE)); Scan scan = new Scan(); Filter filter = new SingleColumnValueFilter(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH), CompareOp.EQUAL, Bytes.toBytes(fileBean.getPath())); ((SingleColumnValueFilter) filter).setFilterIfMissing(true); scan.setFilter(filter); ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { if(Bytes.toLong(result.getRow()) != fileBean.getId()){ System.out.println("(文件夹)获取的副本行号:"+Bytes.toLong(result.getRow())); FileBean fileBean2 = new FileBean(); fileBean2.setId(Bytes.toLong(result.getRow())); fileBean2.setDate(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_DATE)))); fileBean2.setLength(Bytes.toLong(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_LENGTH)))); fileBean2.setName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_NAME)))); fileBean2.setOrginalName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_ORIGINAL_NAME)))); fileBean2.setPath(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH)))); fileBean2.setFile(fileBean.isFile()); fileBean2.setDir(fileBean.isDir()); fileBean2.setViewflag(); fileBean2.setType(); resultScanner.close(); table.close(); System.ou cfe7 t.println("(文件夹)移动后文件的父id:" + parentId); addUserFile(userBean, fileBean2, parentId); } } } catch (IOException e) { e.printStackTrace(); } } } //递归 //更改路径 for(FileBean fileBean : fileBeans){ if(fileBean.isDir()){ try { //找到副本 Table table = HBaseDB.getConn().getTable(TableName.valueOf(Constants.HBASE_TABLE_FILE)); Scan scan = new Scan(); Filter filter = new SingleColumnValueFilter(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH), CompareOp.EQUAL, Bytes.toBytes(fileBean.getPath())); ((SingleColumnValueFilter) filter).setFilterIfMissing(true); scan.setFilter(filter); ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { //获取副本 if(Bytes.toLong(result.getRow()) != fileBean.getId()){ FileBean fileBean2 = new FileBean(); fileBean2.setId(Bytes.toLong(result.getRow())); fileBean2.setDate(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_DATE)))); fileBean2.setLength(Bytes.toLong(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_LENGTH)))); fileBean2.setName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_NAME)))); fileBean2.setOrginalName(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_ORIGINAL_NAME)))); fileBean2.setPath(Bytes.toString(result.getValue(Bytes.toBytes(Constants.HBASE_FAMILY_FILE_FILE), Bytes.toBytes(Constants.HBASE_COLUMN_FILE_PATH)))); fileBean2.setFile(fileBean.isFile()); fileBean2.setDir(fileBean.isDir()); fileBean2.setViewflag(); fileBean2.setType(); //取出parentid parentId = Bytes.toLong(result.getRow()); System.out.println("parentid:"+parentId); resultScanner.close(); table.close(); System.out.println("(文件夹)副本行被取出: "+"Name:"+fileBean2.getName()+fileBean2.getId()); String path =null; path = fileBean2.getPath().substring(fileBean2.getPath().indexOf("/"+parentPath+"/")+1, fileBean2.getPath().length()); System.out.println("(文件夹)被截取后的路径:"+path); System.out.println("(文件夹)拼出的目标路径:"+dstBean.getPath()+path); setPath(fileBean2,dstBean.getPath()+path); copyDir(userBean, fileBean, dstBean, parentPath,parentId); } } System.out.println("进行递归的文件夹:"+fileBean); } catch (IOException e) { e.printStackTrace(); } } } } private Long setCopyPath(UserBean userBean,FileBean srcBean,String desPath,long parentId) { try { //不设置id //long id = HBaseDB.getId(Constants.HBASE_TABLE_GID, Constants.HBASE_FAMILY_GID_GID, Constants.HBASE_ROW_KEY_GID_FILEID, Constants.HBASE_COLUMN_GID_FILEID); FileBean fileBean = new FileBean(); //fileBean.setId(id); fileBean.setDate(srcBean.getDate()); fileBean.setLength(srcBean.getLength()); fileBean.setName(srcBean.getName()); fileBean.setOrginalName(srcBean.getOrginalName()); fileBean.setPath(desPath); fileBean.setDir(srcBean.isDir()); fileBean.setFile(srcBean.isFile()); fileBean.setType(); fileBean.setViewflag(); //这样会在filebean给它赋值id addFile(fileBean); addUserFile(userBean, fileBean, parentId); System.out.println("根目录的parentId:"+parentId); System.out.println("根目录的id:"+fileBean.getId()); return fileBean.getId(); } catch (Exception e) { // TODO: handle exception return null; } } public void copy(UserBean userBean, String ids, long dst) { String[] idsStr = ids.split(","); //将复制文件添加到file与user_file表中 for(String id : idsStr){ FileBean srcBean = getById(Long.valueOf(id)); String parentPath = ""; //获取源路径的名称 if(srcBean.isDir()){ //目录 String[] strs = srcBean.getPath().split("/"); //在list.jsp上选择的目录的目录名称 System.out.println("按'/'切分后数组的长度:"+strs.length); parentPath = strs[strs.length-1]; for(String string : strs){ System.out.println(" 切分结果:"+string); } System.out.println("parentPath:"+parentPath); //先对源目录进行操作,源目录的id可以获得 System.out.println("源文件根目录移动后的目录"+dstBean.getPath()+parentPath+"/"); long parnetId = setCopyPath(userBean,srcBean, dstBean.getPath()+parentPath+"/",dst); copyDir(userBean, srcBean, dstBean, parentPath,parnetId); }else{ setCopyPath(userBean,srcBean, dstBean.getPath()+srcBean.getPath().substring(srcBean.getPath().indexOf("/"+parentPath+"/")+1, srcBean.getPath().length())+"/",dst); System.out.println("源文件根目录移动后的目录"+dstBean.getPath()+srcBean.getPath().substring(srcBean.getPath().indexOf("/"+parentPath+"/")+1, srcBean.getPath().length())); } } }
相关文章推荐
- 基于Metronic的Bootstrap开发框架经验总结(8)--框架功能总体界面介绍
- 大学经历的项目总结——一、基于物联网的森林防火舆情监测系统
- [github项目]基于百度地图二次开发实现的车辆监管(包含车辆定位、车辆图片和方向控制,电子围栏,图形绘制等功能)前端实现(不包含后端实现)
- 基于Server-Sent Event的简单聊天室 Web 2.0时代,即时通信已经成为必不可少的网站功能,那实现Web即时通信的机制有哪些呢?在这门项目课中我们将一一介绍。最后我们将会实现一个基于Server-Sent Event和Flask简单的在线聊天室。
- [置顶]Android 项目开发 基于Web Service 服务的中英翻译软件(一)功能介绍
- [webAPP项目]基于MUI框架开发APP功能点开发详解大纲
- Apad Qzone项目总结(二)---换肤功能实现!!!
- 关于工作中声控功能开发(基于windows 7)的问题和总结
- 基于大数据的电影网站项目开发之HBase分布式安装(四)
- ListView项目长按的菜单功能总结
- OA项目 分页功能总结一 简单的分页(资料来源于传智播客汤阳光的视频教程)
- 科猫网项目总结(基于SSM框架)
- iOS 基于 MVC 的项目重构总结
- [React项目总结] 基于 webpack 搭建前端工程基础篇
- 项目总结(三)——复制文件时显示进度对话框
- 基于Z301P摄像头 H.264OK6410的远程视频web监控 项目阶段1总结篇
- 手机应用在开发即时通讯项目功能时,不要使用基于socket.io的数据推送
- 简笔画项目总结: ios绘图机制 & 实现记录笔迹功能
- [webAPP项目]基于MUI框架webAPP开发功能流程之引导图制作详解01
- iOS基于MVC的项目重构总结