• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10006

06 Sep 2023 05:15AM CUT coverage: 47.697% (+0.006%) from 47.691%
#10006

push

travis_ci

web-flow
[RatisConsensus] retry cache expiration time should be longer than retriable-client wait duration (#11045) (#11052)

13 of 13 new or added lines in 1 file covered. (100.0%)

80213 of 168172 relevant lines covered (47.7%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/AbstractCsvTool.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.tool;
21

22
import org.apache.iotdb.cli.utils.IoTPrinter;
23
import org.apache.iotdb.exception.ArgsErrorException;
24
import org.apache.iotdb.rpc.IoTDBConnectionException;
25
import org.apache.iotdb.rpc.StatementExecutionException;
26
import org.apache.iotdb.session.Session;
27

28
import org.apache.commons.cli.CommandLine;
29
import org.apache.commons.cli.Option;
30
import org.apache.commons.cli.Options;
31
import org.apache.commons.csv.CSVFormat;
32
import org.apache.commons.csv.CSVPrinter;
33
import org.apache.commons.csv.QuoteMode;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

37
import java.io.IOException;
38
import java.io.PrintWriter;
39
import java.time.ZoneId;
40
import java.util.List;
41

42
public abstract class AbstractCsvTool {
43

44
  protected static final String HOST_ARGS = "h";
45
  protected static final String HOST_NAME = "host";
46

47
  protected static final String HELP_ARGS = "help";
48

49
  protected static final String PORT_ARGS = "p";
50
  protected static final String PORT_NAME = "port";
51

52
  protected static final String PW_ARGS = "pw";
53
  protected static final String PW_NAME = "password";
54

55
  protected static final String USERNAME_ARGS = "u";
56
  protected static final String USERNAME_NAME = "username";
57

58
  protected static final String TIME_FORMAT_ARGS = "tf";
59
  protected static final String TIME_FORMAT_NAME = "timeformat";
60

61
  protected static final String TIME_ZONE_ARGS = "tz";
62
  protected static final String TIME_ZONE_NAME = "timeZone";
63

64
  protected static final String TIMEOUT_ARGS = "t";
65
  protected static final String TIMEOUT_NAME = "timeout";
66
  protected static final int MAX_HELP_CONSOLE_WIDTH = 92;
67
  protected static final String[] TIME_FORMAT =
×
68
      new String[] {"default", "long", "number", "timestamp"};
69
  protected static final String[] STRING_TIME_FORMAT =
×
70
      new String[] {
71
        "yyyy-MM-dd HH:mm:ss.SSSX",
72
        "yyyy/MM/dd HH:mm:ss.SSSX",
73
        "yyyy.MM.dd HH:mm:ss.SSSX",
74
        "yyyy-MM-dd HH:mm:ssX",
75
        "yyyy/MM/dd HH:mm:ssX",
76
        "yyyy.MM.dd HH:mm:ssX",
77
        "yyyy-MM-dd HH:mm:ss.SSSz",
78
        "yyyy/MM/dd HH:mm:ss.SSSz",
79
        "yyyy.MM.dd HH:mm:ss.SSSz",
80
        "yyyy-MM-dd HH:mm:ssz",
81
        "yyyy/MM/dd HH:mm:ssz",
82
        "yyyy.MM.dd HH:mm:ssz",
83
        "yyyy-MM-dd HH:mm:ss.SSS",
84
        "yyyy/MM/dd HH:mm:ss.SSS",
85
        "yyyy.MM.dd HH:mm:ss.SSS",
86
        "yyyy-MM-dd HH:mm:ss",
87
        "yyyy/MM/dd HH:mm:ss",
88
        "yyyy.MM.dd HH:mm:ss",
89
        "yyyy-MM-dd'T'HH:mm:ss.SSSX",
90
        "yyyy/MM/dd'T'HH:mm:ss.SSSX",
91
        "yyyy.MM.dd'T'HH:mm:ss.SSSX",
92
        "yyyy-MM-dd'T'HH:mm:ssX",
93
        "yyyy/MM/dd'T'HH:mm:ssX",
94
        "yyyy.MM.dd'T'HH:mm:ssX",
95
        "yyyy-MM-dd'T'HH:mm:ss.SSSz",
96
        "yyyy/MM/dd'T'HH:mm:ss.SSSz",
97
        "yyyy.MM.dd'T'HH:mm:ss.SSSz",
98
        "yyyy-MM-dd'T'HH:mm:ssz",
99
        "yyyy/MM/dd'T'HH:mm:ssz",
100
        "yyyy.MM.dd'T'HH:mm:ssz",
101
        "yyyy-MM-dd'T'HH:mm:ss.SSS",
102
        "yyyy/MM/dd'T'HH:mm:ss.SSS",
103
        "yyyy.MM.dd'T'HH:mm:ss.SSS",
104
        "yyyy-MM-dd'T'HH:mm:ss",
105
        "yyyy/MM/dd'T'HH:mm:ss",
106
        "yyyy.MM.dd'T'HH:mm:ss"
107
      };
108
  protected static final int CODE_OK = 0;
109
  protected static final int CODE_ERROR = 1;
110

111
  protected static String host;
112
  protected static String port;
113
  protected static String username;
114
  protected static String password;
115
  protected static ZoneId zoneId;
116

117
  protected static String timeZoneID;
118
  protected static String timeFormat;
119
  protected static Session session;
120
  private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCsvTool.class);
×
121

122
  protected AbstractCsvTool() {}
×
123

124
  protected static String checkRequiredArg(String arg, String name, CommandLine commandLine)
125
      throws ArgsErrorException {
126
    String str = commandLine.getOptionValue(arg);
×
127
    if (str == null) {
×
128
      String msg = String.format("Required values for option '%s' not provided", name);
×
129
      LOGGER.info(msg);
×
130
      LOGGER.info("Use -help for more information");
×
131
      throw new ArgsErrorException(msg);
×
132
    }
133
    return str;
×
134
  }
135

136
  protected static void setTimeZone() throws IoTDBConnectionException, StatementExecutionException {
137
    if (timeZoneID != null) {
×
138
      session.setTimeZone(timeZoneID);
×
139
    }
140
    zoneId = ZoneId.of(session.getTimeZone());
×
141
  }
×
142

143
  protected static void parseBasicParams(CommandLine commandLine) throws ArgsErrorException {
144
    host = checkRequiredArg(HOST_ARGS, HOST_NAME, commandLine);
×
145
    port = checkRequiredArg(PORT_ARGS, PORT_NAME, commandLine);
×
146
    username = checkRequiredArg(USERNAME_ARGS, USERNAME_NAME, commandLine);
×
147

148
    password = commandLine.getOptionValue(PW_ARGS);
×
149
  }
×
150

151
  protected static boolean checkTimeFormat() {
152
    for (String format : TIME_FORMAT) {
×
153
      if (timeFormat.equals(format)) {
×
154
        return true;
×
155
      }
156
    }
157
    for (String format : STRING_TIME_FORMAT) {
×
158
      if (timeFormat.equals(format)) {
×
159
        return true;
×
160
      }
161
    }
162
    LOGGER.info(
×
163
        "Input time format %s is not supported, "
164
            + "please input like yyyy-MM-dd\\ HH:mm:ss.SSS or yyyy-MM-dd'T'HH:mm:ss.SSS%n",
165
        timeFormat);
166
    return false;
×
167
  }
168

169
  protected static Options createNewOptions() {
170
    Options options = new Options();
×
171

172
    Option opHost =
×
173
        Option.builder(HOST_ARGS)
×
174
            .longOpt(HOST_NAME)
×
175
            .required()
×
176
            .argName(HOST_NAME)
×
177
            .hasArg()
×
178
            .desc("Host Name (required)")
×
179
            .build();
×
180
    options.addOption(opHost);
×
181

182
    Option opPort =
×
183
        Option.builder(PORT_ARGS)
×
184
            .longOpt(PORT_NAME)
×
185
            .required()
×
186
            .argName(PORT_NAME)
×
187
            .hasArg()
×
188
            .desc("Port (required)")
×
189
            .build();
×
190
    options.addOption(opPort);
×
191

192
    Option opUsername =
×
193
        Option.builder(USERNAME_ARGS)
×
194
            .longOpt(USERNAME_NAME)
×
195
            .required()
×
196
            .argName(USERNAME_NAME)
×
197
            .hasArg()
×
198
            .desc("Username (required)")
×
199
            .build();
×
200
    options.addOption(opUsername);
×
201

202
    Option opPassword =
×
203
        Option.builder(PW_ARGS)
×
204
            .longOpt(PW_NAME)
×
205
            .optionalArg(true)
×
206
            .argName(PW_NAME)
×
207
            .hasArg()
×
208
            .desc("Password (required)")
×
209
            .build();
×
210
    options.addOption(opPassword);
×
211
    return options;
×
212
  }
213

214
  /**
215
   * write data to CSV file.
216
   *
217
   * @param headerNames the header names of CSV file
218
   * @param records the records of CSV file
219
   * @param filePath the directory to save the file
220
   */
221
  public static Boolean writeCsvFile(
222
      List<String> headerNames, List<List<Object>> records, String filePath) {
223
    try {
224
      final CSVPrinterWrapper csvPrinterWrapper = new CSVPrinterWrapper(filePath);
×
225
      if (headerNames != null) {
×
226
        csvPrinterWrapper.printRecord(headerNames);
×
227
      }
228
      for (List<Object> CsvRecord : records) {
×
229
        csvPrinterWrapper.printRecord(CsvRecord);
×
230
      }
×
231
      csvPrinterWrapper.flush();
×
232
      csvPrinterWrapper.close();
×
233
      return true;
×
234
    } catch (IOException e) {
×
235
      IoTPrinter.printException(e);
×
236
      return false;
×
237
    }
238
  }
239

240
  static class CSVPrinterWrapper {
241
    private final String filePath;
242
    private final CSVFormat csvFormat;
243
    private CSVPrinter csvPrinter;
244

245
    public CSVPrinterWrapper(String filePath) {
×
246
      this.filePath = filePath;
×
247
      this.csvFormat =
×
248
          CSVFormat.Builder.create(CSVFormat.DEFAULT)
×
249
              .setHeader()
×
250
              .setSkipHeaderRecord(true)
×
251
              .setEscape('\\')
×
252
              .setQuoteMode(QuoteMode.NONE)
×
253
              .build();
×
254
    }
×
255

256
    public void printRecord(final Iterable<?> values) throws IOException {
257
      if (csvPrinter == null) {
×
258
        csvPrinter = csvFormat.print(new PrintWriter(filePath));
×
259
      }
260
      csvPrinter.printRecord(values);
×
261
    }
×
262

263
    public void print(Object value) {
264
      if (csvPrinter == null) {
×
265
        try {
266
          csvPrinter = csvFormat.print(new PrintWriter(filePath));
×
267
        } catch (IOException e) {
×
268
          IoTPrinter.printException(e);
×
269
          return;
×
270
        }
×
271
      }
272
      try {
273
        csvPrinter.print(value);
×
274
      } catch (IOException e) {
×
275
        IoTPrinter.printException(e);
×
276
      }
×
277
    }
×
278

279
    public void println() throws IOException {
280
      csvPrinter.println();
×
281
    }
×
282

283
    public void close() throws IOException {
284
      if (csvPrinter != null) {
×
285
        csvPrinter.close();
×
286
      }
287
    }
×
288

289
    public void flush() throws IOException {
290
      if (csvPrinter != null) {
×
291
        csvPrinter.flush();
×
292
      }
293
    }
×
294
  }
295
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc