You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ge...@apache.org on 2021/01/01 09:22:31 UTC

[iotdb] branch fix_import_csv_tool created (now f16698e)

This is an automated email from the ASF dual-hosted git repository.

geniuspig pushed a change to branch fix_import_csv_tool
in repository https://gitbox.apache.org/repos/asf/iotdb.git.


      at f16698e  Update ImportCsv.java

This branch includes the following new commits:

     new f16698e  Update ImportCsv.java

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[iotdb] 01/01: Update ImportCsv.java

Posted by ge...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

geniuspig pushed a commit to branch fix_import_csv_tool
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit f16698e2dcc4f29ccce74f614089ef2cf8053258
Author: Boris <96...@qq.com>
AuthorDate: Fri Jan 1 17:21:55 2021 +0800

    Update ImportCsv.java
---
 .../main/java/org/apache/iotdb/tool/ImportCsv.java | 58 +++++++++++++++-------
 1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java b/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
index bd4a55f..6001412 100644
--- a/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
+++ b/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
@@ -18,32 +18,23 @@
  */
 package org.apache.iotdb.tool;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.LineNumberReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 import jline.console.ConsoleReader;
 import me.tongfei.progressbar.ProgressBar;
-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.Options;
-import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.*;
 import org.apache.iotdb.exception.ArgsErrorException;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.StatementExecutionException;
 import org.apache.iotdb.session.Session;
 import org.apache.iotdb.tsfile.common.constant.TsFileConstant;
 
+import java.io.*;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
 /**
  * read a CSV formatted data File and insert all the data into IoTDB.
  */
@@ -129,7 +120,7 @@ public class ImportCsv extends AbstractCsvTool {
           String device = deviceToPositions.getKey();
           devices.add(device);
 
-          times.add(Long.parseLong(cols[0]));
+          times.add(parseTime(cols[0]));
 
           List<String> values = new ArrayList<>();
           for (int position : deviceToPositions.getValue()) {
@@ -213,6 +204,35 @@ public class ImportCsv extends AbstractCsvTool {
     }
   }
 
+  private static long parseTime(String str) {
+
+    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+    SimpleDateFormat format2 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
+    SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+
+    try {
+      return Long.parseLong(str);
+    } catch (Exception ignored) {
+    }
+
+    try {
+      return format1.parse(str).getTime();
+    } catch (java.text.ParseException ignored) {
+    }
+
+    try {
+      return format2.parse(str).getTime();
+    } catch (java.text.ParseException ignored) {
+    }
+
+    try {
+      return format3.parse(str).getTime();
+    } catch (java.text.ParseException ignored) {
+    }
+
+    throw new IllegalArgumentException("Input time format " + str + "error. Input like yyyy-MM-dd HH:mm:ss, yyyy-MM-ddTHH:mm:ss or yyyy-MM-ddTHH:mm:ss.SSSZ");
+  }
+
   private static void parseSpecialParams(CommandLine commandLine) {
     timeZoneID = commandLine.getOptionValue(TIME_ZONE_ARGS);
   }