コード例 #1
0
  /** load the CSV input */
  @Override
  public void load(
      SolrQueryRequest req,
      SolrQueryResponse rsp,
      ContentStream stream,
      UpdateRequestProcessor processor)
      throws IOException {
    errHeader = "CSVLoader: input=" + stream.getSourceInfo();
    Reader reader = null;
    try {
      reader = stream.getReader();
      if (skipLines > 0) {
        if (!(reader instanceof BufferedReader)) {
          reader = new BufferedReader(reader);
        }
        BufferedReader r = (BufferedReader) reader;
        for (int i = 0; i < skipLines; i++) {
          r.readLine();
        }
      }

      CSVParser parser = new CSVParser(reader, strategy);

      // parse the fieldnames from the header of the file
      if (fieldnames == null) {
        fieldnames = parser.getLine();
        if (fieldnames == null) {
          throw new SolrException(
              SolrException.ErrorCode.BAD_REQUEST, "Expected fieldnames in CSV input");
        }
        prepareFields();
      }

      // read the rest of the CSV file
      for (; ; ) {
        int line = parser.getLineNumber(); // for error reporting in MT mode
        String[] vals = null;
        try {
          vals = parser.getLine();
        } catch (IOException e) {
          // Catch the exception and rethrow it with more line information
          input_err("can't read line: " + line, null, line, e);
        }
        if (vals == null) break;

        if (vals.length != fieldnames.length) {
          input_err("expected " + fieldnames.length + " values but got " + vals.length, vals, line);
        }

        addDoc(line, vals);
      }
    } finally {
      if (reader != null) {
        IOUtils.closeQuietly(reader);
      }
    }
  }
コード例 #2
0
 @Override
 void add(SolrInputDocument doc, int line, int column, String val) {
   CSVParser parser = new CSVParser(new StringReader(val), strategy);
   try {
     String[] vals = parser.getLine();
     if (vals != null) {
       for (String v : vals) base.add(doc, line, column, v);
     } else {
       base.add(doc, line, column, val);
     }
   } catch (IOException e) {
     throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
   }
 }