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

com.marklogic.flux.impl.export.ExportJsonLinesFilesCommand Maven / Gradle / Ivy

There is a newer version: 1.0.0.ea1
Show newest version
/*
 * Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
 */
package com.marklogic.flux.impl.export;

import com.marklogic.flux.api.JsonLinesFilesExporter;
import com.marklogic.flux.api.ReadRowsOptions;
import com.marklogic.flux.api.WriteFilesOptions;
import picocli.CommandLine;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@CommandLine.Command(
    name = "export-json-lines-files",
    description = "Read rows via Optic from MarkLogic and write them to JSON Lines files on a local filesystem, HDFS, or S3 " +
        "using Spark's support defined at %nhttps://spark.apache.org/docs/latest/sql-data-sources-json.html ."
)
public class ExportJsonLinesFilesCommand extends AbstractExportRowsToFilesCommand implements JsonLinesFilesExporter {

    @CommandLine.Mixin
    private WriteJsonFilesParams writeParams = new WriteJsonFilesParams();

    public static class WriteJsonFilesParams extends WriteStructuredFilesParams implements WriteJsonLinesFilesOptions {

        @CommandLine.Option(names = "--encoding", description = "Specify an encoding for writing files.")
        private String encoding;

        @CommandLine.Option(
            names = "-P",
            description = "Specify any Spark JSON option defined at " +
                "%nhttps://spark.apache.org/docs/latest/sql-data-sources-json.html; e.g. -Pcompression=bzip2."
        )
        private Map additionalOptions = new HashMap<>();

        @Override
        public Map get() {
            Map options = new HashMap<>();
            if (encoding != null) {
                options.put("encoding", encoding);
            }
            options.putAll(additionalOptions);
            return options;
        }

        @Override
        public WriteJsonLinesFilesOptions encoding(String encoding) {
            this.encoding = encoding;
            return this;
        }

        @Override
        public WriteJsonLinesFilesOptions additionalOptions(Map options) {
            this.additionalOptions = options;
            return this;
        }
    }

    @Override
    protected WriteStructuredFilesParams getWriteFilesParams() {
        return writeParams;
    }

    @Override
    protected String getWriteFormat() {
        return "json";
    }

    @Override
    public JsonLinesFilesExporter from(Consumer consumer) {
        consumer.accept(readParams);
        return this;
    }

    @Override
    public JsonLinesFilesExporter from(String opticQuery) {
        return this.from(options -> options.opticQuery(opticQuery));
    }

    @Override
    public JsonLinesFilesExporter to(Consumer consumer) {
        consumer.accept(writeParams);
        return this;
    }

    @Override
    public JsonLinesFilesExporter to(String path) {
        return this.to(options -> options.path(path));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy