适用版本: 6.8-8.9
1. 错误异常的基本描述 #
could not parse time [value]. time hour [h] is not a number 是 Elasticsearch 在解析时间字符串时抛出的异常。此错误表示时间字符串的 hour 片段(冒号前的部分)不能被解析成整数。
该错误通常发生在 hour 片段包含非数字字符时,属于时间字符串的数值解析错误。
常见现象 #
- 创建或更新 watch 时返回 HTTP 400 错误。
- 报错信息明确指出时间字符串和无法解析的
hour片段。 - 常见于
daily、weekly、monthly等 schedule 的at字段配置中。 - 如果
hour片段包含字母、空格、时区符号或其他非数字内容,就会触发此错误。
典型报错与异常栈 #
{
"error": {
"root_cause": [
{
"type": "parse_exception",
"reason": "could not parse time [ab:00]. time hour [ab] is not a number"
}
],
"type": "parse_exception",
"reason": "could not parse time [ab:00]. time hour [ab] is not a number"
},
"status": 400
}
服务端日志中可能出现类似以下内容:
[2024-01-15T10:30:00,123][WARN ][o.e.x.w.t.s.DayTimes ] [node-1] failed to parse time
ElasticsearchParseException[could not parse time [ab:00]. time hour [ab] is not a number]
at org.elasticsearch.xpack.watcher.trigger.schedule.DayTimes.parse(DayTimes.java:145)
at org.elasticsearch.xpack.watcher.trigger.schedule.DailySchedule.parse(DailySchedule.java:89)
2. 为什么会发生这个错误 #
Watcher 的时间解析器在切出 hour 字符串(hrStr)后,会调用 Integer.parseInt(hrStr) 将其转换为整数。只要 hour 片段包含非数字字符,就会抛出此异常。
常见原因包括:
hour片段包含字母:例如ab:00、twelve:00等。hour片段包含空格:例如12:00、12 :00等。hour片段包含时区符号:例如12+08:00、12UTC:00等。hour片段包含其他非数字内容:例如12.0:00(小数点)、12-:00(破折号)等。- 模板渲染问题:使用 Mustache 或 script 模板时,渲染结果可能包含非数字字符。
- 时区后缀:例如
12:00+08:00(整个字符串包含时区,应该只是12:00)。
3. 如何排查和解决这个异常和解决这个异常 #
排查步骤 #
查看原始时间文本和报错中的
hour片段:从错误信息中提取时间值(could not parse time [value])和无法解析的hour片段(time hour [h])。检查 Watch 中的 schedule 配置:
# 获取 watch 配置
GET _watcher/watch/<watch_id>?pretty
# 检查 trigger 部分的 schedule 中的 at 字段
- 检查是否混入字母、空格、时区符号或其他非数字内容:
# 正确的时间格式示例
PUT _watcher/watch/<watch_id>
{
"trigger": {
"schedule": {
"daily": {
"at": ["12:00", "18:00"] # 正确:hour 是纯数字
}
}
},
...
}
合法的时间格式必须是:
hh:mm格式:冒号前是纯数字(hour部分)hour部分:0-23 的数字,可以是 1 位或 2 位(如9:00或09:00)- 不能有字母、空格、时区符号等:只能是数字
- 若该值由模板生成,确认
hour部分未被前缀文本污染:
# 错误:模板渲染后 hour 包含字母
# 假设 ctx.hour = "ab"
"at": ["{{ctx.hour}}:00"] # 渲染后变成 "ab:00",错误
# 正确:确保 ctx.hour 是纯数字
# 假设 ctx.hour = 12
"at": ["{{ctx.hour}}:00"] # 渲染后变成 "12:00",正确
排查时需要注意的问题 #
- 注意
hour部分必须是纯数字,不能包含字母、空格、时区符号等。 - 如果使用了模板变量,确保变量渲染后只产生数字字符。
- 检查是否有不可见字符(如制表符、换行符)混入时间字符串。
4. 如何解决这个错误 #
常用修复思路 #
- 让
hour片段只保留数字:
# 修复前(错误:hour 包含字母)
PUT _watcher/watch/<watch_id>
{
"trigger": {
"schedule": {
"daily": {
"at": ["ab:00"] # 错误:hour 是 "ab"
}
}
}
}
# 修复后(正确:hour 是纯数字)
PUT _watcher/watch/<watch_id>
{
"trigger": {
"schedule": {
"daily": {
"at": ["12:00"] # 正确:hour 是 "12"
}
}
}
}
- 不要把日期、时区或其他字段拼到
hour前面:
# 错误:hour 包含时区
"at": ["12+08:00"] # 错误
# 正确:只包含小时和分钟
"at": ["12:00"] # 正确
- 在业务侧先拆分并校验
hour/minute:
# 在生成配置前,先校验 hour 是纯数字
# 伪代码示例
hour = extract_hour(time_string)
if not hour.isdigit():
raise Error("Hour must be a number")
后续注意事项与推荐建议 #
- 在编写时间配置时,始终确保
hour部分是纯数字(0-23)。 - 对动态生成的时间字符串进行格式验证,确保
hour部分只包含数字字符。 - 在测试环境验证 schedule 配置,特别是在使用模板或程序生成配置时。
借助 INFINI 产品提升排障效率 #
- INFINI Console 提供可视化的 Watch 编辑和验证功能,可以在保存前检查时间格式的合法性,自动检测
hour不是数字的错误。 - INFINI Gateway 可以记录 Watcher API 的完整请求内容,帮助捕获时间配置的详细错误上下文,并通过流量回放验证修复方案。
- 通过 INFINI Console 的 Watch 测试功能,可以在不执行完整 Watch 的情况下验证时间格式的合法性。
5. 小结 #
could not parse time [value]. time hour [h] is not a number 是一个时间字符串数值解析错误,表示 hour 片段不能被解析成整数。解决此问题的关键是:让 hour 片段只保留数字(如 12:00 中的 12),不要把日期、时区或其他字段拼到 hour 前面。
通过 INFINI Console 的配置验证功能和 INFINI Gateway 的请求捕获能力,可以更高效地定位和修复时间格式问题。
相关错误 #
- could not parse time time minute is not a number - 如何解决此 Elasticsearch 异常
- could not parse time time format must be in the form of hh mm - 如何解决此 Elasticsearch 异常
- could not parse time expected string number value or an object but found - 如何解决此 Elasticsearch 异常
- could not parse schedule invalid value for - 如何解决此 Elasticsearch 异常
- failed to parse field - 如何解决此 Elasticsearch 异常
参考文档 #
附:日志上下文 #
下面保留当前页面中的源码或日志片段,便于继续结合异常调用栈定位问题:
try {
hour = new int[] { Integer.parseInt(hrStr) };
} catch (NumberFormatException nfe) {
throw new ElasticsearchParseException("could not parse time [{}]. time hour [{}] is not a number", time, hrStr);
}





