如何为Elasticsearch的/_cat/snapshots及/_cat/tasks API添加可读日期格式?
Elasticsearch _cat API 日期格式转换方法
针对/_cat/snapshots的start_epoch和end_epoch
要将这两个毫秒级时间戳转换为人类可读格式,有两种直接可行的方法:
- 在字段列表中指定格式
通过h参数指定字段时,使用|date:格式后缀转换epoch值,示例命令:
curl 'http://<你的ES地址>:9200/_cat/snapshots/<仓库名>?v&h=id,start_epoch|date:yyyy-MM-dd HH:mm:ss,end_epoch|date:yyyy-MM-dd HH:mm:ss,status'
替换<你的ES地址>和<仓库名>为实际值,格式可按需调整,比如yyyy-MM-dd仅显示日期。
- 结合JSON格式与客户端工具处理
如果需要更灵活的处理逻辑,可先获取JSON结构数据,再用工具(如jq)转换日期:
curl 'http://<你的ES地址>:9200/_cat/snapshots/<仓库名>?format=json' | jq '.[] | {id: .id, 开始时间: (.start_epoch | tonumber / 1000 | strftime("%Y-%m-%d %H:%M:%S")), 结束时间: (.end_epoch | tonumber / 1000 | strftime("%Y-%m-%d %H:%M:%S"))}'
针对/_cat/tasks的start_time
start_time默认是ISO格式,若要自定义显示格式,同样可通过两种方式实现:
- 字段级自定义格式
curl 'http://<你的ES地址>:9200/_cat/tasks?v&h=id,action,start_time|date:yyyy-MM-dd HH:mm:ss,running_time'
- 全局统一设置日期格式
使用-t(或--time)参数统一指定所有日期字段的输出格式:
curl 'http://<你的ES地址>:9200/_cat/tasks?v&t=yyyy-MM-dd HH:mm:ss'
内容的提问来源于stack exchange,提问作者Ricardo
相关产品推荐
相关产品推荐

