grafana elasticsearch date类型问题
- 大致的数据格式
{
"createTime": 1484967199,
"ip": "localhost",
"appId": "10000",
"threadName": "Thread-acceptor-1",
"level": "info",
"type": "error",
"tag": "tag1",
"module": "module1",
"detail": "some description" }
从kafka读取数据后由storm计算后直接将es客户端写入es。
使用grafana配置数据源,其中使用自定义的字段createTime。
配置grafana的dashboard的panel,结果报错,
Invalid number format [epoch_millis#]。- 是时间类型的错误,查下es字段的mapping,
http://132.122.252.22:9200/flume-index/_mapping
{
"flume-index": { "mappings": { "distributed-log": { "properties": { "appId": { "type": "string" }, "createTime": { "type": "long" }, "detail": { "type": "string" }, "ip": { "type": "string" }, "level": { "type": "string" }, "module": { "type": "string" }, "tag": { "type": "string" }, "threadName": { "type": "string" }, "type": { "type": "string" } } } } } }
没有指定es的mapping,所以根据java类型,当storm推数据到es时则会当做long型,具体格式如下,这导致grafana根据createTime查询错误,需要更改createTime的mapping。
- 先删除原来的索引,
curl -XDELETE 'localhost:9200/flume-index/?pretty'
- 再重新建立新索引,且指定createTime字段为date类型,且格式为默认即可。
curl -XPUT 'localhost:9200/flume-index/?pretty' -d ' { "mappings": { "distributed-log": { "properties": { "appId": { "type": "string" }, "createTime": { "type": "date" }, "detail": { "type": "string" }, "ip": { "type": "string" }, "level": { "type": "string" }, "module": { "type": "string" }, "tag": { "type": "string" }, "threadName": { "type": "string" }, "type": { "type": "string" } } } } } '
再用json的字符串格式传入后,es即可以通过这个mapping进行转换。grafana也可以根据时间查出数据来。
如果允许也可以直接使用es的_timestamp字段,但它并非准确的数据生成时间。es默认没有开启timestamp的记录,可以用下面开启,即有了_timestamp字段。
curl -XPOST localhost:9200/flume-index -d ' { "mappings": { "_default_": { "_timestamp": { "enabled": true } } } } '