您的位置:首页 > 其它

基于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()));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐