导出导出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