Search in sources :

Example 1 with ExtendedJsonOutput

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

the class DrillValuesRel method convertToJsonNode.

private static JsonNode convertToJsonNode(RelDataType rowType, ImmutableList<ImmutableList<RexLiteral>> tuples) throws IOException {
    TokenBuffer out = new TokenBuffer(MAPPER.getFactory().getCodec(), false);
    JsonOutput json = new ExtendedJsonOutput(out);
    json.writeStartArray();
    String[] fields = rowType.getFieldNames().toArray(new String[rowType.getFieldCount()]);
    for (List<RexLiteral> row : tuples) {
        json.writeStartObject();
        int i = 0;
        for (RexLiteral field : row) {
            json.writeFieldName(fields[i]);
            writeLiteral(field, json);
            i++;
        }
        json.writeEndObject();
    }
    json.writeEndArray();
    json.flush();
    return out.asParser().readValueAsTree();
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) JsonOutput(org.apache.drill.exec.vector.complex.fn.JsonOutput) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) NlsString(org.apache.calcite.util.NlsString)

Example 2 with ExtendedJsonOutput

use of org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput 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

ExtendedJsonOutput (org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1 TokenBuffer (com.fasterxml.jackson.databind.util.TokenBuffer)1 IOException (java.io.IOException)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 NlsString (org.apache.calcite.util.NlsString)1 BasicJsonOutput (org.apache.drill.exec.vector.complex.fn.BasicJsonOutput)1 JsonOutput (org.apache.drill.exec.vector.complex.fn.JsonOutput)1 Configuration (org.apache.hadoop.conf.Configuration)1 Path (org.apache.hadoop.fs.Path)1