我编写了一个简单的python脚本将JSON文件放到Elasticsearch中,我想根据从JSON文件中提取的id字段来存储它。
但当我试着把它插入弹性体搜索。它引发错误TypeError: expected string or buffer
这是我正在处理的代码。。。在
#! /usr/bin/python
import requests
from elasticsearch import Elasticsearch
import json
es = Elasticsearch([{'host':'localhost','port':9200}])
r = requests.get('http://127.0.0.1:9200')
i = 1
if r.status_code == 200:
with open('d1.json') as json_data:
d = json.load(json_data)
for i in d['tc'][0]['i]['f']['h']:
if i['name'] == 'm':
m = i['value']
dope=str(m)
print dope
print type(dope)
#print type(md5)
es.index(index='lab', doc_type='report',id=dope,body=json.loads(json_data))
错误日志:
^{pr2}$关于如何解决这个错误有什么建议吗?我甚至试图将m转换成int
,但它给出了另一个错误。在
int(m)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '44d88612fea8a8f36de82e1278abb02f'
注:ElasticSearch服务已启动并运行。在
问题与id无关。问题在于 “json_数据”。它是一个文件流,因此您需要json.load文件而不是json.loads在你的es.索引在
相关问题 更多 >
编程相关推荐