Search in sources :

Example 26 with CSVParser

use of org.apache.commons.csv.CSVParser in project phoenix by apache.

the class CSVCommonsLoaderIT method testCSVUpsertWithCustomDelimiters.

@Test
public void testCSVUpsertWithCustomDelimiters() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.<String>asList(STOCK_COLUMNS), true, '1', '2', '3', CSVCommonsLoader.DEFAULT_ARRAY_ELEMENT_SEPARATOR);
        csvUtil.upsert(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER));
        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + stockTableName);
        ResultSet phoenixResultSet = statement.executeQuery();
        parser = new CSVParser(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER), csvUtil.getFormat());
        for (CSVRecord record : parser) {
            assertTrue(phoenixResultSet.next());
            int i = 0;
            for (String value : record) {
                assertEquals(value, phoenixResultSet.getString(i + 1));
                i++;
            }
        }
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CSVRecord(org.apache.commons.csv.CSVRecord) Test(org.junit.Test)

Example 27 with CSVParser

use of org.apache.commons.csv.CSVParser in project phoenix by apache.

the class CSVCommonsLoader method upsert.

/**
     * Upserts data from CSV file.
     *
     * Data is batched up based on connection batch size.
     * Column PDataType is read from metadata and is used to convert
     * column value to correct type before upsert.
     *
     * The constructor determines the format for the CSV files.
     *
     * @param fileName
     * @throws Exception
     */
public void upsert(String fileName) throws Exception {
    CSVParser parser = CSVParser.parse(new File(fileName), Charsets.UTF_8, format);
    upsert(parser);
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) File(java.io.File)

Example 28 with CSVParser

use of org.apache.commons.csv.CSVParser in project phoenix by apache.

the class CSVFileResultHandler method read.

public synchronized List<Result> read() throws IOException {
    CSVParser parser = null;
    util.ensureBaseResultDirExists();
    try {
        File file = new File(resultFileName);
        parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT);
        List<CSVRecord> records = parser.getRecords();
        List<Result> results = new ArrayList<>();
        String header = null;
        for (CSVRecord record : records) {
            // First record is the CSV Header
            if (record.getRecordNumber() == 1) {
                header = record.toString();
                continue;
            }
            List<ResultValue> resultValues = new ArrayList<>();
            for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) {
                resultValues.add(new ResultValue(val));
            }
            Result result = new Result(resultFileDetails, header, resultValues);
            results.add(result);
        }
        return results;
    } finally {
        parser.close();
    }
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) ArrayList(java.util.ArrayList) CSVRecord(org.apache.commons.csv.CSVRecord) ResultValue(org.apache.phoenix.pherf.result.ResultValue) File(java.io.File) Result(org.apache.phoenix.pherf.result.Result)

Example 29 with CSVParser

use of org.apache.commons.csv.CSVParser in project tika by apache.

the class ISATabUtils method parseStudy.

public static void parseStudy(InputStream stream, XHTMLContentHandler xhtml, Metadata metadata, ParseContext context) throws IOException, TikaException, SAXException {
    TikaInputStream tis = TikaInputStream.get(stream);
    // Automatically detect the character encoding
    TikaConfig tikaConfig = context.get(TikaConfig.class);
    if (tikaConfig == null) {
        tikaConfig = TikaConfig.getDefaultConfig();
    }
    try (AutoDetectReader reader = new AutoDetectReader(new CloseShieldInputStream(tis), metadata, tikaConfig.getEncodingDetector());
        CSVParser csvParser = new CSVParser(reader, CSVFormat.TDF)) {
        Iterator<CSVRecord> iterator = csvParser.iterator();
        xhtml.startElement("table");
        xhtml.startElement("thead");
        if (iterator.hasNext()) {
            CSVRecord record = iterator.next();
            for (int i = 0; i < record.size(); i++) {
                xhtml.startElement("th");
                xhtml.characters(record.get(i));
                xhtml.endElement("th");
            }
        }
        xhtml.endElement("thead");
        xhtml.startElement("tbody");
        while (iterator.hasNext()) {
            CSVRecord record = iterator.next();
            xhtml.startElement("tr");
            for (int j = 0; j < record.size(); j++) {
                xhtml.startElement("td");
                xhtml.characters(record.get(j));
                xhtml.endElement("td");
            }
            xhtml.endElement("tr");
        }
        xhtml.endElement("tbody");
        xhtml.endElement("table");
    }
}
Also used : TikaConfig(org.apache.tika.config.TikaConfig) AutoDetectReader(org.apache.tika.detect.AutoDetectReader) CSVParser(org.apache.commons.csv.CSVParser) TikaInputStream(org.apache.tika.io.TikaInputStream) CSVRecord(org.apache.commons.csv.CSVRecord) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 30 with CSVParser

use of org.apache.commons.csv.CSVParser in project tika by apache.

the class ISATabUtils method extractMetadata.

private static void extractMetadata(Reader reader, Metadata metadata, String studyFileName) throws IOException {
    boolean investigationSection = false;
    boolean studySection = false;
    boolean studyTarget = false;
    Map<String, String> map = new HashMap<String, String>();
    try (CSVParser csvParser = new CSVParser(reader, CSVFormat.TDF)) {
        Iterator<CSVRecord> iterator = csvParser.iterator();
        while (iterator.hasNext()) {
            CSVRecord record = iterator.next();
            String field = record.get(0);
            if ((field.toUpperCase(Locale.ENGLISH).equals(field)) && (record.size() == 1)) {
                investigationSection = Arrays.asList(sections).contains(field);
                studySection = (studyFileName != null) && (field.equals(studySectionField));
            } else {
                if (investigationSection) {
                    addMetadata(field, record, metadata);
                } else if (studySection) {
                    if (studyTarget) {
                        break;
                    }
                    String value = record.get(1);
                    map.put(field, value);
                    studyTarget = (field.equals(studyFileNameField)) && (value.equals(studyFileName));
                    if (studyTarget) {
                        mapStudyToMetadata(map, metadata);
                        studySection = false;
                    }
                } else if (studyTarget) {
                    addMetadata(field, record, metadata);
                }
            }
        }
    } catch (IOException ioe) {
        throw ioe;
    }
}
Also used : HashMap(java.util.HashMap) CSVParser(org.apache.commons.csv.CSVParser) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Aggregations

CSVParser (org.apache.commons.csv.CSVParser)31 CSVRecord (org.apache.commons.csv.CSVRecord)19 StringReader (java.io.StringReader)17 Test (org.junit.Test)16 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)15 PreparedStatement (java.sql.PreparedStatement)11 ResultSet (java.sql.ResultSet)11 IOException (java.io.IOException)5 File (java.io.File)3 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)2 TikaConfig (org.apache.tika.config.TikaConfig)2 AutoDetectReader (org.apache.tika.detect.AutoDetectReader)2 TikaInputStream (org.apache.tika.io.TikaInputStream)2 ImmutableTable (com.google.common.collect.ImmutableTable)1 JsonArray (com.google.gson.JsonArray)1