我尝试将一些数据(JSON格式)从mongoDB发布到elasticSearch。
这是我的代码:
首先,我从mongoDB中提取数据,然后尝试将每个“文档”发布到我的弹性文件中,其地址为“ http://127.0.0.1:9200”。我添加了一个额外的函数“ my converter(o)”,以使日期时间对象可序列化。
from pymongo import MongoClient
import requests
import json
import datetime
mongo_host = 'localhost'
mongo_port = '27017'
client=MongoClient(mongo_host+':'+mongo_port)
db = client.cvedb
collection=db['cves']
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
try: db.command("serverStatus")
except Exception as e: print(e)
else: print("You are connected!")
cursor = collection.find({})
for document in cursor:
headers={"content-type":"application/x-www-form-urlencoded"}
url_base = 'http://127.0.0.1:9200'
data=document
data_parsed=(json.dumps(data, default = myconverter))
print("#####")
print("#####")
print(data_parsed)
print("#####")
print("#####")
req = requests.post(url_base,json=data_parsed)
print (req.status_code)
print (req.text)
print("####")
print("#####")
print (req.status_code)
print("#####")
print("####")
client.close()
但是,当我访问我的ES时,地址如下:“ http://127.0.0.1:9200/_cat/indices”,则什么也没有出现。这是我在终端机上得到的:
{"Modified": "2008-09-09 08:35:18.883000", "impact": {"confidentiality": "PARTIAL", "integrity": "PARTIAL", "availability": "PARTIAL"}, "summary": "KDE K-Mail allows local users to gain privileges via a symlink attack in temporary user directories.", "cvss": 4.6, "access": {"vector": "LOCAL", "authentication": "NONE", "complexity": "LOW"}, "vulnerable_configuration": ["cpe:2.3:a:kde:k-mail:1.1"], "_id": null, "references": ["http://www.redhat.com/support/errata/RHSA1999015_01.html", "http://www.securityfocus.com/bid/300"], "Published": "2000-01-04 00:00:00", "id": "CVE-1999-0735", "cvss-time": "2004-01-01 00:00:00", "vulnerable_configuration_cpe_2_2": ["cpe:/a:kde:k-mail:1.1"]}
#####
#####
400
No handler found for uri [/] and method [POST]
####
#####
400
#####
####
#####
#####
我尝试关注有关同一问题的tome帖子,但对我无济于事。 知道为什么它不起作用吗?
答案 0 :(得分:1)
有两个问题
url_base
缺少索引和类型
url_base ='http://127.0.0.1:9200/index/type'
headers
必须为application/json
(您尚未看到此错误,但是一旦解决了上述问题,也会收到此错误。
headers = {“ content-type”:“ application / json”}