Search in sources :

Example 11 with ParseException

use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.

the class Rows method objectToNumber.

/**
 * Convert an object to a number.
 *
 * If {@link NullHandling#replaceWithDefault()} is true, this method will never return null. If false, it will return
 * {@link NullHandling#defaultLongValue()} instead of null.
 *
 * @param name                 field name of the object being converted (may be used for exception messages)
 * @param inputValue           the actual object being converted
 * @param throwParseExceptions whether this method should throw a {@link ParseException} or use a default/null value
 *                             when {@param inputValue} is not numeric
 *
 * @return a Number; will not necessarily be the same type as {@param zeroClass}
 *
 * @throws ParseException if the input cannot be converted to a number and {@code throwParseExceptions} is true
 */
@Nullable
public static <T extends Number> Number objectToNumber(final String name, final Object inputValue, final boolean throwParseExceptions) {
    if (inputValue == null) {
        return NullHandling.defaultLongValue();
    } else if (inputValue instanceof Number) {
        return (Number) inputValue;
    } else if (inputValue instanceof String) {
        try {
            String metricValueString = StringUtils.removeChar(((String) inputValue).trim(), ',');
            // Longs.tryParse() doesn't support leading '+', so we need to trim it ourselves
            metricValueString = trimLeadingPlusOfLongString(metricValueString);
            Long v = Longs.tryParse(metricValueString);
            // Do NOT use ternary operator here, because it makes Java to convert Long to Double
            if (v != null) {
                return v;
            } else {
                return Double.valueOf(metricValueString);
            }
        } catch (Exception e) {
            if (throwParseExceptions) {
                throw new ParseException(String.valueOf(inputValue), e, "Unable to parse value[%s] for field[%s]", inputValue, name);
            } else {
                return NullHandling.defaultLongValue();
            }
        }
    } else {
        if (throwParseExceptions) {
            throw new ParseException(String.valueOf(inputValue), "Unknown type[%s] for field[%s]", inputValue.getClass(), name);
        } else {
            return NullHandling.defaultLongValue();
        }
    }
}
Also used : ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) Nullable(javax.annotation.Nullable)

Example 12 with ParseException

use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.

the class IntermediateRowParsingReader method sampleIntermediateRow.

/**
 * Parses and samples the intermediate row and returns input row and the raw values in it. Metadata supplied can
 * contain information about the source which will get surfaced in case an exception occurs while parsing the
 * intermediate row
 *
 * @param row intermediate row
 * @param metadata additional information about the source and the record getting parsed
 * @return sampled data from the intermediate row
 */
private InputRowListPlusRawValues sampleIntermediateRow(T row, Map<String, Object> metadata) {
    final List<Map<String, Object>> rawColumnsList;
    try {
        rawColumnsList = toMap(row);
    } catch (Exception e) {
        return InputRowListPlusRawValues.of(null, new ParseException(String.valueOf(row), e, buildParseExceptionMessage(StringUtils.nonStrictFormat("Unable to parse row [%s] into JSON", row), source(), null, metadata)));
    }
    if (CollectionUtils.isNullOrEmpty(rawColumnsList)) {
        return InputRowListPlusRawValues.of(null, new ParseException(String.valueOf(row), buildParseExceptionMessage(StringUtils.nonStrictFormat("No map object parsed for row [%s]", row), source(), null, metadata)));
    }
    List<InputRow> rows;
    try {
        rows = parseInputRows(row);
    } catch (ParseException e) {
        return InputRowListPlusRawValues.ofList(rawColumnsList, new ParseException(String.valueOf(row), e, buildParseExceptionMessage(e.getMessage(), source(), null, metadata)));
    } catch (IOException e) {
        ParseException exception = new ParseException(String.valueOf(row), e, buildParseExceptionMessage(StringUtils.nonStrictFormat("Unable to parse row [%s] into inputRow", row), source(), null, metadata));
        return InputRowListPlusRawValues.ofList(rawColumnsList, exception);
    }
    return InputRowListPlusRawValues.ofList(rawColumnsList, rows);
}
Also used : ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) Map(java.util.Map) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException)

Example 13 with ParseException

use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.

the class SchemaRegistryBasedAvroBytesDecoder method parse.

@Override
public GenericRecord parse(ByteBuffer bytes) {
    int length = bytes.limit() - 1 - 4;
    if (length < 0) {
        throw new ParseException(null, "Failed to decode avro message, not enough bytes to decode (%s)", bytes.limit());
    }
    // ignore first \0 byte
    bytes.get();
    // extract schema registry id
    int id = bytes.getInt();
    int offset = bytes.position() + bytes.arrayOffset();
    Schema schema;
    try {
        ParsedSchema parsedSchema = registry.getSchemaById(id);
        schema = parsedSchema instanceof AvroSchema ? ((AvroSchema) parsedSchema).rawSchema() : null;
    } catch (IOException | RestClientException ex) {
        throw new ParseException(null, "Failed to get Avro schema: %s", id);
    }
    if (schema == null) {
        throw new ParseException(null, "Failed to find Avro schema: %s", id);
    }
    DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    try {
        return reader.read(null, DecoderFactory.get().binaryDecoder(bytes.array(), offset, length, null));
    } catch (Exception e) {
        throw new ParseException(null, e, "Fail to decode Avro message for schema: %s!", id);
    }
}
Also used : AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Example 14 with ParseException

use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.

the class InlineSchemasAvroBytesDecoder method parse.

// It is assumed that record has following format.
// byte 1 : version, static 0x1
// byte 2-5 : int schemaId
// remaining bytes would have avro data
@Override
public GenericRecord parse(ByteBuffer bytes) {
    if (bytes.remaining() < 5) {
        throw new ParseException(null, "record must have at least 5 bytes carrying version and schemaId");
    }
    byte version = bytes.get();
    if (version != V1) {
        throw new ParseException(null, "found record of arbitrary version [%s]", version);
    }
    int schemaId = bytes.getInt();
    Schema schemaObj = schemaObjs.get(schemaId);
    if (schemaObj == null) {
        throw new ParseException(null, "Failed to find schema for id [%s]", schemaId);
    }
    DatumReader<GenericRecord> reader = new GenericDatumReader<>(schemaObj);
    try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) {
        return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
    } catch (EOFException eof) {
        // waiting for avro v1.9.0 (#AVRO-813)
        throw new ParseException(null, eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813");
    } catch (Exception e) {
        throw new ParseException(null, e, "Fail to decode avro message with schemaId [%s].", schemaId);
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) EOFException(java.io.EOFException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) GenericRecord(org.apache.avro.generic.GenericRecord) ParseException(org.apache.druid.java.util.common.parsers.ParseException) EOFException(java.io.EOFException)

Example 15 with ParseException

use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.

the class SchemaRepoBasedAvroBytesDecoder method parse.

@Override
public GenericRecord parse(ByteBuffer bytes) {
    Pair<SUBJECT, ID> subjectAndId = subjectAndIdConverter.getSubjectAndId(bytes);
    Schema schema = typedRepository.getSchema(subjectAndId.lhs, subjectAndId.rhs);
    DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) {
        return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
    } catch (EOFException eof) {
        // waiting for avro v1.9.0 (#AVRO-813)
        throw new ParseException(null, eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813");
    } catch (IOException e) {
        throw new ParseException(null, e, "Fail to decode avro message!");
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) EOFException(java.io.EOFException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

ParseException (org.apache.druid.java.util.common.parsers.ParseException)30 IOException (java.io.IOException)9 InputRow (org.apache.druid.data.input.InputRow)8 Map (java.util.Map)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 Schema (org.apache.avro.Schema)3 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 ISE (org.apache.druid.java.util.common.ISE)3 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Descriptors (com.google.protobuf.Descriptors)2 DynamicMessage (com.google.protobuf.DynamicMessage)2 EOFException (java.io.EOFException)2