NIFI-8005 Added warning message for Excel sheets not found

This closes #6897

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
dan-s1 2023-01-27 14:48:14 +00:00 committed by exceptionfactory
parent afdf49b3cd
commit d46aa89788
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
2 changed files with 19 additions and 4 deletions

View File

@ -23,10 +23,13 @@ import java.io.OutputStreamWriter;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVPrinter;
@ -96,7 +99,7 @@ public class ConvertExcelToCSVProcessor
.displayName("Sheets to Extract") .displayName("Sheets to Extract")
.description("Comma separated list of Excel document sheet names that should be extracted from the excel document. If this property" + .description("Comma separated list of Excel document sheet names that should be extracted from the excel document. If this property" +
" is left blank then all of the sheets will be extracted from the Excel document. The list of names is case in-sensitive. Any sheets not " + " is left blank then all of the sheets will be extracted from the Excel document. The list of names is case in-sensitive. Any sheets not " +
"specified in this value will be ignored.") "specified in this value will be ignored. A bulletin will be generated if a specified sheet(s) are not found.")
.required(false) .required(false)
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
@ -240,25 +243,35 @@ public class ConvertExcelToCSVProcessor
.split(desiredSheetsDelimited, DESIRED_SHEETS_DELIMITER); .split(desiredSheetsDelimited, DESIRED_SHEETS_DELIMITER);
if (desiredSheets != null) { if (desiredSheets != null) {
Map<String, Boolean> sheetsFound = Arrays.stream(desiredSheets)
.collect(Collectors.toMap(key -> key, value -> Boolean.FALSE));
while (iter.hasNext()) { while (iter.hasNext()) {
InputStream sheet = iter.next(); InputStream sheet = iter.next();
String sheetName = iter.getSheetName(); String sheetName = iter.getSheetName();
for (int i = 0; i < desiredSheets.length; i++) { for (String desiredSheet : desiredSheets) {
//If the sheetName is a desired one parse it //If the sheetName is a desired one parse it
if (sheetName.equalsIgnoreCase(desiredSheets[i])) { if (sheetName.equalsIgnoreCase(desiredSheet)) {
ExcelSheetReadConfig readConfig = new ExcelSheetReadConfig(columnsToSkip, firstRow, sheetName, formatValues, sst, styles); ExcelSheetReadConfig readConfig = new ExcelSheetReadConfig(columnsToSkip, firstRow, sheetName, formatValues, sst, styles);
handleExcelSheet(session, flowFile, sheet, readConfig, csvFormat); handleExcelSheet(session, flowFile, sheet, readConfig, csvFormat);
sheetsFound.put(desiredSheet, Boolean.TRUE);
break; break;
} }
} }
} }
String sheetsNotFound = sheetsFound.entrySet().stream()
.filter(entry -> !entry.getValue())
.map(Map.Entry::getKey)
.collect(Collectors.joining(","));
if (!sheetsNotFound.isEmpty()) {
getLogger().warn("Excel sheets not found: {}", sheetsNotFound);
}
} else { } else {
getLogger().debug("Excel document was parsed but no sheets with the specified desired names were found."); getLogger().debug("Excel document was parsed but no sheets with the specified desired names were found.");
} }
} else { } else {
//Get all of the sheets in the document. //Get all the sheets in the document.
while (iter.hasNext()) { while (iter.hasNext()) {
InputStream sheet = iter.next(); InputStream sheet = iter.next();
String sheetName = iter.getSheetName(); String sheetName = iter.getSheetName();

View File

@ -35,6 +35,7 @@ import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -521,6 +522,7 @@ public class ConvertExcelToCSVProcessorTest {
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.SUCCESS, 0); //We aren't expecting any output to success here because the sheet doesn't exist testRunner.assertTransferCount(ConvertExcelToCSVProcessor.SUCCESS, 0); //We aren't expecting any output to success here because the sheet doesn't exist
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.ORIGINAL, 1); testRunner.assertTransferCount(ConvertExcelToCSVProcessor.ORIGINAL, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.FAILURE, 0); testRunner.assertTransferCount(ConvertExcelToCSVProcessor.FAILURE, 0);
assertFalse(testRunner.getLogger().getWarnMessages().isEmpty());
} }
/** /**