我在ElasticSearch中创建了索引和字段。 我可以使用以下命令成功运行Logstash配置文件,将MySQL数据库表中的数据添加到ElasticSearch中:
bin/logstash -f [PATH TO LOGSTASH CONFIG FILE] -v
我需要从我的Java源代码中运行此命令。如何从Java代码运行此logstash配置文件?
答案 0 :(得分:1)
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();
}
}
}
答案 1 :(得分:0)
试试这段代码。它有效。
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) {
}
答案 2 :(得分:0)
尝试一下,对上面提到的代码进行少量更改即可正常工作
尝试{
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "logstash -f --Your file Path-- ");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line1;
while (true) {
line1 = r.readLine();
if (line1 == null) { break; }
System.out.println(line1);
}
}catch(Exception e) {
e.printStackTrace();
}