如何使用 Flux 将 Json Pojo 写入 CSV
How to use Flux to write Json Pojo to a CSV
以下函数将检索字符串列表。在合并过程中,与其打印输出,不如将输出写入 CSV 文件。
List<Mono<Device>> list1 = page.get().map(device -> webClient.post()
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just("{ \"mac\": \"" + device.getData() ), String.class)
.attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId("xxxx"))
.retrieve()
.bodyToMono(Device.class)).collect(Collectors.toList());
Flux<Device> mergedMonos = Flux.fromIterable(list1).flatMapSequential(Function.identity());
mergedMonos.doOnNext(System.out::println)
.subscribe();
public class Device {
@JsonProperty("ip")
private String ip;
@JsonProperty("macAddress")
private String macAddress;
@JsonProperty("vendor")
private String vendor;
如何将设备 (Json) 数据写入 CSV 文件?
谢谢
结帐Converting JSON to CSV in Java
您可以利用 jackson 库将 java 对象转换为 csv。在依赖项中添加:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
然后在代码中:
ObjectMapper mapper = new ObjectMapper();
Flux<Device> mergedMonos =
Flux.fromIterable(list1).flatMapSequential(Function.identity());
List<JsonNode> nodes = mergedMonos
.collectList()
.map(mapper::valueToTree)
.subscribe(list -> {
Builder csvSchemaBuilder = CsvSchema.builder();
JsonNode firstObject = list.elements().next();
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
csvMapper.writerFor(JsonNode.class)
.with(csvSchema)
.writeValue(new File("output.csv"), list);
});
以下函数将检索字符串列表。在合并过程中,与其打印输出,不如将输出写入 CSV 文件。
List<Mono<Device>> list1 = page.get().map(device -> webClient.post()
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just("{ \"mac\": \"" + device.getData() ), String.class)
.attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId("xxxx"))
.retrieve()
.bodyToMono(Device.class)).collect(Collectors.toList());
Flux<Device> mergedMonos = Flux.fromIterable(list1).flatMapSequential(Function.identity());
mergedMonos.doOnNext(System.out::println)
.subscribe();
public class Device {
@JsonProperty("ip")
private String ip;
@JsonProperty("macAddress")
private String macAddress;
@JsonProperty("vendor")
private String vendor;
如何将设备 (Json) 数据写入 CSV 文件?
谢谢
结帐Converting JSON to CSV in Java
您可以利用 jackson 库将 java 对象转换为 csv。在依赖项中添加:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
然后在代码中:
ObjectMapper mapper = new ObjectMapper();
Flux<Device> mergedMonos =
Flux.fromIterable(list1).flatMapSequential(Function.identity());
List<JsonNode> nodes = mergedMonos
.collectList()
.map(mapper::valueToTree)
.subscribe(list -> {
Builder csvSchemaBuilder = CsvSchema.builder();
JsonNode firstObject = list.elements().next();
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
csvMapper.writerFor(JsonNode.class)
.with(csvSchema)
.writeValue(new File("output.csv"), list);
});