io.kestra.plugin.serdes.json.IonToJson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-serdes Show documentation
Show all versions of plugin-serdes Show documentation
Serialize and deserialize data formats in Kestra workflows.
The newest version!
package io.kestra.plugin.serdes.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.FileSerde;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;
import jakarta.validation.constraints.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicLong;
import static io.kestra.core.utils.Rethrow.throwConsumer;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Schema(
title = "Read an ion serialized data file and write it to a new line delimited json file."
)
@Plugin(
examples = {
@Example(
full = true,
title = "Download a CSV file and convert it to a JSON format.",
code = """
id: ion_to_json
namespace: company.team
tasks:
- id: http_download
type: io.kestra.plugin.core.http.Download
uri: https://huggingface.co/datasets/kestra/datasets/raw/main/csv/products.csv
- id: convert
type: io.kestra.plugin.serdes.csv.CsvToIon
from: "{{ outputs.http_download.uri }}"
- id: to_json
type: io.kestra.plugin.serdes.json.IonToJson
from: "{{ outputs.convert.uri }}"
"""
)
},
aliases = "io.kestra.plugin.serdes.json.JsonWriter"
)
public class IonToJson extends Task implements RunnableTask {
@NotNull
@Schema(
title = "Source file URI"
)
@PluginProperty(dynamic = true)
private String from;
@Builder.Default
@Schema(
title = "The name of a supported charset",
description = "Default value is UTF-8."
)
@PluginProperty(dynamic = true)
private final String charset = StandardCharsets.UTF_8.name();
@Builder.Default
@Schema(
title = "Is the file is a json new line (JSON-NL)",
description = "Is the file is a json with new line separator\n" +
"Warning, if not, the whole file will loaded in memory and can lead to out of memory!"
)
@PluginProperty
private final boolean newLine = true;
@Builder.Default
@io.swagger.v3.oas.annotations.media.Schema(
title = "Timezone to use when no timezone can be parsed on the source."
)
@PluginProperty(dynamic = true)
private final String timeZoneId = ZoneId.systemDefault().toString();
@Override
public Output run(RunContext runContext) throws Exception {
String suffix = this.newLine ? ".jsonl" : ".json";
File tempFile = runContext.workingDir().createTempFile(suffix).toFile();
URI from = new URI(runContext.render(this.from));
try (
OutputStream outfile = new BufferedOutputStream(new FileOutputStream(tempFile), FileSerde.BUFFER_SIZE);
BufferedReader inputStream = new BufferedReader(new InputStreamReader(runContext.storage().getFile(from)), FileSerde.BUFFER_SIZE);
) {
ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setSerializationInclusion(JsonInclude.Include.ALWAYS)
.setTimeZone(TimeZone.getTimeZone(ZoneId.of(runContext.render(this.timeZoneId))))
.registerModule(new JavaTimeModule())
.registerModule(new Jdk8Module());
SequenceWriter objectWriter = mapper.writerFor(Object.class).writeValues(outfile);
if (this.newLine) {
Flux