in backend/src/main/java/org/apache/iotdb/admin/service/impl/IotDBServiceImpl.java [2256:2339]
public void randomImport(
Connection connection, String deviceName, RandomImportDTO randomImportDTO)
throws BaseException {
SessionPool sessionPool = null;
int totalLine = randomImportDTO.getTotalLine();
int stepSize = randomImportDTO.getStepSize();
Long startTime = randomImportDTO.getStartTime().getTime();
List<Long> times = new ArrayList<>(totalLine);
List<List<String>> measurementsList = new ArrayList<>(totalLine);
List<List<Object>> valuesList = new ArrayList<>(totalLine);
List<List<TSDataType>> typesList = new ArrayList<>(totalLine);
try {
sessionPool = getSessionPool(connection);
SessionDataSetWrapper sessionDataSetWrapper =
sessionPool.executeQueryStatement("show timeseries " + deviceName);
List<String> columnNames = sessionDataSetWrapper.getColumnNames();
sessionDataSetWrapper.getColumnTypes();
int timeseriesIndex = -1;
int dataTypeIndex = -1;
if (columnNames != null) {
for (int i = 0; i < columnNames.size(); i++) {
if ("timeseries".equalsIgnoreCase(columnNames.get(i))) {
timeseriesIndex = i;
}
if ("dataType".equalsIgnoreCase(columnNames.get(i))) {
dataTypeIndex = i;
}
}
}
if (timeseriesIndex == -1 || dataTypeIndex == -1) {
logger.error(ErrorCode.RANDOM_IMPORT_DATA_FAIL_MSG);
throw new BaseException(
ErrorCode.RANDOM_IMPORT_DATA_FAIL, ErrorCode.RANDOM_IMPORT_DATA_FAIL_MSG);
}
List<String> measurements = new ArrayList<>();
List<String> typesStr = new ArrayList<>();
while (sessionDataSetWrapper.hasNext()) {
RowRecord next = sessionDataSetWrapper.next();
String timeseries = next.getFields().get(timeseriesIndex).toString();
timeseries = StringUtils.removeStart(timeseries, deviceName + ".");
if (timeseries.contains(".")) {
continue;
}
measurements.add(timeseries);
String dataType = next.getFields().get(dataTypeIndex).toString();
typesStr.add(dataType);
}
if (measurements.size() == 0) {
logger.error(ErrorCode.NO_MEASUREMENT_MSG);
throw new BaseException(ErrorCode.NO_MEASUREMENT, ErrorCode.NO_MEASUREMENT_MSG);
}
List<TSDataType> types = handleTypeStr(typesStr);
List<String> devices = new ArrayList<>(totalLine);
for (int i = 0; i < totalLine; i++) {
typesList.add(types);
measurementsList.add(measurements);
devices.add(deviceName);
List<Object> values = createRandomData(typesStr);
valuesList.add(values);
times.add(stepSize * i + startTime);
}
sessionPool.insertRecords(devices, times, measurementsList, typesList, valuesList);
} catch (IoTDBConnectionException e) {
logger.error(e.getMessage());
throw new BaseException(ErrorCode.GET_SESSION_FAIL, ErrorCode.GET_SESSION_FAIL_MSG);
} catch (StatementExecutionException e) {
logger.error(e.getMessage());
System.out.println(e.getStatusCode());
if (e.getMessage().contains("No permissions")) {
throw new BaseException(ErrorCode.NO_PRI_INSERT_DATA, ErrorCode.NO_PRI_INSERT_DATA_MSG);
}
throw new BaseException(
ErrorCode.RANDOM_IMPORT_DATA_FAIL, ErrorCode.RANDOM_IMPORT_DATA_FAIL_MSG);
} finally {
closeSessionPool(sessionPool);
}
}