in backend/src/main/java/org/apache/iotdb/admin/tool/ExportCsv.java [92:126]
private String dumpResult(String sql, Session session, ZoneId zoneId) throws BaseException {
final String fileName = TARGET_FILE + System.currentTimeMillis() + ".csv";
final String path = fileStorageLocation.resolve(fileName).toString();
File tf = new File(path);
try {
if (!tf.exists() && !tf.createNewFile()) {
throw new BaseException(ErrorCode.CREATE_FILE_FAIL, ErrorCode.CREATE_FILE_FAIL_MSG);
}
} catch (IOException | BaseException e) {
throw new BaseException(ErrorCode.CREATE_FILE_FAIL, ErrorCode.CREATE_FILE_FAIL_MSG);
}
log.info("Start exporting data from SQL statement:" + sql);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(tf))) {
SessionDataSet sessionDataSet = session.executeQueryStatement(sql);
long startTime = System.currentTimeMillis();
// write data in csv file
List<String> columnNames = sessionDataSet.getColumnNames();
writeMetadata(bw, columnNames);
boolean isDataType = false;
if ("Time".equals(columnNames.get(0))) {
isDataType = true;
}
int line = writeResultSet(sessionDataSet, bw, zoneId, isDataType);
String runTime = System.currentTimeMillis() - startTime + "ms";
log.info("Export " + line + " line data from SQL:" + sql + ". Time consuming:" + runTime);
return fileName;
} catch (IoTDBConnectionException e) {
throw new BaseException(ErrorCode.GET_SESSION_FAIL, ErrorCode.GET_SESSION_FAIL_MSG);
} catch (IOException e) {
throw new BaseException(ErrorCode.FILE_IO_FAIL, ErrorCode.FILE_IO_FAIL_MSG + e.getMessage());
} catch (StatementExecutionException e) {
throw new BaseException(
ErrorCode.EXPORT_CSV_FAIL, ErrorCode.EXPORT_CSV_FAIL_MSG + e.getMessage());
}
}