Search in sources :

Example 16 with ParseException

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

the class JsonReader method parseInputRows.

@Override
protected List<InputRow> parseInputRows(String intermediateRow) throws IOException, ParseException {
    final List<InputRow> inputRows;
    try (JsonParser parser = jsonFactory.createParser(intermediateRow)) {
        final MappingIterator<JsonNode> delegate = mapper.readValues(parser, JsonNode.class);
        inputRows = FluentIterable.from(() -> delegate).transform(jsonNode -> MapInputRowParser.parse(inputRowSchema, flattener.flatten(jsonNode))).toList();
    } catch (RuntimeException e) {
        // JsonParseException will be thrown from MappingIterator#hasNext or MappingIterator#next when input json text is ill-formed
        if (e.getCause() instanceof JsonParseException) {
            throw new ParseException(intermediateRow, e, "Unable to parse row [%s]", intermediateRow);
        }
        // throw unknown exception
        throw e;
    }
    if (CollectionUtils.isNullOrEmpty(inputRows)) {
        throw new ParseException(intermediateRow, "Unable to parse [%s] as the intermediateRow resulted in empty input row", intermediateRow);
    }
    return inputRows;
}
Also used : InputRow(org.apache.druid.data.input.InputRow) JsonNode(com.fasterxml.jackson.databind.JsonNode) ParseException(org.apache.druid.java.util.common.parsers.ParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 17 with ParseException

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

the class MapInputRowParser method parse.

@VisibleForTesting
static InputRow parse(TimestampSpec timestampSpec, DimensionsSpec dimensionsSpec, Map<String, Object> theMap) throws ParseException {
    final List<String> dimensionsToUse = findDimensions(dimensionsSpec, theMap);
    final DateTime timestamp;
    try {
        timestamp = timestampSpec.extractTimestamp(theMap);
    } catch (Exception e) {
        String rawMap = rawMapToPrint(theMap);
        throw new ParseException(rawMap, e, "Timestamp[%s] is unparseable! Event: %s", timestampSpec.getRawTimestamp(theMap), rawMap);
    }
    if (timestamp == null) {
        String rawMap = rawMapToPrint(theMap);
        throw new ParseException(rawMap, "Timestamp[%s] is unparseable! Event: %s", timestampSpec.getRawTimestamp(theMap), rawMap);
    }
    if (!Intervals.ETERNITY.contains(timestamp)) {
        String rawMap = rawMapToPrint(theMap);
        throw new ParseException(rawMap, "Encountered row with timestamp[%s] that cannot be represented as a long: [%s]", timestamp, rawMap);
    }
    return new MapBasedInputRow(timestamp, dimensionsToUse, theMap);
}
Also used : ParseException(org.apache.druid.java.util.common.parsers.ParseException) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) DateTime(org.joda.time.DateTime) ParseException(org.apache.druid.java.util.common.parsers.ParseException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 18 with ParseException

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

the class InfluxParser method parseToMap.

@Nullable
@Override
public Map<String, Object> parseToMap(String input) {
    CharStream charStream = new ANTLRInputStream(input);
    InfluxLineProtocolLexer lexer = new InfluxLineProtocolLexer(charStream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    InfluxLineProtocolParser parser = new InfluxLineProtocolParser(tokenStream);
    List<InfluxLineProtocolParser.LineContext> lines = parser.lines().line();
    if (parser.getNumberOfSyntaxErrors() != 0) {
        throw new ParseException(null, "Unable to parse line.");
    }
    if (lines.size() != 1) {
        throw new ParseException(null, "Multiple lines present; unable to parse more than one per record.");
    }
    Map<String, Object> out = new LinkedHashMap<>();
    InfluxLineProtocolParser.LineContext line = lines.get(0);
    String measurement = parseIdentifier(line.identifier());
    if (!checkWhitelist(measurement)) {
        throw new ParseException(null, "Metric [%s] not whitelisted.", measurement);
    }
    out.put(MEASUREMENT_KEY, measurement);
    if (line.tag_set() != null) {
        line.tag_set().tag_pair().forEach(t -> parseTag(t, out));
    }
    line.field_set().field_pair().forEach(t -> parseField(t, out));
    if (line.timestamp() != null) {
        String timestamp = line.timestamp().getText();
        parseTimestamp(timestamp, out);
    }
    return out;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenStream(org.antlr.v4.runtime.TokenStream) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream) LinkedHashMap(java.util.LinkedHashMap) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Nullable(javax.annotation.Nullable)

Example 19 with ParseException

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

the class ProtobufInputRowParser method parseBatch.

@Override
public List<InputRow> parseBatch(ByteBuffer input) {
    if (parser == null) {
        parser = parseSpec.makeParser();
    }
    Map<String, Object> record;
    DateTime timestamp;
    if (isFlatSpec) {
        try {
            DynamicMessage message = protobufBytesDecoder.parse(input);
            record = CollectionUtils.mapKeys(message.getAllFields(), k -> k.getJsonName());
            timestamp = this.timestampSpec.extractTimestamp(record);
        } catch (Exception ex) {
            throw new ParseException(null, ex, "Protobuf message could not be parsed");
        }
    } else {
        try {
            DynamicMessage message = protobufBytesDecoder.parse(input);
            String json = JsonFormat.printer().print(message);
            record = parser.parseToMap(json);
            timestamp = this.timestampSpec.extractTimestamp(record);
        } catch (InvalidProtocolBufferException e) {
            throw new ParseException(null, e, "Protobuf message could not be parsed");
        }
    }
    final List<String> dimensions;
    if (!this.dimensions.isEmpty()) {
        dimensions = this.dimensions;
    } else {
        dimensions = Lists.newArrayList(Sets.difference(record.keySet(), parseSpec.getDimensionsSpec().getDimensionExclusions()));
    }
    return ImmutableList.of(new MapBasedInputRow(timestamp, dimensions, record));
}
Also used : ParseSpec(org.apache.druid.data.input.impl.ParseSpec) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteBufferInputRowParser(org.apache.druid.data.input.ByteBufferInputRowParser) DynamicMessage(com.google.protobuf.DynamicMessage) Parser(org.apache.druid.java.util.common.parsers.Parser) ParseException(org.apache.druid.java.util.common.parsers.ParseException) DateTime(org.joda.time.DateTime) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) JSONParseSpec(org.apache.druid.data.input.impl.JSONParseSpec) CollectionUtils(org.apache.druid.utils.CollectionUtils) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) Sets(com.google.common.collect.Sets) ByteBuffer(java.nio.ByteBuffer) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) JsonFormat(com.google.protobuf.util.JsonFormat) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) DateTime(org.joda.time.DateTime) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ParseException(org.apache.druid.java.util.common.parsers.ParseException)

Example 20 with ParseException

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

the class ParseExceptionHandlerTest method testMetricWhenAllConfigurationsAreTurnedOff.

@Test
public void testMetricWhenAllConfigurationsAreTurnedOff() {
    final ParseException parseException = new ParseException(null, "test");
    final RowIngestionMeters rowIngestionMeters = new SimpleRowIngestionMeters();
    final ParseExceptionHandler parseExceptionHandler = new ParseExceptionHandler(rowIngestionMeters, false, Integer.MAX_VALUE, 0);
    IntStream.range(0, 100).forEach(i -> {
        parseExceptionHandler.handle(parseException);
        Assert.assertEquals(i + 1, rowIngestionMeters.getUnparseable());
    });
}
Also used : ParseException(org.apache.druid.java.util.common.parsers.ParseException) Test(org.junit.Test)

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