Search in sources :

Example 61 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project opentheso by miledrousset.

the class CsvReadHelper method readFile.

public boolean readFile(Reader in) {
    try {
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().withDelimiter(delimiter).withIgnoreEmptyLines().parse(in);
        String uri1;
        for (CSVRecord record : records) {
            // if(first) {
            // // set the Uri
            // try {
            // uri = record.get("URI");
            // uri = uri.substring(0, uri.lastIndexOf("/"));
            // } catch (Exception e) {
            // //System.err.println("");
            // }
            // first = false;
            // }
            ConceptObject conceptObject = new ConceptObject();
            // puis on génère un nouvel identifiant
            try {
                uri1 = record.get("URI");
                conceptObject.setIdConcept(getId(uri1));
            } catch (Exception e) {
            // System.err.println("");
            }
            // on récupère les labels
            conceptObject = getLabels(conceptObject, record);
            // on récupère les notes
            conceptObject = getNotes(conceptObject, record);
            // on récupère le type
            conceptObject.setType(getType(record));
            // on récupère la notation
            conceptObject.setNotation(getNotation(record));
            // on récupère les relations (BT, NT, RT)
            conceptObject = getRelations(conceptObject, record);
            // on récupère les alignements
            conceptObject = getAlignments(conceptObject, record);
            // on récupère la localisation
            conceptObject = getGeoLocalisation(conceptObject, record);
            // on récupère les membres (l'appartenance du concept à un groupe, collection ...
            conceptObject = getMembers(conceptObject, record);
            // on récupère la date
            conceptObject = getDates(conceptObject, record);
            conceptObjects.add(conceptObject);
        }
        return true;
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(CsvReadHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
Also used : CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException) IOException(java.io.IOException)

Example 62 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project opennms by OpenNMS.

the class MeasurementQueryExecutorRemoteIT method testReportHwForecast.

@Test
public void testReportHwForecast() throws IOException, JRException {
    createReport("Forecast", new ReportFiller() {

        @Override
        public void fill(Map<String, Object> params) throws Exception {
            params.put(JRParameter.IS_IGNORE_PAGINATION, true);
            params.put("MEASUREMENT_URL", "http://localhost:9999/opennms/rest/measurements");
            params.put("dsName", "ifInOctets");
            params.put("startDate", "1414602000000");
            params.put("endDate", "1417046400000");
        }
    });
    // Verify the results of the generated report
    Table<Integer, String, Double> forecasts = TreeBasedTable.create();
    FileReader reader = new FileReader(createFileName("Forecast", "csv"));
    CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader());
    int k = 0;
    for (CSVRecord record : parser) {
        try {
            Double fit = Double.parseDouble(record.get("HWFit"));
            Double lwr = Double.parseDouble(record.get("HWLwr"));
            Double upr = Double.parseDouble(record.get("HWUpr"));
            if (Double.isNaN(fit)) {
                continue;
            }
            forecasts.put(k, "fit", fit);
            forecasts.put(k, "lwr", lwr);
            forecasts.put(k, "upr", upr);
            k++;
        } catch (NumberFormatException e) {
        // pass
        }
    }
    Assert.assertEquals(340, forecasts.rowKeySet().size());
    // First fitted value
    Assert.assertEquals(432.526086422424, forecasts.get(0, "fit"), 0.00001);
    // Last fitted value for which there is a known data point
    Assert.assertEquals(24079.4692522087, forecasts.get(327, "fit"), 0.00001);
    // First forecasted value
    Assert.assertEquals(22245.5417010936, forecasts.get(328, "fit"), 0.00001);
}
Also used : JRException(net.sf.jasperreports.engine.JRException) IOException(java.io.IOException) CSVParser(org.apache.commons.csv.CSVParser) FileReader(java.io.FileReader) CSVRecord(org.apache.commons.csv.CSVRecord) Test(org.junit.Test)

Example 63 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project camel by apache.

the class CsvRecordConvertersTest method setUp.

@Before
public void setUp() throws Exception {
    CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C");
    CSVParser parser = new CSVParser(new StringReader("1,2,3"), format);
    List<CSVRecord> records = parser.getRecords();
    record = records.get(0);
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) Before(org.junit.Before)

Example 64 with CSVRecord

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

the class CSVCommonsLoaderIT method testCSVCommonsUpsertEncapsulatedControlChars.

@Test
public void testCSVCommonsUpsertEncapsulatedControlChars() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + ENCAPSULATED_CHARS_TABLE + "(MYKEY VARCHAR NOT NULL PRIMARY KEY, MYVALUE VARCHAR);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, ENCAPSULATED_CHARS_TABLE, Collections.<String>emptyList(), true);
        csvUtil.upsert(new StringReader(CSV_VALUES_ENCAPSULATED_CONTROL_CHARS_WITH_HEADER));
        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn.prepareStatement("SELECT MYKEY, MYVALUE FROM " + ENCAPSULATED_CHARS_TABLE);
        ResultSet phoenixResultSet = statement.executeQuery();
        parser = new CSVParser(new StringReader(CSV_VALUES_ENCAPSULATED_CONTROL_CHARS_WITH_HEADER), 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 65 with CSVRecord

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

the class CSVCommonsLoaderIT method testCSVUpsertWithNoColumns.

@Test
public void testCSVUpsertWithNoColumns() 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, null, true);
        csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
        // 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), 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)

Aggregations

CSVRecord (org.apache.commons.csv.CSVRecord)127 CSVParser (org.apache.commons.csv.CSVParser)71 IOException (java.io.IOException)40 CSVFormat (org.apache.commons.csv.CSVFormat)40 ArrayList (java.util.ArrayList)36 Reader (java.io.Reader)24 StringReader (java.io.StringReader)22 InputStreamReader (java.io.InputStreamReader)18 FileReader (java.io.FileReader)16 Test (org.junit.Test)14 Path (java.nio.file.Path)13 HashMap (java.util.HashMap)11 File (java.io.File)10 PreparedStatement (java.sql.PreparedStatement)10 InputStream (java.io.InputStream)9 ResultSet (java.sql.ResultSet)9 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)9 BufferedReader (java.io.BufferedReader)8 Map (java.util.Map)7