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);
}
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
}
}
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;
}
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());
}
}
}
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);
}
}
}
Aggregations