Search in sources :

Example 6 with ParseException

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));
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ObjectFlattener(org.apache.druid.java.util.common.parsers.ObjectFlattener) CollectionUtils(org.apache.druid.utils.CollectionUtils) InputRowSchema(org.apache.druid.data.input.InputRowSchema) Iterators(com.google.common.collect.Iterators) ByteBuffer(java.nio.ByteBuffer) JSONPathSpec(org.apache.druid.java.util.common.parsers.JSONPathSpec) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MapInputRowParser(org.apache.druid.data.input.impl.MapInputRowParser) JSONFlattenerMaker(org.apache.druid.java.util.common.parsers.JSONFlattenerMaker) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) IOUtils(org.apache.commons.io.IOUtils) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) IntermediateRowParsingReader(org.apache.druid.data.input.IntermediateRowParsingReader) CloseableIterators(org.apache.druid.java.util.common.CloseableIterators) JsonFormat(com.google.protobuf.util.JsonFormat) ObjectFlatteners(org.apache.druid.java.util.common.parsers.ObjectFlatteners) InputEntity(org.apache.druid.data.input.InputEntity) Collections(java.util.Collections) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 7 with ParseException

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!");
    }
}
Also used : RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) Descriptors(com.google.protobuf.Descriptors) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Example 8 with ParseException

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParseException(org.apache.druid.java.util.common.parsers.ParseException)

Example 9 with ParseException

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;
}
Also used : ParseException(org.apache.druid.java.util.common.parsers.ParseException) CoderResult(java.nio.charset.CoderResult)

Example 10 with ParseException

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();
        }
    };
}
Also used : CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Iterator(java.util.Iterator) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) IOException(java.io.IOException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

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