Search in sources :

Example 1 with FieldValidator

use of org.apache.metron.common.configuration.FieldValidator in project metron by apache.

the class BaseValidationTest method execute.

public boolean execute(String config, Map<String, Object> input) throws IOException {
    Configurations configurations = getConfiguration(config);
    FieldValidator validator = getValidator(configurations);
    return validator.isValid(new JSONObject(input), configurations.getGlobalConfig(), Context.EMPTY_CONTEXT());
}
Also used : JSONObject(org.json.simple.JSONObject) FieldValidator(org.apache.metron.common.configuration.FieldValidator) Configurations(org.apache.metron.common.configuration.Configurations)

Example 2 with FieldValidator

use of org.apache.metron.common.configuration.FieldValidator in project metron by apache.

the class ParserBolt method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(Tuple tuple) {
    byte[] originalMessage = (byte[]) messageGetStrategy.get(tuple);
    SensorParserConfig sensorParserConfig = getSensorParserConfig();
    try {
        // we want to ack the tuple in the situation where we have are not doing a bulk write
        // otherwise we want to defer to the writerComponent who will ack on bulk commit.
        boolean ackTuple = !writer.handleAck();
        int numWritten = 0;
        if (sensorParserConfig != null) {
            Map<String, Object> metadata = getMetadata(tuple, sensorParserConfig.getReadMetadata());
            List<FieldValidator> fieldValidations = getConfigurations().getFieldValidations();
            Optional<List<JSONObject>> messages = parser.parseOptional(originalMessage);
            for (JSONObject message : messages.orElse(Collections.emptyList())) {
                message.put(Constants.SENSOR_TYPE, getSensorType());
                if (sensorParserConfig.getMergeMetadata()) {
                    message.putAll(metadata);
                }
                for (FieldTransformer handler : sensorParserConfig.getFieldTransformations()) {
                    if (handler != null) {
                        if (!sensorParserConfig.getMergeMetadata()) {
                            // if we haven't merged metadata, then we need to pass them along as configuration params.
                            handler.transformAndUpdate(message, stellarContext, sensorParserConfig.getParserConfig(), metadata);
                        } else {
                            handler.transformAndUpdate(message, stellarContext, sensorParserConfig.getParserConfig());
                        }
                    }
                }
                if (!message.containsKey(Constants.GUID)) {
                    message.put(Constants.GUID, UUID.randomUUID().toString());
                }
                if (parser.validate(message) && (filter == null || filter.emitTuple(message, stellarContext))) {
                    numWritten++;
                    List<FieldValidator> failedValidators = getFailedValidators(message, fieldValidations);
                    if (failedValidators.size() > 0) {
                        MetronError error = new MetronError().withErrorType(Constants.ErrorType.PARSER_INVALID).withSensorType(getSensorType()).addRawMessage(message);
                        Set<String> errorFields = failedValidators.stream().flatMap(fieldValidator -> fieldValidator.getInput().stream()).collect(Collectors.toSet());
                        if (!errorFields.isEmpty()) {
                            error.withErrorFields(errorFields);
                        }
                        ErrorUtils.handleError(collector, error);
                    } else {
                        writer.write(getSensorType(), tuple, message, getConfigurations(), messageGetStrategy);
                    }
                }
            }
        }
        // then we want to handle the ack ourselves.
        if (ackTuple || numWritten == 0) {
            collector.ack(tuple);
        }
    } catch (Throwable ex) {
        handleError(originalMessage, tuple, ex, collector);
    }
}
Also used : OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) TopologyContext(org.apache.storm.task.TopologyContext) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) METADATA_PREFIX(org.apache.metron.common.Constants.METADATA_PREFIX) StringUtils(org.apache.commons.lang3.StringUtils) Filters(org.apache.metron.parsers.filters.Filters) MessageParser(org.apache.metron.parsers.interfaces.MessageParser) ArrayList(java.util.ArrayList) Tuple(org.apache.storm.tuple.Tuple) OutputCollector(org.apache.storm.task.OutputCollector) Map(java.util.Map) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) JSONUtils(org.apache.metron.common.utils.JSONUtils) MessageGetStrategy(org.apache.metron.common.message.MessageGetStrategy) ConfiguredParserBolt(org.apache.metron.common.bolt.ConfiguredParserBolt) FieldValidator(org.apache.metron.common.configuration.FieldValidator) ErrorUtils(org.apache.metron.common.utils.ErrorUtils) MetronError(org.apache.metron.common.error.MetronError) Context(org.apache.metron.stellar.dsl.Context) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) IOException(java.io.IOException) UUID(java.util.UUID) Fields(org.apache.storm.tuple.Fields) Constants(org.apache.metron.common.Constants) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) JSONObject(org.json.simple.JSONObject) MessageGetters(org.apache.metron.common.message.MessageGetters) MessageFilter(org.apache.metron.parsers.interfaces.MessageFilter) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) Optional(java.util.Optional) Collections(java.util.Collections) MetronError(org.apache.metron.common.error.MetronError) FieldValidator(org.apache.metron.common.configuration.FieldValidator) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) JSONObject(org.json.simple.JSONObject) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FieldValidator (org.apache.metron.common.configuration.FieldValidator)2 JSONObject (org.json.simple.JSONObject)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Constants (org.apache.metron.common.Constants)1 METADATA_PREFIX (org.apache.metron.common.Constants.METADATA_PREFIX)1 ConfiguredParserBolt (org.apache.metron.common.bolt.ConfiguredParserBolt)1 Configurations (org.apache.metron.common.configuration.Configurations)1 FieldTransformer (org.apache.metron.common.configuration.FieldTransformer)1