简体   繁体   English

ElasticSearch:TypeError:预期的字符串还是缓冲区?

[英]ElasticSearch : TypeError: expected string or buffer?

I wrote a simple python script to put the JSON file to Elasticsearch.I want to store it based on the id field I am extracting from the JSON file. 我写了一个简单的python脚本将JSON文件放到Elasticsearch中,我想根据我从JSON文件中提取的id字段来存储它。 But when I try to insert into elastic search.It raises an error TypeError: expected string or buffer 但是当我尝试插入弹性搜索时,它会引发错误TypeError: expected string or buffer

Here is the code I am working on... 这是我正在处理的代码...

#! /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))

Error Log: 错误日志:

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

Any suggestions on how to solve this error.I even tried to convert the m to int but it gave another error. 关于如何解决此错误的任何建议。我什至尝试将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 service is up and running. PS:ElasticSearch服务已启动并正在运行。

The problem is not related to the id. 问题与编号无关。 the problem is with "json_data". 问题出在“ json_data”上。 it is a file stream so you need json.load and not json.loads in your es.index 这是一个文件流,因此您需要json.load而不是es.index中的json.loads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM