如何使用 Python 将数据作为整数提供给 Elasticsearch?
how to feed data to Elasticseach as Integer using Python?
我正在使用这个 python 脚本将我的数据提供给 elasticsearch 6.0。如何在 Elasticsearch 中存储类型为 float 的变量 Value
?
我无法在 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)
Elasticsearch 在索引映射中定义字段类型。看起来您可能启用了动态映射,因此当您第一次将数据发送到 Elasticsearch 时,它会对数据的形状和字段类型进行有根据的猜测。
一旦设置了这些类型,它们就固定用于该索引,无论您在 python 脚本中做什么,Elasticsearch 都会继续根据这些类型解释您的数据。
要解决此问题,您需要:
- 在加载任何数据之前定义索引映射。这是更好的选择,因为它使您可以完全控制数据的解释方式。 https://www.elastic.co/guide/en/elasticsearch/reference/6.0/mapping.html
- 确保在第一次将数据发送到索引时使用正确的数据类型。这将依赖于动态映射生成,但它通常会做正确的事情。
定义索引映射是最好的选择。通常在 Kibana 中或使用 curl 一次性执行此操作,或者如果您创建大量索引,则使用模板。
但是,如果您想使用 python,您应该查看 IndicesClient
上的 create
或 put_mapping
函数
您需要使用python请求发送一个HTTPPost请求,如下:
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 替换为您的实际索引名称。删除索引后,使用上面的第一个代码示例重新创建它,包括您需要的映射。享受吧。
我正在使用这个 python 脚本将我的数据提供给 elasticsearch 6.0。如何在 Elasticsearch 中存储类型为 float 的变量 Value
?
我无法在 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)
Elasticsearch 在索引映射中定义字段类型。看起来您可能启用了动态映射,因此当您第一次将数据发送到 Elasticsearch 时,它会对数据的形状和字段类型进行有根据的猜测。
一旦设置了这些类型,它们就固定用于该索引,无论您在 python 脚本中做什么,Elasticsearch 都会继续根据这些类型解释您的数据。
要解决此问题,您需要:
- 在加载任何数据之前定义索引映射。这是更好的选择,因为它使您可以完全控制数据的解释方式。 https://www.elastic.co/guide/en/elasticsearch/reference/6.0/mapping.html
- 确保在第一次将数据发送到索引时使用正确的数据类型。这将依赖于动态映射生成,但它通常会做正确的事情。
定义索引映射是最好的选择。通常在 Kibana 中或使用 curl 一次性执行此操作,或者如果您创建大量索引,则使用模板。
但是,如果您想使用 python,您应该查看 IndicesClient
create
或 put_mapping
函数
您需要使用python请求发送一个HTTPPost请求,如下:
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 替换为您的实际索引名称。删除索引后,使用上面的第一个代码示例重新创建它,包括您需要的映射。享受吧。