Skip to content

Commit

Permalink
NIFI-14141 Accept Expression Language statement in GenerateFlowFile, …
Browse files Browse the repository at this point in the history
…File Size property allowing FlowFiles of variable sizes to be generated. Included some examples of EL statements in Additional Details for GenerateFlowFile.
  • Loading branch information
markobean committed Jan 9, 2025
1 parent b80a673 commit 0376b15
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public class GenerateFlowFile extends AbstractProcessor {
.description("The size of the file that will be used")
.required(true)
.defaultValue("0B")
.addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
.build();
public static final PropertyDescriptor BATCH_SIZE = new PropertyDescriptor.Builder()
.name("Batch Size")
Expand Down Expand Up @@ -187,11 +188,13 @@ protected Collection<ValidationResult> customValidate(ValidationContext validati
+ "text and Unique FlowFiles must be false.").build());
}

results.add(StandardValidators.DATA_SIZE_VALIDATOR.validate("File Size", validationContext.getProperty(FILE_SIZE).evaluateAttributeExpressions().getValue(), validationContext));

return results;
}

private byte[] generateData(final ProcessContext context) {
final int byteCount = context.getProperty(FILE_SIZE).asDataSize(DataUnit.B).intValue();
final int byteCount = context.getProperty(FILE_SIZE).evaluateAttributeExpressions().asDataSize(DataUnit.B).intValue();

final Random random = new Random();
final byte[] array = new byte[byteCount];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
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.
-->

# GenerateFlowFile

This processor can be configured to generate variable-sized FlowFiles. The `File Size` property accepts both a literal
value, e.g. "1 KB", and Expression Language statements. In order to create FlowFiles of variable sizes, the Expression
Language function `random()` can be used. For example, `${random():mod(101)}` will generate values between 0 and 100,
inclusive. A data size label, e.g. B, KB, MB, etc., must be included in the Expression Language statement since the
`File Size` property holds a data size value. The table below shows some examples.

| File Size Expression Language Statement | File Sizes Generated (values are inclusive) |
|--------------------------------------------|---------------------------------------------|
| ${random():mod(101)}b | 0 - 100 bytes |
| ${random():mod(101)}mb | 0 - 100 MB |
| ${random():mod(101):plus(20)} B | 20 - 120 bytes |
| ${random():mod(71):plus(30):append("KB")} | 30 - 100 KB |

See the Expression Language Guide for more details on the `random()` function.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Unit tests for the GenerateFlowFile processor.
*/
Expand Down Expand Up @@ -73,4 +78,30 @@ public void testDynamicPropertiesToAttributes() {
generatedFlowFile.assertAttributeEquals("mime.type", "application/text");
}

@Test
public void testExpressionLanguageSupport() {
TestRunner runner = TestRunners.newTestRunner(new GenerateFlowFile());
runner.setProperty(GenerateFlowFile.FILE_SIZE, "${random():mod(10):plus(1)}B"); // represents a range of 1-10 bytes, inclusive
runner.setProperty(GenerateFlowFile.UNIQUE_FLOWFILES, "true");
runner.assertValid();

// Execute multiple times to ensure adequate distribution of file sizes
runner.run(1000);

runner.assertTransferCount(GenerateFlowFile.SUCCESS, 1000);
List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(GenerateFlowFile.SUCCESS);
Map<Long, Integer> fileSizeDistribution = new HashMap<>();
flowFiles.forEach(ff -> {
long fileSize = ff.getSize();
fileSizeDistribution.put(fileSize, fileSizeDistribution.getOrDefault(fileSize, 0) + 1);
});
long minSize = fileSizeDistribution.keySet().stream().min(Long::compareTo).orElse(-1L);
long maxSize = fileSizeDistribution.keySet().stream().max(Long::compareTo).orElse(-1L);

// Assert all file sizes fall within the range and that there exists more than one unique file size value
Assertions.assertTrue(minSize > 0 && minSize < maxSize);
Assertions.assertTrue(maxSize <= 10);
}


}

0 comments on commit 0376b15

Please sign in to comment.