Apache NiFi向Elasticsearch写入数据时如何指定动态索引名
实现方案
步骤1:提取JSON中的timestamp字段为流文件属性
使用EvaluateJsonPath处理器完成字段提取,配置规则如下:
- 新增自定义属性
event_timestamp,值填写$.timestamp Destination属性选择flowfile-attributes,将提取的13位毫秒时间戳存储为流文件属性备用
步骤2:构造符合要求的索引名称
在写入Elasticsearch的对应处理器(如PutElasticsearchHttp、PutElasticsearchRecord等)的Index配置项中,填写如下NiFi表达式即可:
myindex-${event_timestamp:toDate('unix_epoch_millis'):format('yyyy.MM.dd')}
你示例中的时间戳1625579799000通过该表达式转换后,最终生成的索引名为myindex-2021.07.06,完全匹配预期格式。
可选兜底配置
如果存在部分流文件缺少timestamp字段的场景,可以添加默认值规则避免写入失败,参考表达式:
myindex-${event_timestamp:isNull():ifElse(${now():format('yyyy.MM.dd')}, ${event_timestamp:toDate('unix_epoch_millis'):format('yyyy.MM.dd'))}
上述表达式会在timestamp字段为空时,自动使用系统当前时间生成索引名。
内容的提问来源于stack exchange,提问作者lucas kim
相关产品推荐
相关产品推荐

