Search in sources :

Example 81 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project sandbox by backpaper0.

the class CsvTest method testWrite.

@Test
public void testWrite() throws Exception {
    final CsvMapper mapper = new CsvMapper();
    final CsvSchema schema = mapper.schemaFor(Message.class).rebuild().setUseHeader(true).setLineSeparator("\r\n").build();
    final StringWriter out = new StringWriter();
    try (final SequenceWriter writer = mapper.writerFor(Message.class).with(schema).writeValues(out)) {
        writer.write(new Message(123, "foo"));
        writer.write(new Message(456, "bar"));
        writer.write(new Message(789, "baz"));
    }
    final String csv = out.toString();
    System.out.println(csv);
    assertEquals("id,content\r\n" + "123,foo\r\n" + "456,bar\r\n" + "789,baz\r\n", csv);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) StringWriter(java.io.StringWriter) SequenceWriter(com.fasterxml.jackson.databind.SequenceWriter) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) Test(org.junit.jupiter.api.Test)

Example 82 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project java-client-api by marklogic.

the class JacksonDatabindTest method testDatabindingThirdPartyPojoWithMixinAnnotations.

/**
 * Demonstrate using Jackson's CSV mapper directly to simplify reading in data, populating a
 * third-party pojo (one we cannot annotate) then writing it out
 * via JacksonDatabindHandle with configuration provided by mix-in annotations.
 */
@Test
public void testDatabindingThirdPartyPojoWithMixinAnnotations() throws JsonProcessingException, IOException {
    CsvSchema schema = CsvSchema.builder().setColumnSeparator('\t').addColumn("geoNameId").addColumn("name").addColumn("asciiName").addColumn("alternateNames").addColumn("latitude", CsvSchema.ColumnType.NUMBER).addColumn("longitude", CsvSchema.ColumnType.NUMBER).addColumn("featureClass").addColumn("featureCode").addColumn("countryCode").addColumn("countryCode2").addColumn("adminCode1").addColumn("adminCode2").addColumn("adminCode3").addColumn("adminCode4").addColumn("population").addColumn("elevation", CsvSchema.ColumnType.NUMBER).addColumn("dem", CsvSchema.ColumnType.NUMBER).addColumn("timezoneCode").addColumn("lastModified").build();
    CsvMapper mapper = new CsvMapper();
    mapper.addMixInAnnotations(Toponym.class, ToponymMixIn1.class);
    ObjectReader reader = mapper.reader(Toponym.class).with(schema);
    try (BufferedReader cityReader = new BufferedReader(Common.testFileToReader(CITIES_FILE))) {
        GenericDocumentManager docMgr = client.newDocumentManager();
        DocumentWriteSet set = docMgr.newWriteSet();
        String line = null;
        for (int numWritten = 0; numWritten < MAX_TO_WRITE && (line = cityReader.readLine()) != null; numWritten++) {
            Toponym city = reader.readValue(line);
            JacksonDatabindHandle handle = new JacksonDatabindHandle(city);
            handle.getMapper().addMixInAnnotations(Toponym.class, ToponymMixIn2.class);
            set.add(DIRECTORY + "/thirdPartyJsonCities/" + city.getGeoNameId() + ".json", handle);
        }
        docMgr.write(set);
    // we can add assertions later, for now this test just serves as example code and
    // ensures no exceptions are thrown
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) GenericDocumentManager(com.marklogic.client.document.GenericDocumentManager) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) BufferedReader(java.io.BufferedReader) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JacksonDatabindHandle(com.marklogic.client.io.JacksonDatabindHandle) Toponym(org.geonames.Toponym) DocumentWriteSet(com.marklogic.client.document.DocumentWriteSet) Test(org.junit.Test)

Example 83 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project deephaven-csv by deephaven.

the class LargeTableJacksonCsv method read.

public static Results read(final InputStream in, final String[] headers, final Results results) throws Exception {
    final DateTimeToLongParser dtlp = new Deephaven();
    final CsvSchema.Builder builder = CsvSchema.builder();
    for (String header : headers) {
        builder.addColumn(header);
    }
    final CsvSchema schema = builder.build().withSkipFirstDataRow(true);
    final MappingIterator<Row> it = new CsvMapper().readerFor(Row.class).with(schema).readValues(in);
    int row = 0;
    while (it.hasNext()) {
        final Row record = it.next();
        results.timestamps[row] = dtlp.parse(record.timestampAsString);
        results.strings[row] = record.string;
        results.boolsAsBytes[row] = record.bool ? (byte) 1 : (byte) 0;
        results.longs0[row] = record.long0;
        results.longs1[row] = record.long1;
        results.doubles0[row] = record.double0;
        results.doubles1[row] = record.double1;
        results.doubles2[row] = record.double2;
        ++row;
        if ((row % 1_000_000) == 0) {
            System.out.printf("JacksonCSV: processed %d rows\n", row);
        }
    }
    return results;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) Deephaven(io.deephaven.csv.benchmark.util.DateTimeToLongParser.Deephaven) DateTimeToLongParser(io.deephaven.csv.benchmark.util.DateTimeToLongParser) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper)

Example 84 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project RouteConverter by cpesch.

the class CsvFormat method write.

public void write(CsvRoute route, OutputStream target, int startIndex, int endIndex) throws IOException {
    List<CsvPosition> positions = route.getPositions();
    CsvSchema.Builder builder = new CsvSchema.Builder();
    for (String key : collectKeys(positions)) builder = builder.addColumn(key);
    CsvSchema schema = builder.build().withHeader().withColumnSeparator(getColumnSeparator());
    try (SequenceWriter writer = new CsvMapper().writer(schema).writeValues(target)) {
        for (int i = startIndex; i < endIndex; i++) {
            CsvPosition position = positions.get(i);
            writer.write(position.getRowAsMap());
        }
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) SequenceWriter(com.fasterxml.jackson.databind.SequenceWriter) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper)

Example 85 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project ballerina-distribution by ballerina-platform.

the class GenerateBuildDataCsv method main.

public static void main(String[] args) throws IOException {
    Path artefactsDir = Paths.get(System.getProperty("user.dir"), "build", "build-time-data");
    Path outputCsvFile = artefactsDir.resolve("buildTimeData.csv");
    List<Path> jsonFiles = getJsonFiles(artefactsDir);
    CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder();
    JsonNode jsonTree = new ObjectMapper().readTree(jsonFiles.get(0).toFile());
    csvSchemaBuilder.addColumn("name");
    jsonTree.fieldNames().forEachRemaining(fieldName -> csvSchemaBuilder.addColumn(fieldName, CsvSchema.ColumnType.NUMBER));
    CsvSchema csvSchema;
    CsvMapper csvMapper = new CsvMapper();
    if (!Files.exists(outputCsvFile)) {
        // This done to avoid duplication of the header in the CSV file
        csvSchema = csvSchemaBuilder.build().withHeader();
        csvMapper.writerFor(JsonNode.class).with(csvSchema).writeValue(outputCsvFile.toFile(), null);
    }
    csvSchema = csvSchemaBuilder.build().withoutHeader();
    for (Path jsonFile : jsonFiles) {
        JsonNode jsonTree1 = new ObjectMapper().readTree(jsonFile.toFile());
        // additionally ass the name of the sample to the record
        ((ObjectNode) jsonTree1).put("name", jsonFile.getFileName().toString().split("-build-time.json")[0]);
        ObjectWriter writer = csvMapper.writerFor(JsonNode.class).with(csvSchema);
        try (OutputStream outstream = new FileOutputStream(outputCsvFile.toFile(), true)) {
            writer.writeValue(outstream, jsonTree1);
        }
    }
}
Also used : Path(java.nio.file.Path) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)91 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)67 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)21 IOException (java.io.IOException)18 Map (java.util.Map)16 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)15 Test (org.junit.jupiter.api.Test)13 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Converter (org.apache.flink.formats.common.Converter)12 CsvSchema (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema)12 ArrayList (java.util.ArrayList)11 BulkWriter (org.apache.flink.api.common.serialization.BulkWriter)9 File (java.io.File)7 InputStream (java.io.InputStream)6 Serializable (java.io.Serializable)6 Arrays (java.util.Arrays)6 FileOutputStream (java.io.FileOutputStream)5 Internal (org.apache.flink.annotation.Internal)5