in iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportData.java [62:260]
public static void main(String[] args) {
OptionsUtil.setIsImport(false);
Options helpOptions = OptionsUtil.createHelpOptions();
Options tsFileOptions = OptionsUtil.createExportTsFileOptions();
Options csvOptions = OptionsUtil.createExportCsvOptions();
Options sqlOptions = OptionsUtil.createExportSqlOptions();
HelpFormatter hf = new HelpFormatter();
CommandLine commandLine = null;
CommandLineParser parser = new DefaultParser();
hf.setOptionComparator(null); // avoid reordering
hf.setWidth(Constants.MAX_HELP_CONSOLE_WIDTH);
if (args == null || args.length == 0) {
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
System.exit(Constants.CODE_ERROR);
}
try {
commandLine = parser.parse(helpOptions, args, true);
} catch (ParseException e) {
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
System.exit(Constants.CODE_ERROR);
}
final List<String> argList = Arrays.asList(args);
int helpIndex = argList.indexOf(Constants.MINUS + Constants.HELP_ARGS);
int sql_dialect = argList.indexOf(Constants.MINUS + Constants.SQL_DIALECT_ARGS); // -sql_dialect
if (sql_dialect >= 0
&& !Constants.SQL_DIALECT_VALUE_TREE.equalsIgnoreCase(argList.get(sql_dialect + 1))) {
final String sqlDialectValue = argList.get(sql_dialect + 1);
if (Constants.SQL_DIALECT_VALUE_TABLE.equalsIgnoreCase(sqlDialectValue)) {
sqlDialectTree = false;
csvOptions = OptionsUtil.createTableExportCsvOptions();
tsFileOptions = OptionsUtil.createTableExportTsFileOptions();
sqlOptions = OptionsUtil.createTableExportSqlOptions();
} else {
ioTPrinter.println(String.format("sql_dialect %s is not support", sqlDialectValue));
printHelpOptions(
Constants.IMPORT_CLI_HEAD,
Constants.IMPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
System.exit(Constants.CODE_ERROR);
}
}
int ftIndex = argList.indexOf(Constants.MINUS + Constants.FILE_TYPE_ARGS);
if (ftIndex < 0) {
ftIndex = argList.indexOf(Constants.MINUS + Constants.FILE_TYPE_NAME);
}
if (helpIndex >= 0) {
fileType = argList.get(helpIndex + 1);
if (StringUtils.isNotBlank(fileType)) {
if (Constants.TSFILE_SUFFIXS.equalsIgnoreCase(fileType)) {
printHelpOptions(null, Constants.EXPORT_CLI_PREFIX, hf, tsFileOptions, null, null, false);
} else if (Constants.CSV_SUFFIXS.equalsIgnoreCase(fileType)) {
printHelpOptions(null, Constants.EXPORT_CLI_PREFIX, hf, null, csvOptions, null, false);
} else if (Constants.SQL_SUFFIXS.equalsIgnoreCase(fileType)) {
printHelpOptions(null, Constants.EXPORT_CLI_PREFIX, hf, null, null, sqlOptions, false);
} else {
ioTPrinter.println(String.format("File type %s is not support", fileType));
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
}
} else {
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
}
System.exit(Constants.CODE_ERROR);
} else if (ftIndex >= 0) {
fileType = argList.get(ftIndex + 1);
if (StringUtils.isNotBlank(fileType)) {
if (Constants.TSFILE_SUFFIXS.equalsIgnoreCase(fileType)) {
try {
commandLine = parser.parse(tsFileOptions, args, true);
} catch (ParseException e) {
ioTPrinter.println("Parse error: " + e.getMessage());
printHelpOptions(
null, Constants.EXPORT_CLI_PREFIX, hf, tsFileOptions, null, null, false);
System.exit(Constants.CODE_ERROR);
}
} else if (Constants.CSV_SUFFIXS.equalsIgnoreCase(fileType)) {
try {
commandLine = parser.parse(csvOptions, args, true);
} catch (ParseException e) {
ioTPrinter.println("Parse error: " + e.getMessage());
printHelpOptions(null, Constants.EXPORT_CLI_PREFIX, hf, null, csvOptions, null, false);
System.exit(Constants.CODE_ERROR);
}
} else if (Constants.SQL_SUFFIXS.equalsIgnoreCase(fileType)) {
try {
commandLine = parser.parse(sqlOptions, args, true);
} catch (ParseException e) {
ioTPrinter.println("Parse error: " + e.getMessage());
printHelpOptions(null, Constants.EXPORT_CLI_PREFIX, hf, null, null, sqlOptions, false);
System.exit(Constants.CODE_ERROR);
}
} else {
ioTPrinter.println(String.format("File type %s is not support", fileType));
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
System.exit(Constants.CODE_ERROR);
}
} else {
printHelpOptions(
Constants.EXPORT_CLI_HEAD,
Constants.EXPORT_CLI_PREFIX,
hf,
tsFileOptions,
csvOptions,
sqlOptions,
true);
System.exit(Constants.CODE_ERROR);
}
} else {
ioTPrinter.println(
String.format(
"Invalid args: Required values for option '%s' not provided",
Constants.FILE_TYPE_NAME));
System.exit(Constants.CODE_ERROR);
}
int exitCode = Constants.CODE_OK;
try {
parseBasicParams(commandLine);
parseSpecialParams(commandLine);
if (!checkTimeFormat()) {
System.exit(Constants.CODE_ERROR);
}
AbstractExportData exportData;
exportData = new ExportDataTree();
exportData.init();
if (!sqlDialectTree) {
exportData = new ExportDataTable();
exportData.init();
}
if (sqlDialectTree && queryCommand == null) {
LineReader lineReader =
JlineUtils.getLineReader(
new CliContext(System.in, System.out, System.err, ExitType.EXCEPTION),
username,
host,
port);
String sql = lineReader.readLine(Constants.EXPORT_CLI_PREFIX + "> please input query: ");
ioTPrinter.println(sql);
String[] values = sql.trim().split(";");
for (int i = 0; i < values.length; i++) {
exportData.exportBySql(values[i], i);
}
} else {
exportData.exportBySql(queryCommand, 0);
}
} catch (IOException e) {
ioTPrinter.println("Failed to operate on file, because " + e.getMessage());
exitCode = Constants.CODE_ERROR;
} catch (ArgsErrorException e) {
ioTPrinter.println("Invalid args: " + e.getMessage());
exitCode = Constants.CODE_ERROR;
} catch (IoTDBConnectionException | StatementExecutionException e) {
ioTPrinter.println("Connect failed because " + e.getMessage());
exitCode = Constants.CODE_ERROR;
} catch (TException e) {
ioTPrinter.println(
"Can not get the timestamp precision from server because " + e.getMessage());
exitCode = Constants.CODE_ERROR;
}
System.exit(exitCode);
}