AddResourceCommand extends AbstractCommand { public static AddResourceCommand parse(String commandString, ExecutionContext sessionContext) throws ODPSConsoleException { if (isSecurityCommand(commandString)) { return null; } String oldString = commandString; if (commandString.toUpperCase().matches( "\\s*ADD\\s+((FILE)|(PY)|(JAR)|(ARCHIVE)|(TABLE)|(VOLUMEFILE)|(VOLUMEARCHIVE))[\\s\\S]*")) { commandString = commandString.replaceAll("\\s+", " ").trim(); commandString = commandString.substring(4); boolean isUpdate = false; if (commandString.toUpperCase().endsWith(" -F")) { isUpdate = true; commandString = commandString.substring(0, (commandString.length() - 2)).trim(); } int commandIndex = commandString.indexOf(" "); if (commandIndex == -1) { throwUsageError(System.err, "bad command"); } String command = commandString.substring(0, commandIndex).trim().toUpperCase(); commandString = commandString.substring(commandIndex).trim(); int fileIndex = commandString.indexOf(" "); String refName; if (fileIndex == -1) { refName = commandString; } else { refName = commandString.substring(0, fileIndex); } commandString = commandString.substring(refName.length()).trim(); String partitionSpec = ""; if (command.equals("TABLE")) { if (commandString.toUpperCase().indexOf("PARTITION") == 0 && commandString.indexOf("(") > 0 && commandString.indexOf(")") > 0) { partitionSpec = commandString.substring(commandString.indexOf("(") + 1, commandString.indexOf(")")); commandString = commandString.substring(commandString.indexOf(")") + 1).trim(); } } int asIndex = commandString.toUpperCase().indexOf("AS "); String alias = ""; if (asIndex == 0) { commandString = commandString.substring(2).trim(); int asIndexSpace = commandString.indexOf(" "); if (asIndexSpace > 0) { alias = commandString.substring(0, asIndexSpace); } else { alias = commandString; } } commandString = commandString.substring(alias.length()).trim(); int commentIndex = commandString.toUpperCase().indexOf("COMMENT "); String comment = ""; if (commentIndex == 0) { commandString = commandString.substring("COMMENT ".length()).trim(); comment = commandString; } commandString = commandString.substring(comment.length()).trim(); if (!commandString.isEmpty()) { String warningName = "AddResourceCommandAsMissing"; Long count = OdpsDeprecatedLogger.getDeprecatedCalls().get(warningName); if (count == null) { count = 1L; } else { count += 1L; } OdpsDeprecatedLogger.getDeprecatedCalls().put(warningName, count); System.err.println( "Warning: ignore part \"" + commandString + "\" in command, maybe 'AS' is missing"); } if (command.equals("TABLE") && (alias == null || alias.length() == 0)) { alias = refName; } AddResourceCommand addResourceCommand = new AddResourceCommand(oldString, sessionContext); addResourceCommand.type = command; addResourceCommand.refName = refName.replaceAll("'", "").replaceAll("\"", ""); addResourceCommand.alias = alias.replaceAll("'", "").replaceAll("\"", ""); addResourceCommand.comment = comment; addResourceCommand.partitionSpec = partitionSpec.replaceAll(" ", ""); addResourceCommand.isUpdate = isUpdate; return addResourceCommand; } return null; } AddResourceCommand(String commandText, ExecutionContext context);  AddResourceCommand(String commandText,
                            ExecutionContext context, String refName, String alias,
                            String comment, String type, String partitionSpec, boolean isUpdate, String projectName); static void printUsage(PrintStream out); String getRefName(); String getAlias(); String getComment(); String getType(); String getPartitionSpec(); boolean isUpdate(); void run(); static boolean isSecurityCommand(String cmd); static AddResourceCommand parse(String commandString, ExecutionContext sessionContext); static final String[] HELP_TAGS; }
@Test public void testAddResourceCommandParser() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "add archive file.zip /home/admin/alisatasknode/taskinfo command = AddResourceCommand.parse(commandText, context); }
@Test public void testAddResourceVolumeArchive() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "add volumearchive /volumename/a/b.zip as file.zip"; command = AddResourceCommand.parse(commandText, context); assertNotNull(command); }
@Test public void testAddResourceVolueArchiveNeg2() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "add volumeblabla b.a as b.a"; command = AddResourceCommand.parse(commandText, context); assertNull(command); }
@Test public void testAddResourceJarNotCheck() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "add jar a as a.jar"; command = AddResourceCommand.parse(commandText, context); assertNotNull(command); }
@Test public void testAddResourcePyNotCheck() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "add py a as a.py"; command = AddResourceCommand.parse(commandText, context); assertNotNull(command); }
@Test public void testAddVolumeArchiveExecute() throws ODPSConsoleException, OdpsException, IOException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AbstractCommand command; Odps odps = OdpsConnectionFactory.createOdps(context); if (odps.resources().exists("volumearchive.zip")) { odps.resources().delete("volumearchive.zip"); } if (!odps.volumes().exists("addvolumetest")) { odps.volumes().create("addvolumetest", null); } VolumeTunnel tunnel = new VolumeTunnel(odps); VolumeTunnel.UploadSession session = tunnel.createUploadSession(odps.getDefaultProject(), "addvolumetest", "addvolumetest"); OutputStream os = session.openOutputStream("volumearchive.zip"); os.write(IOUtils.readFully(this.getClass().getResourceAsStream("/resource.zip"))); os.close(); System.out.println(StringUtils.join(session.getFileList())); session.commit(session.getFileList()); commandText = String.format("add volumearchive /addvolumetest/addvolumetest/volumearchive.zip as volumearchive.zip"); command = AddResourceCommand.parse(commandText, context); assertNotNull(command); command.execute(); command = ListResourcesCommand.parse("list resources", context); command.execute(); command = DescribeResourceCommand.parse("desc resource volumearchive.zip", context); command.execute(); commandText = String.format("add volumearchive /addvolumetest/addvolumetest/volumearchive.zip as volumearchive.zip -f"); command = AddResourceCommand.parse(commandText, context); assertNotNull(command); command.execute(); }