You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2019/02/22 12:07:05 UTC
[incubator-iotdb] branch fix_sonar_bug updated: refactor ExportCsv
This is an automated email from the ASF dual-hosted git repository.
jiangtian pushed a commit to branch fix_sonar_bug
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git
The following commit(s) were added to refs/heads/fix_sonar_bug by this push:
new 19c84dd refactor ExportCsv
19c84dd is described below
commit 19c84dd4c9fe42c442ad76fbe081263b19e438ba
Author: 江天 <jt...@163.com>
AuthorDate: Fri Feb 22 20:06:10 2019 +0800
refactor ExportCsv
---
.../org/apache/iotdb/cli/tool/CsvTestDataGen.java | 125 ----------------
.../java/org/apache/iotdb/cli/tool/ExportCsv.java | 159 +++++++++++----------
2 files changed, 81 insertions(+), 203 deletions(-)
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/CsvTestDataGen.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/CsvTestDataGen.java
deleted file mode 100644
index b8b497c..0000000
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/CsvTestDataGen.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.iotdb.cli.tool;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-
-@Deprecated
-public class CsvTestDataGen {
-
- private CsvTestDataGen() {
-
- }
-
- private static final String PATHS = "Time,root.fit.p.s1,root.fit.d1.s1,root.fit.d1.s2,root.fit.d2."
- + "s1,root.fit.d2.s3";
- private static String[] iso = {
- PATHS,
- "1970-01-01T08:00:00.001+08:00,,1,pass,1,1", "1970-01-01T08:00:00.002+08:00,,2,pass,,",
- "1970-01-01T08:00:00.003+08:00,,3,pass,,", "1970-01-01T08:00:00.004+08:00,4,,,4,4"};
- private static String[] defaultLong = {
- PATHS,
- "1,,1,pass,1,1",
- "2,,2,pass,,", "1970-01-01T08:00:00.003+08:00,,3,pass,,", "3,4,,,4,4"};
- private static String[] userSelfDefine = {
- PATHS,
- "1971,,1,pass,1,1",
- "1972,,2,pass,,", "1973-01-01T08:00:00.003+08:00,,3,pass,,", "1974,4,,,4,4"};
- private static FileOutputStream fos = null;
- private static OutputStreamWriter osw = null;
- private static BufferedWriter bw = null;
- private static final String USER_DIR = "user.dir";
-
- /**
- * generate iso.csv data.
- *
- * @return path
- */
- public static String isoDataGen() {
- String path = System.getProperties().getProperty(USER_DIR) + "/src/test/resources/iso.csv";
- File file = new File(path);
- writeDataFrom(file, iso);
- return path;
- }
-
- /**
- * generate default long data file: defaultLong.csv .
- *
- * @return path
- */
- public static String defaultLongDataGen() {
- String path =
- System.getProperties().getProperty(USER_DIR) + "/src/test/resources/defaultLong.csv";
- File file = new File(path);
- writeDataFrom(file, defaultLong);
- return path;
- }
-
- /**
- * generate user defined data: userSelfDefine.csv .
- *
- * @return path
- */
- public static String userSelfDataGen() {
- String path =
- System.getProperties().getProperty(USER_DIR) + "/src/test/resources/userSelfDefine.csv";
- File file = new File(path);
- writeDataFrom(file, userSelfDefine);
- return path;
- }
-
- private static void writeDataFrom(File file, String[] info) {
- try {
- if (!file.exists()) {
- file.createNewFile();
- }
- fos = new FileOutputStream(file);
- osw = new OutputStreamWriter(fos);
- bw = new BufferedWriter(osw);
- for (String str : info) {
- bw.write(str + "\n");
- }
- bw.flush();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- bw.close();
- osw.close();
- fos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- public static void main(String[] args) {
- System.out.println(defaultLongDataGen());
- }
-
-}
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/ExportCsv.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/ExportCsv.java
index 427aa8a..cae4527 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/ExportCsv.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/tool/ExportCsv.java
@@ -121,23 +121,22 @@ public class ExportCsv extends AbstractCsvTool {
for (int i = 0; i < values.length; i++) {
dumpResult(values[i], i);
}
- return;
} else {
dumpFromSqlFile(sqlFile);
}
} catch (ClassNotFoundException e) {
LOGGER.error(
"Failed to dump data because cannot find TsFile JDBC Driver, "
- + "please check whether you have imported driver or not");
+ + "please check whether you have imported driver or not", e);
} catch (SQLException e) {
- LOGGER.error("Encounter an error when dumping data, error is {}", e.getMessage());
+ LOGGER.error("Encounter an error when dumping data, error is ", e);
} catch (IOException e) {
- LOGGER.error("Failed to operate on file, because {}", e.getMessage());
+ LOGGER.error("Failed to operate on file, because ", e);
} catch (TException e) {
- LOGGER.error("Encounter an error when connecting to server, because {}",
- e.getMessage());
+ LOGGER.error("Encounter an error when connecting to server, because ",
+ e);
} catch (ArgsErrorException e) {
- e.printStackTrace();
+ LOGGER.error("Invalid args.", e);
} finally {
reader.close();
if (connection != null) {
@@ -222,7 +221,7 @@ public class ExportCsv extends AbstractCsvTool {
try {
dumpResult(sql, index);
} catch (SQLException e) {
- LOGGER.error("Cannot dump data for statment {}, because {}", sql, e.getMessage());
+ LOGGER.error("Cannot dump data for statement {}, because ", sql, e);
}
index++;
}
@@ -238,95 +237,99 @@ public class ExportCsv extends AbstractCsvTool {
*/
private static void dumpResult(String sql, int index)
throws SQLException {
- FileWriter fw = null;
- BufferedWriter bw = null;
+
final String path = targetDirectory + DUMP_FILE_NAME + index + ".csv";
+ File tf = new File(path);
try {
- File tf = new File(path);
if (!tf.exists() && !tf.createNewFile()) {
LOGGER.error("Could not create target file for sql statement: {}", sql);
return;
}
- fw = new FileWriter(tf);
- bw = new BufferedWriter(fw);
} catch (IOException e) {
LOGGER.error(e.getMessage());
return;
}
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery(sql);
- ResultSetMetaData metadata = rs.getMetaData();
- long startTime = System.currentTimeMillis();
- try {
+ try (Statement statement = connection.createStatement();
+ ResultSet rs = statement.executeQuery(sql);
+ BufferedWriter bw = new BufferedWriter(new FileWriter(tf))) {
+ ResultSetMetaData metadata = rs.getMetaData();
+ long startTime = System.currentTimeMillis();
+
int count = metadata.getColumnCount();
// write data in csv file
- for (int i = 1; i <= count; i++) {
- if (i < count) {
- bw.write(metadata.getColumnLabel(i) + ",");
- } else {
- bw.write(metadata.getColumnLabel(i) + "\n");
- }
+ writeMetadata(bw, count, metadata);
+
+ writeResultSet(rs, bw, count);
+ LOGGER.info("Statement [{}] has dumped to file {} successfully! It costs {}ms.",
+ sql, path, System.currentTimeMillis() - startTime);
+ } catch (IOException e) {
+ LOGGER.error("Cannot dump result because", e);
+ }
+ }
+
+ private static void writeMetadata(BufferedWriter bw, int count, ResultSetMetaData metadata)
+ throws SQLException, IOException {
+ for (int i = 1; i <= count; i++) {
+ if (i < count) {
+ bw.write(metadata.getColumnLabel(i) + ",");
+ } else {
+ bw.write(metadata.getColumnLabel(i) + "\n");
+ }
+ }
+ }
+
+ private static void writeResultSet(ResultSet rs, BufferedWriter bw, int count)
+ throws SQLException, IOException {
+ while (rs.next()) {
+ if (rs.getString(1) == null ||
+ "null".equalsIgnoreCase(rs.getString(1))) {
+ bw.write(",");
+ } else {
+ writeTime(rs, bw);
+ writeValue(rs, count, bw);
}
- while (rs.next()) {
- if (rs.getString(1) == null || "null".equalsIgnoreCase(rs.getString(1))) {
+ }
+ }
+
+ private static void writeTime(ResultSet rs, BufferedWriter bw) throws SQLException, IOException {
+ ZonedDateTime dateTime;
+ switch (timeFormat) {
+ case DEFAULT_TIME_FORMAT:
+ case "default":
+ dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(rs.getLong(1)),
+ zoneId);
+ bw.write(dateTime.toString() + ",");
+ break;
+ case "timestamp":
+ case "long":
+ case "nubmer":
+ bw.write(rs.getLong(1) + ",");
+ break;
+ default:
+ dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(rs.getLong(1)),
+ zoneId);
+ bw.write(dateTime.format(DateTimeFormatter.ofPattern(timeFormat)) + ",");
+ break;
+ }
+ }
+
+ private static void writeValue(ResultSet rs, int count, BufferedWriter bw)
+ throws SQLException, IOException {
+ for (int j = 2; j <= count; j++) {
+ if (j < count) {
+ if ("null".equals(rs.getString(j))) {
bw.write(",");
} else {
- ZonedDateTime dateTime;
- switch (timeFormat) {
- case DEFAULT_TIME_FORMAT:
- case "default":
- dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(rs.getLong(1)), zoneId);
- bw.write(dateTime.toString() + ",");
- break;
- case "timestamp":
- case "long":
- case "nubmer":
- bw.write(rs.getLong(1) + ",");
- break;
- default:
- dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(rs.getLong(1)), zoneId);
- bw.write(dateTime.format(DateTimeFormatter.ofPattern(timeFormat)) + ",");
- break;
- }
-
- for (int j = 2; j <= count; j++) {
- if (j < count) {
- if ("null".equals(rs.getString(j))) {
- bw.write(",");
- } else {
- bw.write(rs.getString(j) + ",");
- }
- } else {
- if ("null".equals(rs.getString(j))) {
- bw.write("\n");
- } else {
- bw.write(rs.getString(j) + "\n");
- }
- }
- }
+ bw.write(rs.getString(j) + ",");
}
- }
- LOGGER.info("Statement [{}] has dumped to file {} successfully! It costs {}ms.",
- sql, path, System.currentTimeMillis() - startTime);
- } catch (IOException e) {
- LOGGER.error(e.getMessage());
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (bw != null) {
- bw.close();
- }
- if (fw != null) {
- fw.close();
+ } else {
+ if ("null".equals(rs.getString(j))) {
+ bw.write("\n");
+ } else {
+ bw.write(rs.getString(j) + "\n");
}
- } catch (IOException e) {
- LOGGER.error(e.getMessage());
}
- statement.close();
}
}
-
}