MikeThomsen commented on a change in pull request #4646: URL: https://github.com/apache/nifi/pull/4646#discussion_r530678668
########## File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/DeduplicateRecords.java ########## @@ -0,0 +1,675 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.standard; + +import com.google.common.base.Joiner; +import com.google.common.hash.BloomFilter; +import com.google.common.hash.Funnels; +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.codec.digest.MessageDigestAlgorithms; +import org.apache.nifi.annotation.behavior.DynamicProperty; +import org.apache.nifi.annotation.behavior.EventDriven; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.behavior.SystemResource; +import org.apache.nifi.annotation.behavior.SystemResourceConsideration; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.SeeAlso; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.distributed.cache.client.Serializer; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.record.path.FieldValue; +import org.apache.nifi.record.path.RecordPath; +import org.apache.nifi.record.path.RecordPathResult; +import org.apache.nifi.record.path.util.RecordPathCache; +import org.apache.nifi.record.path.validation.RecordPathPropertyNameValidator; +import org.apache.nifi.record.path.validation.RecordPathValidator; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.RecordSetWriter; +import org.apache.nifi.serialization.RecordSetWriterFactory; +import org.apache.nifi.serialization.WriteResult; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.serialization.record.RecordSchema; +import org.apache.nifi.serialization.record.util.DataTypeUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Serializable; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.toList; +import static org.apache.commons.codec.binary.StringUtils.getBytesUtf8; + +@EventDriven +@SupportsBatching +@InputRequirement(Requirement.INPUT_REQUIRED) +@SystemResourceConsideration(resource = SystemResource.MEMORY, + description = "The HashSet filter type will grow memory space proportionate to the number of unique records processed. " + + "The BloomFilter type will use constant memory regardless of the number of records processed.") +@Tags({"text", "record", "update", "change", "replace", "modify", "distinct", "unique", + "filter", "hash", "dupe", "duplicate", "dedupe"}) +@CapabilityDescription("This processor attempts to deduplicate a record set in memory using either a hashset or a bloom filter. " + + "It operates on a per-file basis rather than across an entire data set that spans multiple files.") +@WritesAttribute(attribute = "record.count", description = "The number of records processed.") +@DynamicProperty( + name = "RecordPath", + value = "An expression language statement used to determine how the RecordPath is resolved. " + + "The following variables are availble: ${field.name}, ${field.value}, ${field.type}", + description = "The name of each user-defined property must be a valid RecordPath.") +@SeeAlso(classNames = { + "org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService", + "org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer", + "org.apache.nifi.processors.standard.DetectDuplicate" +}) +public class DeduplicateRecords extends AbstractProcessor { + + private static final String FIELD_NAME = "field.name"; + private static final String FIELD_VALUE = "field.value"; + private static final String FIELD_TYPE = "field.type"; + + private volatile RecordPathCache recordPathCache; + private volatile List<String> recordPaths; + + // VALUES + + static final AllowableValue NONE_ALGORITHM_VALUE = new AllowableValue("none", "None", + "Do not use a hashing algorithm. The value of resolved RecordPaths will be combined with tildes (~) to form the unique record key. " + + "This may use significantly more storage depending on the size and shape or your data."); + static final AllowableValue MD5_ALGORITHM_VALUE = new AllowableValue(MessageDigestAlgorithms.MD5, "MD5", + "The MD5 message-digest algorithm."); + static final AllowableValue SHA1_ALGORITHM_VALUE = new AllowableValue(MessageDigestAlgorithms.SHA_1, "SHA-1", + "The SHA-1 cryptographic hash algorithm."); + static final AllowableValue SHA256_ALGORITHM_VALUE = new AllowableValue(MessageDigestAlgorithms.SHA3_256, "SHA-256", + "The SHA-256 cryptographic hash algorithm."); + static final AllowableValue SHA512_ALGORITHM_VALUE = new AllowableValue(MessageDigestAlgorithms.SHA3_512, "SHA-512", + "The SHA-512 cryptographic hash algorithm."); + + static final AllowableValue HASH_SET_VALUE = new AllowableValue("hash-set", "HashSet", + "Exactly matches records seen before with 100% accuracy at the expense of more storage usage. " + + "Stores the filter data in a single cache entry in the distributed cache, and is loaded entirely into memory during duplicate detection. " + + "This filter is preferred for small to medium data sets and offers high performance loaded into memory when this processor is running."); + static final AllowableValue BLOOM_FILTER_VALUE = new AllowableValue("bloom-filter", "BloomFilter", + "Space-efficient data structure ideal for large data sets using probability to determine if a record was seen previously. " + + "False positive matches are possible, but false negatives are not – in other words, a query returns either \"possibly in the set\" or \"definitely not in the set\". " + + "You should use this option if the FlowFile content is large and you can tolerate some duplication in the data. Uses constant storage space regardless of the record set size."); + + // PROPERTIES + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description("Specifies the Controller Service to use for reading incoming data") + .identifiesControllerService(RecordReaderFactory.class) + .required(true) + .build(); + + static final PropertyDescriptor RECORD_WRITER = new PropertyDescriptor.Builder() + .name("record-writer") + .displayName("Record Writer") + .description("Specifies the Controller Service to use for writing out the records") + .identifiesControllerService(RecordSetWriterFactory.class) + .required(true) + .build(); + + static final AllowableValue OPTION_SINGLE_FILE = new AllowableValue("single", "Single File"); + static final AllowableValue OPTION_DATA_LAKE = new AllowableValue("dataLake", "Data Lake"); + + static final PropertyDescriptor DEDUPLICATION_STRATEGY = new PropertyDescriptor.Builder() + .name("deduplication-strategy") + .displayName("Deduplication Strategy") + .description("The strategy to use for detecting and isolating duplicate records. The option for doing it " + + "across a single data file will operate in memory, whereas the one for going across the enter data lake " + + "will require a distributed map cache.") + .allowableValues(OPTION_SINGLE_FILE, OPTION_DATA_LAKE) + .defaultValue(OPTION_SINGLE_FILE.getValue()) + .required(true) + .build(); + + static final PropertyDescriptor DISTRIBUTED_MAP_CACHE = new PropertyDescriptor.Builder() + .name("distributed-map-cache") + .displayName("Distributed Map Cache client") + .description("This configuration is required when the deduplication strategy is set to 'across data lake.' The map " + + "cache will be used to check a data source such as HBase or Redis for entries indicating that a record has " + + "been processed before. This option requires a downstream process that uses PutDistributedMapCache to write " + + "an entry to the cache data source once the record has been processed to indicate that it has been handled before.") + .identifiesControllerService(DistributedMapCacheClient.class) + .required(false) + .addValidator(Validator.VALID) + .build(); + + static final PropertyDescriptor CACHE_IDENTIFIER = new PropertyDescriptor.Builder() + .name("cache-identifier") + .displayName("Cache Identifier") + .description("This option defines a record path operation to use for defining the cache identifier. It can be used " + + "in addition to the hash settings. This field will have the expression language attribute \"record.hash.value\" " + + "available to it to use with it to generate the record path operation.") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .required(false) + .addValidator(Validator.VALID) + .build(); + + static final PropertyDescriptor INCLUDE_ZERO_RECORD_FLOWFILES = new PropertyDescriptor.Builder() + .name("include-zero-record-flowfiles") + .displayName("Include Zero Record FlowFiles") + .description("When converting an incoming FlowFile, if the conversion results in no data, " + + "this property specifies whether or not a FlowFile will be sent to the corresponding relationship") + .expressionLanguageSupported(ExpressionLanguageScope.NONE) + .allowableValues("true", "false") + .defaultValue("true") + .required(true) + .build(); + + static final PropertyDescriptor RECORD_HASHING_ALGORITHM = new PropertyDescriptor.Builder() + .name("record-hashing-algorithm") + .displayName("Record Hashing Algorithm") + .description("The algorithm used to hash the combined set of resolved RecordPath values for cache storage.") + .allowableValues( + NONE_ALGORITHM_VALUE, + MD5_ALGORITHM_VALUE, + SHA1_ALGORITHM_VALUE, + SHA256_ALGORITHM_VALUE, + SHA512_ALGORITHM_VALUE + ) + .defaultValue(SHA1_ALGORITHM_VALUE.getValue()) Review comment: Good catch. FWIW, this PR combines two different implementations, so that slipped by me. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
