All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.exasol.adapter.document.documentfetcher.files.JsonDocumentFetcher Maven / Gradle / Ivy

There is a newer version: 8.1.3
Show newest version
package com.exasol.adapter.document.documentfetcher.files;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.exasol.adapter.document.documentfetcher.files.segmentation.FileSegment;
import com.exasol.adapter.document.documentfetcher.files.segmentation.FileSegmentDescription;
import com.exasol.adapter.document.documentnode.DocumentNode;
import com.exasol.adapter.document.documentnode.json.JsonNodeFactory;
import com.exasol.adapter.document.files.FileTypeSpecificDocumentFetcher;
import com.exasol.adapter.document.iterators.CloseableIterator;
import com.exasol.adapter.document.iterators.CloseableIteratorWrapper;
import com.exasol.errorreporting.ExaError;

import jakarta.json.*;

/**
 * {@link FileTypeSpecificDocumentFetcher} for JSON files.
 */
public class JsonDocumentFetcher implements FileTypeSpecificDocumentFetcher {
    private static final long serialVersionUID = 2783593249946168796L;
    private static final JsonReaderFactory JSON_READER_FACTORY = Json.createReaderFactory(null);

    @Override
    public CloseableIterator readDocuments(final FileSegment segment) {
        if (!segment.getSegmentDescription().equals(FileSegmentDescription.ENTIRE_FILE)) {
            throw new IllegalStateException(ExaError.messageBuilder("F-VSDF-16")
                    .message("The JsonDocumentFetcher does not support loading split files.").ticketMitigation()
                    .toString());
        }
        final RemoteFile remoteFile = segment.getFile();
        try (final InputStream inputStream = remoteFile.getContent().getInputStream();
                final JsonReader jsonReader = buildJsonReader(inputStream)) {
            //will read out the json object
            final JsonValue jsonValue = jsonReader.readValue();
            //exasol specific Document node gets created
            final DocumentNode jsonNode = JsonNodeFactory.getInstance().getJsonNode(jsonValue);
            //1 element, the json object is returned in a list
            return new CloseableIteratorWrapper<>(List.of(jsonNode).iterator());

        } catch (final JsonException | IOException jsonException) {
            throw new InputDataException(
                    ExaError.messageBuilder("E-VSDF-1").message("Error in input file {{JSON_FILE}}.")
                            .parameter("JSON_FILE", remoteFile.getResourceName()).toString(),
                    jsonException);
        }
    }



    @Override
    public boolean supportsFileSplitting() {
        return false;
    }

    private JsonReader buildJsonReader(final InputStream inputStream) {
        try {
            return JSON_READER_FACTORY.createReader(inputStream);
        } catch (final JsonException exception) {
            if (exception.getMessage().equals("Cannot auto-detect encoding, not enough chars")) {
                return JSON_READER_FACTORY.createReader(inputStream, java.nio.charset.StandardCharsets.UTF_8);
            } else {
                throw exception;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy