[英]ElasticSearch : TypeError: expected string or buffer?
我寫了一個簡單的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))
錯誤日志:
44d88612fea8a8f36de82e1278abb02f
<type 'str'>
Traceback (most recent call last):
File "elastic_insert.py", line 22, in <module>
es.index(index='labs', doc_type='report',id=dope,body=json.loads(json_data))
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
關於如何解決此錯誤的任何建議。我什至嘗試將m轉換為int
但它給出了另一個錯誤。
int(m)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '44d88612fea8a8f36de82e1278abb02f'
PS:ElasticSearch服務已啟動並正在運行。
問題與編號無關。 問題出在“ json_data”上。 這是一個文件流,因此您需要json.load而不是es.index中的json.loads
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.