首页 > 其他分享 >调用kibana API操作,导入导出仪表板和索引

调用kibana API操作,导入导出仪表板和索引

时间:2023-04-06 19:44:24浏览次数:37  
标签:kibana API 仪表板 setRequestProperty import new kibanaExportConn def kibanaImportCo

导出导出ChatGPT:

Java

 1 import java.io.*;
 2 import java.net.*;
 3 import org.apache.commons.io.IOUtils;
 4 import org.json.JSONObject;
 5 
 6 public class ExportAndImportKibanaDashboardAndIndex {
 7     public static void main(String[] args) throws Exception {
 8         String elasticsearchUrl = "http://localhost:9200";
 9         String kibanaExportUrl = "http://localhost:5601/api/kibana/dashboards/export?dashboard={dashboardId}";
10         String kibanaImportUrl = "http://localhost:5601/api/kibana/dashboards/import";
11         String dashboardId = "mydashboard";
12         String indexName = "myindex";
13         String dashboardTitle = "My Dashboard";
14         String kibanaCookie = "kibanaCookie";
15 
16         // Export index mapping
17         URL elasticsearch = new URL(elasticsearchUrl);
18         HttpURLConnection elasticsearchConn = (HttpURLConnection) elasticsearch.openConnection();
19         elasticsearchConn.setRequestMethod("GET");
20         elasticsearchConn.setRequestProperty("Content-Type", "application/json");
21         int elasticsearchResponseCode = elasticsearchConn.getResponseCode();
22         if (elasticsearchResponseCode == HttpURLConnection.HTTP_OK) {
23             String indexMapping = IOUtils.toString(elasticsearchConn.getInputStream(), "UTF-8");
24             File indexFile = new File("index_" + indexName + ".json");
25             FileUtils.writeStringToFile(indexFile, indexMapping, "UTF-8");
26         }
27 
28         // Export dashboard
29         URL kibanaExport = new URL(kibanaExportUrl.replace("{dashboardId}", dashboardId));
30         HttpURLConnection kibanaExportConn = (HttpURLConnection) kibanaExport.openConnection();
31         kibanaExportConn.setRequestMethod("GET");
32         kibanaExportConn.setRequestProperty("Content-Type", "application/json");
33         kibanaExportConn.setRequestProperty("kbn-xsrf", "true");
34         kibanaExportConn.setRequestProperty("Cookie", kibanaCookie);
35         int kibanaExportResponseCode = kibanaExportConn.getResponseCode();
36         if (kibanaExportResponseCode == HttpURLConnection.HTTP_OK) {
37             String dashboardJson = IOUtils.toString(kibanaExportConn.getInputStream(), "UTF-8");
38             JSONObject dashboard = new JSONObject(dashboardJson);
39             String dashboardTitleEscaped = dashboardTitle.replaceAll("[^\\w\\d]", "_");
40             File dashboardFile = new File("dashboard_" + dashboardId + "_" + dashboardTitleEscaped + ".json");
41             FileUtils.writeStringToFile(dashboardFile, dashboardJson, "UTF-8");
42 
43             // Import dashboard
44             URL kibanaImport = new URL(kibanaImportUrl);
45             HttpURLConnection kibanaImportConn = (HttpURLConnection) kibanaImport.openConnection();
46             kibanaImportConn.setDoOutput(true);
47             kibanaImportConn.setRequestMethod("POST");
48             kibanaImportConn.setRequestProperty("Content-Type", "application/json");
49             kibanaImportConn.setRequestProperty("kbn-xsrf", "true");
50             kibanaImportConn.setRequestProperty("Cookie", kibanaCookie);
51             OutputStreamWriter writer = new OutputStreamWriter(kibanaImportConn.getOutputStream());
52             writer.write(dashboardJson);
53             writer.flush();
54             int kibanaImportResponseCode = kibanaImportConn.getResponseCode();
55             if (kibanaImportResponseCode == HttpURLConnection.HTTP_OK) {
56                 System.out.println("Dashboard imported successfully.");
57             }
58         }
59     }
60 }

 

Groovy

@Grab(group='commons-io', module='commons-io', version='2.6')
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.13')
import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.json.JSONObject

def elasticsearchUrl = "http://localhost:9200"
def kibanaExportUrl = "http://localhost:5601/api/kibana/dashboards/export?dashboard={dashboardId}"
def kibanaImportUrl = "http://localhost:5601/api/kibana/dashboards/import"
def dashboardId = "mydashboard"
def indexName = "myindex"
def dashboardTitle = "My Dashboard"
def kibanaCookie = "kibanaCookie"

// Export index mapping
def elasticsearch = new URL(elasticsearchUrl)
def elasticsearchConn = elasticsearch.openConnection()
elasticsearchConn.setRequestMethod("GET")
elasticsearchConn.setRequestProperty("Content-Type", "application/json")
def elasticsearchResponseCode = elasticsearchConn.responseCode
if (elasticsearchResponseCode == HttpURLConnection.HTTP_OK) {
    def indexMapping = IOUtils.toString(elasticsearchConn.getInputStream(), "UTF-8")
    def indexFile = new File("index_${indexName}.json")
    FileUtils.writeStringToFile(indexFile, indexMapping, "UTF-8")
}

// Export dashboard
def kibanaExport = new URL(kibanaExportUrl.replace("{dashboardId}", dashboardId))
def kibanaExportConn = kibanaExport.openConnection() as HttpURLConnection
kibanaExportConn.setRequestMethod("GET")
kibanaExportConn.setRequestProperty("Content-Type", "application/json")
kibanaExportConn.setRequestProperty("kbn-xsrf", "true")
kibanaExportConn.setRequestProperty("Cookie", kibanaCookie)
def kibanaExportResponseCode = kibanaExportConn.responseCode
if (kibanaExportResponseCode == HttpURLConnection.HTTP_OK) {
    def dashboardJson = IOUtils.toString(kibanaExportConn.getInputStream(), "UTF-8")
    def dashboard = new JSONObject(dashboardJson)
    def dashboardTitleEscaped = dashboardTitle.replaceAll("[^\\w\\d]", "_")
    def dashboardFile = new File("dashboard_${dashboardId}_${dashboardTitleEscaped}.json")
    FileUtils.writeStringToFile(dashboardFile, dashboardJson, "UTF-8")

    // Import dashboard
    def kibanaImport = new URL(kibanaImportUrl)
    def kibanaImportConn = kibanaImport.openConnection() as HttpURLConnection
    kibanaImportConn.setDoOutput(true)
    kibanaImportConn.setRequestMethod("POST")
    kibanaImportConn.setRequestProperty("Content-Type", "application/json")
    kibanaImportConn.setRequestProperty("kbn-xsrf", "true")
    kibanaImportConn.setRequestProperty("Cookie", kibanaCookie)
    def writer = new OutputStreamWriter(kibanaImportConn.getOutputStream())
    writer.write(dashboardJson)
    writer.flush()
    def kibanaImportResponseCode = kibanaImportConn.responseCode
    if (kibanaImportResponseCode == HttpURLConnection.HTTP_OK) {
        println "Dashboard imported successfully."
    }
}

  

 

标签:kibana,API,仪表板,setRequestProperty,import,new,kibanaExportConn,def,kibanaImportCo
From: https://www.cnblogs.com/altbswing/p/17293947.html

相关文章

  • 洛谷P1552 [APIO2012] 派遣 题解 左偏树
    题目链接:https://www.luogu.com.cn/problem/P1552题目大意:每次求子树中薪水和不超过\(M\)的最大节点数。解题思路:使用左偏树维护一个大根堆。首先定义一个Node的结构体:structNode{ints[2],c,sz,dis;longlongsum;Node(){};Node(int_c){s......
  • 分享:包括 AI 绘画在内的超齐全免费可用的API 大全
    AI绘画已经火出圈了,你还不知道哪里可以用嘛?我给大家整理了超级齐全的免费可用API,包括AI绘画在内,有需要的小伙伴赶紧收藏了。 AI绘画/AI作画类AI绘画(apispace.com):通过AI生成图片,包括图生文、文生图等。AI绘画文生图API:输入文本描述,生成符合文本描述的图像,可用于......
  • Android Api版本对照表
     Android版本ApiAPIAndroid13.0(T)32Android12.0(S)31Android11.0(R)30Android10.0(Q)29Android9.0(Pie)28Android8.1(Oreo)27Android8.0(Oreo)26Android7.1(Nougat)25Android7.0(Nougat)24Android6.0(Marshmallow)23......
  • 苹果CMS V10 API接口相关
    苹果CMSV10内置API接口网上查了一下没啥结果,都是采集和第三方的。所以看了下源码,提取出来的内内置接口如下,比较少,而且缺乏一些字段,还是要自己写才行,供参考。苹果CMSV10API接口相关影片接口url:/api.php/provide/vod/可用于获取分类、列表和详情ac:模式(videolist或detail详......
  • 1688关键字搜索新品数据API接口(item_search_new-按关键字搜索新品数据)
    1688关键字搜索新品数据API接口(item_search_new-按关键字搜索新品数据)代码接口教程如下:公共参数名称类型必须描述key String 是 调用key(必须以GET方式拼接在URL中)secret String 是 调用密钥api_name String 是 API接口名称(包括在请求地址中)[item_search,item_get,item_search......
  • C# WebApi - Basic验证实现;
    1.Filter文件夹下添加如下BasicAuthorizeAttribute类;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Web;usingSystem.Web.Http;usingSystem.Web.Security;namespaceABBPMP.WebApi.Filter{///<summary>///自定......
  • 学习笔记292—docker api是什么
    dockerapi指的是docker的应用程序接口,是软件系统不同组成部分衔接的约定,docker主要有三大对外api:1、DockerRegistryAPI;2、DockerHubAPI;3、DockerRemoteAPI。本教程操作环境:linux5.9.8系统、docker-1.13.1版、DellG3电脑。一、什么是API1.API具体是什么?API这个词在......
  • 微信小程序入门教程(一)API接口数据记录
    今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来小程序代码:httpsearch:function(name,offset,type,cb){wx.request({url:'https://www.tinywan.com/api/wechat/songsSearch',data:{name:name,offset:o......
  • 第四十一篇 vue - 进阶主题 - 组合式 API 常见问答
    什么是组合式API?组合式API(CompositionAPI)是一系列API的集合,使我们可以使用函数而不是声明选项的方式书写Vue组件。它是一个概括性的术语,涵盖了以下方面的API1、响应式API例如ref()和reactive(),使我们可以直接创建响应式状态、计算属性和侦听器。2、生命......
  • HTML5地理定位 Geolocation API
    使用getCurrentPosition方法来取得用户当前的地理位置信息,该方法的定义如下所示。voidgetCurrentPosition(onSuccess,onError,options);第一个参数为获取当前地理位置信息成功时所执行的回调函数;第二个参数为获取当前地理位置信息失败时所执行的回调函数;第三个参数为一些可选属......