示例#1
0
  /**
   * 指定したパスに配置されているCSVファイルを読み込み、Agentデータを生成する。
   *
   * @param parent CSVファイルが配置されているディレクトリ。
   * @param filename CSVファイル名。
   * @return 読み込みに成功した結果、生成されたAgentオブジェクト。
   * @throws IOException 読み込みに失敗した場合。
   */
  public Agent load(String parent, String filename) throws IOException {
    Agent agent = null;

    // ディレクトリとファイル名からFileオブジェクトを生成する
    File csvFile = new File(parent, filename);

    // CSVファイルリーダー
    ICsvBeanReader reader = null;
    try {
      // Excelの読み込み規則を使用してCSVファイルを読み込む
      reader = new CsvBeanReader(new FileReader(csvFile), CsvPreference.EXCEL_PREFERENCE);

      // ヘッダを取得する
      final String[] headers = reader.getHeader(true);

      // Agentを生成し、1行1個のVarbindを生成し格納する
      agent = new Agent();
      while (true) {
        SnmpVarbind varbind = reader.read(SnmpVarbind.class, headers, PROCESSORS);
        if (varbind == null) {
          break;
        }

        agent.addVarbind(varbind);
      }
    } catch (SuperCsvException exception) {
      IOException ioex = new IOException(exception.getLocalizedMessage());
      throw ioex;
    } finally {
      // 開かれているReaderはクローズする
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException ioex) {
        }
      }
    }

    return agent;
  }
示例#2
0
  @Override
  public void run() {
    List<String> lineList;

    // if header option is true, check whether csv first line is valid
    if (m_config.header) {
      if (!checkHeader()) {
        m_log.error(
            "In the CSV file "
                + m_config.file
                + ", the header "
                + m_listReader.getUntokenizedRow()
                + " does not match "
                + "an existing column in the table "
                + m_config.table
                + ".");
        System.exit(-1);
      }
    }

    while ((m_config.limitrows-- > 0)) {
      if (m_errHandler.hasReachedErrorLimit()) {
        break;
      }

      try {
        // Initial setting of m_totalLineCount
        if (m_listReader.getLineNumber() == 0) {
          m_totalLineCount.set(m_config.skip);
        } else {
          m_totalLineCount.set(m_listReader.getLineNumber());
        }
        long st = System.nanoTime();
        lineList = m_listReader.read();
        long end = System.nanoTime();
        m_parsingTime += (end - st);
        if (lineList == null) {
          if (m_totalLineCount.get() > m_listReader.getLineNumber()) {
            m_totalLineCount.set(m_listReader.getLineNumber());
          }
          break;
        }
        m_totalRowCount.incrementAndGet();

        if (lineList.isEmpty()) {
          continue;
        }

        String[] lineValues = lineList.toArray(new String[0]);
        String lineCheckResult;
        String[] reorderValues = new String[m_columnCount];
        if ((lineCheckResult = checkparams_trimspace_reorder(lineValues, reorderValues)) != null) {
          final RowWithMetaData metaData =
              new RowWithMetaData(m_listReader.getUntokenizedRow(), m_totalLineCount.get() + 1);
          if (m_errHandler.handleError(metaData, null, lineCheckResult)) {
            break;
          }
          continue;
        }

        RowWithMetaData lineData =
            new RowWithMetaData(m_listReader.getUntokenizedRow(), m_listReader.getLineNumber());
        m_loader.insertRow(lineData, reorderValues);
      } catch (SuperCsvException e) {
        // Catch rows that can not be read by superCSV m_listReader.
        // e.g. items without quotes when strictquotes is enabled.
        final RowWithMetaData metaData =
            new RowWithMetaData(m_listReader.getUntokenizedRow(), m_totalLineCount.get() + 1);
        if (m_errHandler.handleError(metaData, null, e.getMessage())) {
          break;
        }
      } catch (IOException ex) {
        m_log.error("Failed to read CSV line from file: " + ex);
        break;
      } catch (InterruptedException e) {
        m_log.error("CSVLoader interrupted: " + e);
        break;
      }
    }

    // Now wait for processors to see endOfData and count down. After that drain to finish all
    // callbacks
    try {
      m_log.debug("Waiting for CSVDataLoader to finish.");
      m_loader.close();
      m_log.debug("CSVDataLoader Done.");
    } catch (Exception ex) {
      m_log.warn(
          "Stopped processing because of connection error. "
              + "A report will be generated with what we processed so far. Error: "
              + ex);
    }
  }