Search in sources :

Example 1 with BasicJsonOutput

use of org.apache.drill.exec.vector.complex.fn.BasicJsonOutput in project drill by apache.

the class JsonRecordWriter method init.

@Override
public void init(Map<String, String> writerOptions) throws IOException {
    this.location = writerOptions.get("location");
    this.prefix = writerOptions.get("prefix");
    this.fieldDelimiter = writerOptions.get("separator");
    this.extension = writerOptions.get("extension");
    this.useExtendedOutput = Boolean.parseBoolean(writerOptions.get("extended"));
    this.skipNullFields = Boolean.parseBoolean(writerOptions.get("skipnulls"));
    final boolean uglify = Boolean.parseBoolean(writerOptions.get("uglify"));
    Configuration conf = new Configuration();
    conf.set(FileSystem.FS_DEFAULT_NAME_KEY, writerOptions.get(FileSystem.FS_DEFAULT_NAME_KEY));
    this.fs = FileSystem.get(conf);
    Path fileName = new Path(location, prefix + "_" + index + "." + extension);
    try {
        // json writer does not support partitions, so only one file can be created
        // and thus only one location should be deleted in case of abort
        // to ensure that our writer was the first to create output file,
        // we create empty output file first and fail if file exists
        cleanUpLocation = storageStrategy.createFileAndApply(fs, fileName);
        // since empty output file will be overwritten (some file systems may restrict append option)
        // we need to re-apply file permission
        stream = fs.create(fileName);
        storageStrategy.applyToFile(fs, fileName);
        JsonGenerator generator = factory.createGenerator(stream).useDefaultPrettyPrinter();
        if (uglify) {
            generator = generator.setPrettyPrinter(new MinimalPrettyPrinter(LINE_FEED));
        }
        if (useExtendedOutput) {
            gen = new ExtendedJsonOutput(generator);
        } else {
            gen = new BasicJsonOutput(generator);
        }
        logger.debug("Created file: {}", fileName);
    } catch (IOException ex) {
        logger.error("Unable to create file: " + fileName, ex);
        throw ex;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) Configuration(org.apache.hadoop.conf.Configuration) BasicJsonOutput(org.apache.drill.exec.vector.complex.fn.BasicJsonOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1 IOException (java.io.IOException)1 BasicJsonOutput (org.apache.drill.exec.vector.complex.fn.BasicJsonOutput)1 ExtendedJsonOutput (org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput)1 Configuration (org.apache.hadoop.conf.Configuration)1 Path (org.apache.hadoop.fs.Path)1