Splunk Dev

how to use SID in java sdk

kicksammy
Explorer

Hi,
i am developing a tool which connects to splunk and runs a search with SID.
if i have SID and want to get the results through java sdk using SID as input. How can i achieve this?

Tags (1)
0 Karma

kicksammy
Explorer

Thanks for the reply. it worked for me

0 Karma

Damien_Dallimor
Ultra Champion

Here is a code example for you.Verified it working against Splunk 6 using Java SDK version 1.2 running from Eclipse , JRE 1.6.

public static void getJobResults(String sid){

    ServiceArgs connectionArgs = new ServiceArgs();
    connectionArgs.setHost("yourhost");
    connectionArgs.setUsername("someuser");
    connectionArgs.setPassword("somepass");
    connectionArgs.setPort(8089);
    Service splunkService = Service.connect(connectionArgs);

    JobCollection jobs = splunkService.getJobs();
    Job job = jobs.get(sid);

    while (!job.isDone()) {
        try {
            Thread.sleep(500);
        } catch (Exception e) {
        }
    }

    OutputMode outputMode = OutputMode.JSON;

    JobResultsArgs outputArgs = new JobResultsArgs();
    outputArgs.setOutputMode(outputMode);

    ResultsReader resultsReader = null;
    try {
        resultsReader = new ResultsReaderJson(job.getResults(outputArgs));
        HashMap<String, String> map;
        while ((map = resultsReader.getNextEvent()) != null) {
            for (String key : map.keySet())
                System.out.println("   " + key + " --> " + map.get(key));
        }
    } catch (Exception e) {/**handle exception**/}
    finally{
      try {
        resultsReader.close();
    } catch (Exception e) {/**handle exception**/}
    }

}

kicksammy
Explorer

Thanks for the reply. It worked for me

0 Karma
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...