在Java程序中运行Logstash

我在ElasticSearch中创建了索引和字段。 我可以使用以下命令成功运行Logstash配置文件,将MySQL数据库表中的数据添加到ElasticSearch中:

bin/logstash -f [PATH TO LOGSTASH CONFIG FILE] -v 

我需要从我的Java源代码中运行此命令。 如何从Java代码运行此logstash配置文件?

 import java.io.*; class UserTest{ public static void main(String[] args) { try { String s = ""; String[] cmd = new String[]{"/bin/sh", "./logstash","-f","loggingConfFile.conf"}; Process processes = Runtime.getRuntime().exec(cmd); BufferedReader stdInput = new BufferedReader(new InputStreamReader(processes.getInputStream())); while ((s = stdInput.readLine()) != null) { System.out.println(s); } } catch(Exception ex) { ex.printStackTrace(); } } } 

试试这个代码。 有用。

  try { ProcessBuilder b1 = new ProcessBuilder("cmd.exe", "/c", "cd \"C:\\elk\\logstash-5.1.2\\bin\" && logstash -f first-pipeline.conf --config.reload.automatic"); b1.redirectErrorStream(true); Process p1 = b1.start(); BufferedReader r1 = new BufferedReader(new InputStreamReader(p1.getInputStream())); String line1; while (true) { line1 = r1.readLine(); if (line1 == null) { break; } System.out.println(line1); } }catch(Exception e) { }