CritasWang commented on code in PR #16252:
URL: https://github.com/apache/iotdb/pull/16252#discussion_r2302682795


##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTree.java:
##########
@@ -169,33 +187,31 @@ private void exportToSqlFile(SessionDataSet 
sessionDataSet, String filePath)
             timeseries.addAll(headers);
             timeseries.remove(0);
           }
+          long timestamp = iterator.getLong(1);
           String sqlMiddle =
               Boolean.TRUE.equals(aligned)
-                  ? " ALIGNED VALUES (" + rowRecord.getTimestamp() + ","
-                  : " VALUES (" + rowRecord.getTimestamp() + ",";
+                  ? " ALIGNED VALUES (" + timestamp + ","
+                  : " VALUES (" + timestamp + ",";
           List<String> values = new ArrayList<>();
-          if (headers.contains("Device")) {
-            fields.remove(0);
-          }
-          for (int index = 0; index < fields.size(); index++) {
-            RowRecord next =
-                session
-                    .executeQueryStatement("SHOW TIMESERIES " + 
timeseries.get(index), timeout)
-                    .next();
-            if (ObjectUtils.isNotEmpty(next)) {
-              List<Field> timeseriesList = next.getFields();
-              String value = fields.get(index).toString();
+          int startIndex = headers.contains("Device") ? 2 : 1;
+          for (int index = startIndex; index < totalColumns; index++) {
+            SessionDataSet sessionDataSet2 =
+                session.executeQueryStatement(
+                    "SHOW TIMESERIES " + timeseries.get(index - startIndex), 
timeout);
+            SessionDataSet.DataIterator iterator2 = sessionDataSet2.iterator();
+            if (iterator2.next()) {
+              String value = iterator.getString(index + 1);
               if (value.equals("null")) {

Review Comment:
   ```suggestion
                 if (StringUtils.isEmpty(value)) {
   ```



##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTree.java:
##########
@@ -264,47 +278,52 @@ private static Boolean exportToTsFile(SessionDataSet 
sessionDataSet, String file
 
   private void exportToCsvFile(SessionDataSet sessionDataSet, String filePath)
       throws IOException, IoTDBConnectionException, 
StatementExecutionException {
+    processedRows = 0;
+    lastPrintTime = 0;
     List<String> headers = sessionDataSet.getColumnNames();
     int fileIndex = 0;
-    boolean hasNext = sessionDataSet.hasNext();
-    while (hasNext) {
+    SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
+    List<String> columnTypeList = iterator.getColumnTypeList();
+    int totalColumns = columnTypeList.size();
+    boolean fromOuterloop = false;
+    while (iterator.next()) {
       final String finalFilePath = filePath + "_" + fileIndex + ".csv";
       CSVPrinterWrapper csvPrinterWrapper = new 
CSVPrinterWrapper(finalFilePath);
-      csvPrinterWrapper.printRecord(headers);
-      int i = 0;
-      while (i++ < linesPerFile && hasNext) {
-        RowRecord rowRecord = sessionDataSet.next();
-        if (rowRecord.getTimestamp() != 0) {
-          csvPrinterWrapper.print(timeTrans(rowRecord.getTimestamp()));
-        }
-        rowRecord
-            .getFields()
-            .forEach(
-                field -> {
-                  String fieldStringValue = field.getStringValue();
-                  if (!"null".equals(field.getStringValue())) {
-                    if ((field.getDataType() == TSDataType.TEXT
-                            || field.getDataType() == TSDataType.STRING)
-                        && !fieldStringValue.startsWith("root.")) {
-                      fieldStringValue = "\"" + fieldStringValue + "\"";
-                    }
-                    csvPrinterWrapper.print(fieldStringValue);
-                  } else {
-                    csvPrinterWrapper.print("");
-                  }
-                });
-        csvPrinterWrapper.println();
-        hasNext = sessionDataSet.hasNext();
-        if (!hasNext) {
-          break;
+      try {
+        csvPrinterWrapper.printRecord(headers);
+        fromOuterloop = true;
+        int i = 0;
+        while (i++ < linesPerFile && (fromOuterloop || iterator.next())) {
+          fromOuterloop = false;
+          csvPrinterWrapper.print(timeTrans(iterator.getLong(1)));
+          for (int curColumnIndex = 1; curColumnIndex < totalColumns; 
curColumnIndex++) {
+            String columnValue = iterator.getString(curColumnIndex + 1);
+            if (columnValue.equals("null")) {

Review Comment:
   ```suggestion
               if (StringUtils.isEmpty(columnValue)) {
   ```



##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTable.java:
##########
@@ -233,44 +248,54 @@ private Boolean exportToTsFile(SessionDataSet 
sessionDataSet, String filePath, S
 
   private void exportToCsvFile(SessionDataSet sessionDataSet, String filePath)
       throws IOException, IoTDBConnectionException, 
StatementExecutionException {
+    processedRows = 0;
+    lastPrintTime = 0;
     List<String> headers = sessionDataSet.getColumnNames();
     int fileIndex = 0;
-    boolean hasNext = sessionDataSet.hasNext();
-    while (hasNext) {
+    SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
+    List<String> columnTypeList = iterator.getColumnTypeList();
+    int totalColumns = columnTypeList.size();
+    boolean fromOuterloop = false;
+    while (iterator.next()) {
       final String finalFilePath = filePath + "_" + fileIndex + ".csv";
       final CSVPrinterWrapper csvPrinterWrapper = new 
CSVPrinterWrapper(finalFilePath);
-      csvPrinterWrapper.printRecord(headers);
-      int i = 0;
-      while (i++ < linesPerFile) {
-        RowRecord rowRecord = sessionDataSet.next();
-        rowRecord
-            .getFields()
-            .forEach(
-                field -> {
-                  String fieldStringValue = field.getStringValue();
-                  if (!"null".equals(field.getStringValue())) {
-                    if ((field.getDataType() == TSDataType.TEXT
-                        || field.getDataType() == TSDataType.STRING)) {
-                      fieldStringValue = "\"" + fieldStringValue + "\"";
-                    } else if (field.getDataType() == TSDataType.TIMESTAMP) {
-                      fieldStringValue = timeTrans(field.getLongV());
-                    }
-                    csvPrinterWrapper.print(fieldStringValue);
-                  } else {
-                    csvPrinterWrapper.print("");
-                  }
-                });
-        csvPrinterWrapper.println();
-        // 检查下一行是否存在
-        hasNext = sessionDataSet.hasNext();
-        if (!hasNext) {
-          break;
+      try {
+        csvPrinterWrapper.printRecord(headers);
+        fromOuterloop = true;
+        int countLine = 0;
+        while (countLine++ < linesPerFile && (fromOuterloop || 
iterator.next())) {
+          fromOuterloop = false;
+          for (int curColumnIndex = 0; curColumnIndex < totalColumns; 
curColumnIndex++) {
+            String curType = columnTypeList.get(curColumnIndex);
+            if (curType.equalsIgnoreCase("TIMESTAMP")) {
+              
csvPrinterWrapper.print(timeTrans(iterator.getLong(curColumnIndex + 1)));
+            } else {
+              String columnValue = iterator.getString(curColumnIndex + 1);
+              if (columnValue.equals("null")) {

Review Comment:
   ```suggestion
                 if (StringUtils.isEmpty(columnValue)) {
   ```



-- 
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]

Reply via email to