2

我正在尝试将索引模式、可视化和仪表板从一个 Kibana 移动到另一个。它们在不同的 docker 中运行,监听不同的端口。在 saved_objects API 中有导入和导出。天真地假设我尝试了下面的代码export可以消耗产生的东西:import

import requests as req

header = {'Content-Type': 'application/json', 'kbn-xsrf': 'true'}

request_json_index_pattern = '''
{
  "type": "index-pattern"
}
'''

index_patterns = req.post(kibana_host+':5601/api/saved_objects/_export',
                    data=request_json_index_pattern,
                    headers = header,
                    auth = (elastic_user, elastic_password))


res = req.post(kibana_host+':5602/api/saved_objects/_import',
                    data=index_patterns.content,
                    headers = header,
                    auth = (elastic_user, elastic_password))



print(res.content)

但我得到的只是

{'statusCode': 415, 'error': 'Unsupported Media Type', 'message': 'Unsupported Media Type'}

所有这一切,尽管这index_patterns.content是一个格式良好的 ndjson——我可以用ndjson.loads.

我错过了什么?

(顺便说一句,我的源 Kibana 实例是 7.3.1 而目标是 7.4.0。这可能是问题吗?)

4

2 回答 2

2

我自己也遇到了这个。API 需要换行符分隔的_importjson (ndjson),而不是 json。

https://www.elastic.co/guide/en/kibana/current/saved-objects-api-import.html#_examples_3

我发现的一个稍微麻烦的解决方法是使用仪表板 import api: /api/kibana/dashboards/import。它似乎导入了所有保存的对象类型(不仅仅是仪表板)。

于 2020-04-01T17:08:43.163 回答
1

我有同样的问题,这就是我解决的方法:

url="" # your kibana host + path 
# i.e kibana_host+':5602/api/saved_objects/_import
# in your question

params=None # or your params
username = "" # kibana username
password = "" # kibana password

# currently min requires headers are: "kbn-xsrf" - set to anything
# in future you may need: Content-Type: application/x-ndjson
headers = {"kbn-xsrf": "true"}

# json = ## your json object
# assuming you have a json object - you need to convert it to a string and format as ndjson

# removed all newlines from the string, then add a couple to the end
# i also removed spaces too, but i dont think this is required

data = json_dumps(json).replace('\n', '').replace(" ", "")+"\n\n"

# if you dont have a json object and just have a string, which may be an ndjson string 
# i.e. {}{}{} or {}\n{}\n{}\n
#  - remove the json_dumps

# myString = ## your string var
# data = myString.replace('\n', '').replace(" ", "")+"\n\n"

# now make it a file - you need to define a filename with an ndjson extension - or it will get rejected

files = {'file': ('request.ndjson', data) }    

response = requests.post(url=url, params=params, files=files, 
                            auth=HTTPBasicAuth(username, password), 
                            headers=headers)

显然,您需要添加所需的相关导入,作为开始:

import requests
from requests.auth import HTTPBasicAuth
import json
from json import dumps as json_dumps, loads as json_loads
于 2020-09-17T10:39:53.447 回答