1

我正在使用这个 python 脚本将我的数据提供给 elasticsearch 6.0。如何Value在 Elasticsearch 中存储浮点类型的变量?我无法在 Kibana 中使用度量选项进行可视化,因为所有数据都自动存储为字符串

from elasticsearch import Elasticsearch

Device=""
Value=""
for key, value in row.items():  
    Device = key
    Value = value
    print("Dev",Device,  "Val:", Value)                     
    doc = {'Device':Device, 'Measure':Value ,  'Sourcefile':filename}
    print('   doc:    ', doc)
    es.index(index=name, doc_type='trends', body=doc)

谢谢

编辑:

在@Saul 的建议之后,我可以使用以下代码解决此问题:

import os,csv
import time
from elasticsearch import Elasticsearch
#import pandas as pd
import requests

Datum = time.strftime("%Y-%m-%d_")
path = '/home/pi/Desktop/Data'


os.chdir(path)
name = 'test'
es = Elasticsearch() 

    #'Time': time ,
#url = 'http://localhost:9200/index_name?pretty'
doc = {
  "mappings": {
    "doc": { 
      "properties": { 
        "device":    { "type": "text"  }, 
        "measure":     { "type": "text"  }, 
        "age":      { "type": "integer" },  
        "created":  {
          "type":   "date", 
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
#headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
#r = requests.post(url, data=json.dumps(data), headers=headers)
r= es.index(index=name, doc_type='trends', body=doc)

print(r)
4

2 回答 2

0

Elasticsearch 在索引映射中定义字段类型。看起来您可能启用了动态映射,因此当您第一次向 Elasticsearch 发送数据时,它会对数据的形状和字段类型进行有根据的猜测。

一旦设置了这些类型,它们就会针对该索引进行固定,无论您在 Python 脚本中做什么,Elasticsearch 都会继续根据这些类型解释您的数据。

要解决此问题,您需要:

定义索引映射是最好的选择。在 Kibana 或 curl 中,或者如果您使用模板创建大量索引时,通常会这样做。但是,如果你想使用 python,你应该看看createorput_mapping函数IndicesClient

于 2017-12-01T23:44:44.453 回答
0

你需要使用python请求发送一个HTTP Post请求,如下:

url = "http://localhost:9200/index_name?pretty”
data = {
  "mappings": {
    "doc": { 
      "properties": { 
        "title":    { "type": "text"  }, 
        "name":     { "type": "text"  }, 
        "age":      { "type": "integer" },  
        "created":  {
          "type":   "date", 
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)

请将 URL 中的 index_name 替换为您在 elasticsearch 引擎中定义的索引名称。

如果要在重新创建索引之前删除索引,请执行以下操作:

url = "http://localhost:9200/index_name”
data = { }
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.delete(url, data=json.dumps(data), headers=headers)

请将 URL 中的 index_name 替换为您的实际索引名称。删除索引后,使用上面的第一个代码示例重新创建它,包括您需要的映射。享受。

于 2017-12-04T04:15:03.080 回答