可以在ConvertExcelToCSVProcessor中使用自定义Groovy脚本进行转换。
以下是一个示例脚本:
import org.apache.nifi.logging.ComponentLog
import java.text.SimpleDateFormat
import java.util.Date
// 获取组件日志
ComponentLog logger = log
// 定义日期格式
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy")
SimpleDateFormat outFormat = new SimpleDateFormat("yy")
// 获取原始日期字符串
String yearStr = flowFile."yyyy".toString()
try {
// 将日期字符串转换为日期对象
Date year = inFormat.parse(yearStr)
// 将日期对象转换为目标日期字符串
String shortYear = outFormat.format(year)
// 将目标日期字符串添加到flowfile中
flowFile."yy" = shortYear
} catch (Exception e) {
// 如果无法解析日期,则记录错误日志并传递flowfile
logger.error("无法将日期${yearStr}转换为${outFormat.toPattern()}", e)
session.transfer(flowFile, REL_FAILURE)
}
在使用该脚本时,需要将输入流文件的年份列的字段名称设置为"yyyy",将目标输出流文件的年份列的字段名称设置为"yy"。
使用该脚本,您可以在ConvertExcelToCSVProcessor中自动将年份格式从'yyyy'更改为'yy',而无需手动更改Excel文件或CSV文件。