in odps-console-basic/src/main/java/com/aliyun/openservices/odps/console/utils/ExportProjectUtil.java [264:392]
private static void exportResources(Odps odps, String localPath, ExecutionContext sessionContext,
StringBuilder addResourceBuilder, StringBuilder dropResourceBuilder)
throws ODPSConsoleException, OdpsException {
Resources resourceMetaList = odps.resources();
List<Resource> resourceList = new ArrayList<Resource>();
for (Resource resource : resourceMetaList) {
resourceList.add(resource);
}
sessionContext.getOutputWriter().writeResult(
"----Total resources:" + resourceList.size() + "\r\n");
addResourceBuilder.append("----Total resources:" + resourceList.size() + "\r\n");
for (Resource resourceMeta : resourceList) {
Type type = resourceMeta.getType();
StringBuilder resourceCommandBuf = new StringBuilder();
if (resourceMeta.getName() == null || resourceMeta.getName().equals("")) {
addResourceBuilder.append("---Failed: no resource name---:" + type + "\r\n");
continue;
}
String alias = resourceMeta.getName();
// File name, 为了避免文件名冲突,使用alias作为文件名
String filePath = localPath + "/resources/" + alias;
// 要注意文件名和alias关系, file和ARCHIVE的情况是用户可以指定alias名称的
String filename = resourceMeta.getName();
switch (type) {
case FILE:
if (filename != null && !filename.trim().equals("")) {
int index1 = filename.lastIndexOf("/");
int index2 = filename.lastIndexOf("\\");
// file的情况把alias和文件名作为后缀
filePath = filePath + "_" + filename.substring((index1 > index2 ? index1 : index2) + 1);
}
resourceCommandBuf.append("add file ").append(filePath).append(" as ").append(alias);
break;
case ARCHIVE:
if (filename != null && !filename.trim().equals("")) {
int index1 = filename.lastIndexOf("/");
int index2 = filename.lastIndexOf("\\");
// file的情况把alias和文件名作为后缀
filePath = filePath + "_" + filename.substring((index1 > index2 ? index1 : index2) + 1);
}
resourceCommandBuf.append("add archive ").append(filePath).append(" as ").append(alias);
break;
case TABLE:
// head meta
/*
HACK: since "list resources" request cannot get partition info, we need to do an
additional "get resource" request. We will fix this later on server side.
*/
TableResource tableResourceMeta =
(TableResource) odps.resources().get(resourceMeta.getName());
resourceCommandBuf.append("add table ");
resourceCommandBuf.append(tableResourceMeta.getSourceTable().getName());
PartitionSpec partitionSpec = tableResourceMeta.getSourceTablePartition();
if (partitionSpec != null) {
resourceCommandBuf.append(" partition(").append(partitionSpec.toString()).append(")");
}
resourceCommandBuf.append(" as ").append(alias);
break;
case PY:
resourceCommandBuf.append("add py ").append(filePath);
break;
case JAR:
resourceCommandBuf.append("add jar ").append(filePath);
break;
default:
throw new ODPSConsoleException("Invalid resource type : " + type.toString());
}
String comment = resourceMeta.getComment();
if (comment == null || comment.trim().equals("")) {
resourceCommandBuf.append(";\r\n");
} else {
resourceCommandBuf.append(" comment ").append(comment).append(";\r\n");
}
boolean resourceExist = true;
// download resource
if (!resourceMeta.getType().equals(Type.TABLE)) {
try {
FileResource fileResource = (FileResource) resourceMeta;
InputStream inputStream = odps.resources().getResourceAsStream(fileResource.getProject(),
fileResource.getName());
try {
FileUtil.saveInputStreamToFile(inputStream, filePath);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
// do nothing
}
}
} catch (Exception e) {
resourceExist = false;
sessionContext.getOutputWriter().writeResult(
"----Failed: can't find resource:" + resourceMeta.getName() + "\r\n");
addResourceBuilder.append("----Failed: can't find resource:" + resourceMeta.getName()
+ "\r\n"); // just print stack , don't break;
e.printStackTrace();
}
}
if (resourceExist) {
sessionContext.getOutputWriter().writeResult("drop resource " + alias + ";\r\n");
dropResourceBuilder.append("drop resource " + alias + ";\r\n");
if (!"".equals(resourceCommandBuf.toString())) {
sessionContext.getOutputWriter().writeResult(resourceCommandBuf.toString());
}
addResourceBuilder.append(resourceCommandBuf);
}
}
}