Caideyipi commented on code in PR #12541: URL: https://github.com/apache/iotdb/pull/12541#discussion_r1620211866
########## iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportTsFile.java: ########## @@ -0,0 +1,459 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.tool; + +import org.apache.iotdb.cli.utils.IoTPrinter; +import org.apache.iotdb.exception.ArgsErrorException; +import org.apache.iotdb.session.pool.SessionPool; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.FileStore; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; + +public class ImportTsFile extends AbstractTsFileTool { + + private static final String SOURCE_ARGS = "s"; + private static final String SOURCE_NAME = "source"; + + private static final String ON_SUCCESS_ARGS = "os"; + private static final String ON_SUCCESS_NAME = "on_success"; + + private static final String SUCCESS_DIR_ARGS = "sd"; + private static final String SUCCESS_DIR_NAME = "success_dir"; + + private static final String FAIL_DIR_ARGS = "fd"; + private static final String FAIL_DIR_NAME = "fail_dir"; + + private static final String ON_FAIL_ARGS = "of"; + private static final String ON_FAIL_NAME = "on_fail"; + + private static final String THREAD_NUM_ARGS = "tn"; + private static final String THREAD_NUM_NAME = "thread_num"; + + private static final IoTPrinter ioTPrinter = new IoTPrinter(System.out); + + private static final String TSFILEDB_CLI_PREFIX = "ImportTsFile"; + + private static String source; + private static String sourceFullPath; + private static String failDir = "fail/"; + private static String successDir = "success/"; + private static String onSuccess; + private static String onFail; + private static Operation successOperation; + private static Operation failOperation; + private static int threadNum = 8; + private static final LinkedBlockingQueue<String> linkedBlockingQueue = + new LinkedBlockingQueue<>(); + + private static void createOptions() { + createBaseOptions(); + + Option opSource = + Option.builder(SOURCE_ARGS) + .longOpt(SOURCE_NAME) + .required() + .required() + .argName(SOURCE_NAME) + .hasArg() + .desc( + "If input a file path, load a file, " + + "otherwise load all file under this directory (required)") + .build(); + options.addOption(opSource); + + Option opOnSuccess = + Option.builder(ON_SUCCESS_ARGS) + .longOpt(ON_SUCCESS_NAME) + .argName(ON_SUCCESS_NAME) + .required() + .hasArg() + .desc( + "When loading tsfile successfully, do operation on tsfile, optional parameters are none, mv, cp, delete.") + .build(); + options.addOption(opOnSuccess); + + Option opOnFail = + Option.builder(ON_FAIL_ARGS) + .longOpt(ON_FAIL_NAME) + .argName(ON_FAIL_NAME) + .required() + .hasArg() + .desc( + "When loading tsfile failed, do operation on tsfile, optional parameters are none, mv, cp, delete.") + .build(); + options.addOption(opOnFail); + + Option opSuccessDir = + Option.builder(SUCCESS_DIR_ARGS) + .longOpt(SUCCESS_DIR_NAME) + .argName(SUCCESS_DIR_NAME) + .hasArg() + .desc("When os is mv, cp, you need to specify the folder to operate on.") + .build(); + options.addOption(opSuccessDir); + + Option opFailDir = + Option.builder(FAIL_DIR_ARGS) + .longOpt(FAIL_DIR_NAME) + .argName(FAIL_DIR_NAME) + .hasArg() + .desc("When of is mv, cp, you need to specify the folder to operate on.") + .build(); + options.addOption(opFailDir); + + Option opThreadNum = + Option.builder(THREAD_NUM_ARGS) + .longOpt(THREAD_NUM_NAME) + .argName(THREAD_NUM_NAME) + .hasArgs() + .desc("Support for concurrent parameters") + .build(); + options.addOption(opThreadNum); + } + + public static void main(String[] args) { + createOptions(); + hf = new HelpFormatter(); + hf.setOptionComparator(null); + hf.setWidth(MAX_HELP_CONSOLE_WIDTH); + CommandLine commandLine = null; + CommandLineParser parser = new DefaultParser(); + + if (args == null || args.length == 0) { + ioTPrinter.println("Too few params input, please check the following hint."); + hf.printHelp(TSFILEDB_CLI_PREFIX, options, true); + System.exit(CODE_ERROR); + } + + try { + CommandLine helpCommandLine = parser.parse(helpOptions, args, true); + if (helpCommandLine.hasOption(HELP_ARGS)) { + hf.printHelp(TSFILEDB_CLI_PREFIX, options, true); + System.exit(CODE_ERROR); + } + } catch (ParseException e) { + ioTPrinter.println("Parse error: " + e.getMessage()); + hf.printHelp(TSFILEDB_CLI_PREFIX, options, true); + System.exit(CODE_ERROR); + } + + try { + commandLine = parser.parse(options, args, true); + } catch (ParseException e) { + ioTPrinter.println("Parse error: " + e.getMessage()); + hf.printHelp(TSFILEDB_CLI_PREFIX, options, true); + System.exit(CODE_ERROR); + } + + try { + parseBasicParams(commandLine); + parseSpecialParams(commandLine); + } catch (ArgsErrorException e) { + ioTPrinter.println("Args error: " + e.getMessage()); + System.exit(CODE_ERROR); + } catch (Exception e) { + ioTPrinter.println("Encounter an error, because: " + e.getMessage()); + System.exit(CODE_ERROR); + } + + System.exit(importFromTargetPath()); + } + + private static void parseSpecialParams(CommandLine commandLine) throws ArgsErrorException { + + source = commandLine.getOptionValue(SOURCE_ARGS); + if (!Files.exists(Paths.get(source))) { + ioTPrinter.println("source file is not exist"); + System.exit(CODE_OK); + } + onSuccess = commandLine.getOptionValue(ON_SUCCESS_ARGS); + onFail = commandLine.getOptionValue(ON_FAIL_ARGS); + + boolean checkSuccessFileStory = false; + if (Operation.MV.name().equalsIgnoreCase(onSuccess) + || Operation.CP.name().equalsIgnoreCase(onSuccess)) { + File file = createSuccessDir(commandLine); Review Comment: Or "successDirectory"... Just not "file". -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
