Run Logstash in Java Program

2019-02-20 15:13发布

I have created indexes and fields in ElasticSearch. I could successfully run Logstash config file to add data from MySQL database table into ElasticSearch using the following command :

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

I need to run this command from my Java source code. How do I run this logstash config file from Java code?

3条回答
放我归山
2楼-- · 2019-02-20 15:42
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();
                }
        }
}
查看更多
We Are One
3楼-- · 2019-02-20 15:43

Try this and this works fine with few changes to the above mentioned code:

try {

            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();
            }
查看更多
该账号已被封号
4楼-- · 2019-02-20 16:02

Try this code. It works.

        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) {

        }
查看更多
登录 后发表回答