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();
}
}
}
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);
}
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);
}
}
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);
}
}
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!");
}
}
Aggregations