use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.
the class ProtobufReader method parseInputRows.
@Override
protected List<InputRow> parseInputRows(DynamicMessage intermediateRow) throws ParseException, JsonProcessingException {
Map<String, Object> record;
if (flattenSpec == null || JSONPathSpec.DEFAULT.equals(flattenSpec)) {
try {
record = CollectionUtils.mapKeys(intermediateRow.getAllFields(), k -> k.getJsonName());
} catch (Exception ex) {
throw new ParseException(null, ex, "Protobuf message could not be parsed");
}
} else {
try {
String json = JsonFormat.printer().print(intermediateRow);
record = recordFlattener.flatten(OBJECT_MAPPER.readValue(json, JsonNode.class));
} catch (InvalidProtocolBufferException e) {
throw new ParseException(null, e, "Protobuf message could not be parsed");
}
}
return Collections.singletonList(MapInputRowParser.parse(inputRowSchema, record));
}
use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.
the class SchemaRegistryBasedProtobufBytesDecoder method parse.
@Override
public DynamicMessage parse(ByteBuffer bytes) {
// ignore first \0 byte
bytes.get();
// extract schema registry id
int id = bytes.getInt();
// ignore \0 byte before PB message
bytes.get();
int length = bytes.limit() - 2 - 4;
Descriptors.Descriptor descriptor;
try {
ProtobufSchema schema = (ProtobufSchema) registry.getSchemaById(id);
descriptor = schema.toDescriptor();
} catch (RestClientException e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to get protobuf schema because of can not connect to registry or failed http request!");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to get protobuf schema because of invalid schema!");
}
try {
byte[] rawMessage = new byte[length];
bytes.get(rawMessage, 0, length);
DynamicMessage message = DynamicMessage.parseFrom(descriptor, rawMessage);
return message;
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to decode protobuf message!");
}
}
use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.
the class RegexReader method parseLine.
private Map<String, Object> parseLine(String line) {
try {
final Matcher matcher = compiledPattern.matcher(line);
if (!matcher.matches()) {
throw new ParseException(line, "Incorrect Regex: %s . No match found.", pattern);
}
final List<String> values = new ArrayList<>();
for (int i = 1; i <= matcher.groupCount(); i++) {
values.add(matcher.group(i));
}
if (columns == null) {
columns = ParserUtils.generateFieldNames(matcher.groupCount());
}
return Utils.zipMapPartial(columns, Iterables.transform(values, multiValueFunction));
} catch (Exception e) {
throw new ParseException(line, e, "Unable to parse row [%s]", line);
}
}
use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.
the class StringInputRowParser method buildStringKeyMap.
public Map<String, Object> buildStringKeyMap(ByteBuffer input) {
int payloadSize = input.remaining();
if (chars == null || chars.remaining() < payloadSize) {
chars = CharBuffer.allocate(payloadSize);
}
final CoderResult coderResult = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).decode(input, chars, true);
Map<String, Object> theMap;
if (coderResult.isUnderflow()) {
chars.flip();
try {
theMap = parseString(chars.toString());
} finally {
chars.clear();
}
} else {
throw new ParseException(chars.toString(), "Failed with CoderResult[%s]", coderResult);
}
return theMap;
}
use of org.apache.druid.java.util.common.parsers.ParseException in project druid by druid-io.
the class IntermediateRowParsingReader method read.
@Override
public CloseableIterator<InputRow> read() throws IOException {
final CloseableIteratorWithMetadata<T> intermediateRowIteratorWithMetadata = intermediateRowIteratorWithMetadata();
return new CloseableIterator<InputRow>() {
// since parseInputRows() returns a list, the below line always iterates over the list,
// which means it calls Iterator.hasNext() and Iterator.next() at least once per row.
// This could be unnecessary if the row wouldn't be exploded into multiple inputRows.
// If this line turned out to be a performance bottleneck, perhaps parseInputRows() interface might not be a
// good idea. Subclasses could implement read() with some duplicate codes to avoid unnecessary iteration on
// a singleton list.
Iterator<InputRow> rows = null;
long currentRecordNumber = 1;
@Override
public boolean hasNext() {
if (rows == null || !rows.hasNext()) {
if (!intermediateRowIteratorWithMetadata.hasNext()) {
return false;
}
final T row = intermediateRowIteratorWithMetadata.next();
try {
rows = parseInputRows(row).iterator();
++currentRecordNumber;
} catch (IOException e) {
final Map<String, Object> metadata = intermediateRowIteratorWithMetadata.currentMetadata();
rows = new ExceptionThrowingIterator(new ParseException(String.valueOf(row), e, buildParseExceptionMessage(StringUtils.format("Unable to parse row [%s]", row), source(), currentRecordNumber, metadata)));
} catch (ParseException e) {
final Map<String, Object> metadata = intermediateRowIteratorWithMetadata.currentMetadata();
// Replace the message of the ParseException e
rows = new ExceptionThrowingIterator(new ParseException(e.getInput(), e.isFromPartiallyValidRow(), buildParseExceptionMessage(e.getMessage(), source(), currentRecordNumber, metadata)));
}
}
return true;
}
@Override
public InputRow next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return rows.next();
}
@Override
public void close() throws IOException {
intermediateRowIteratorWithMetadata.close();
}
};
}
Aggregations