<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Getting Search Results using Java REST API in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88399#M22608</link>
    <description>&lt;P&gt;Hi - You can set the output_mode on your results to json, xml, csv.  Take a look at this code that sets the output mode to JSON.  &lt;/P&gt;

&lt;P&gt;Job job = service.getJobs().create("search index=twitter | head 5");                        &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    while (!job.isDone()) 
    {
           try
           {
                Thread.sleep(2000);
           }
           catch (InterruptedException e) {}

           job.refresh();
    }

    Map&amp;lt;String, Object&amp;gt; outputArgs = new HashMap&amp;lt;String, Object&amp;gt;();
    outputArgs.put("output_mode", "json");

    InputStream stream = job.getResults(outputArgs);

    InputStreamReader reader = new InputStreamReader(stream);
    OutputStreamWriter writer = new OutputStreamWriter(System.out);

    int size = 1024;
    char[] buffer = new char[size];

    try
    {

    while (true) {
        int count = reader.read(buffer);
        if (count == -1) break;
        writer.write(buffer, 0, count);
    }

    writer.write("\n");
    writer.close();
    reader.close();
    }
    catch (Exception e) {}

    job.cancel();
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Mar 2012 07:56:06 GMT</pubDate>
    <dc:creator>psanford_splunk</dc:creator>
    <dc:date>2012-03-09T07:56:06Z</dc:date>
    <item>
      <title>Getting Search Results using Java REST API</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88398#M22607</link>
      <description>&lt;P&gt;Am i right to say that the results derived from the Splunk search is returned as XML by default?&lt;BR /&gt;
I was using the Java Splunk REST API to get the results.&lt;/P&gt;

&lt;P&gt;This is the code that i used :&lt;/P&gt;

&lt;P&gt;InputStream dataStream = job.getResults();&lt;/P&gt;

&lt;P&gt;Does this input stream data contain XML?&lt;/P&gt;

&lt;P&gt;I attempted to output the data as XML file using Java but sometimes I can view the XML file and most of the time i ran the same Splunk search using Java and when i view the xml file on the browsers, I saw the message that says something like XML must have a top level element and sometimes  there is a blank space on the browser and the windows notepad.&lt;/P&gt;

&lt;P&gt;What is the cause of this problem? I need an answer urgently!!!!!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2012 02:38:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88398#M22607</guid>
      <dc:creator>misteryuku</dc:creator>
      <dc:date>2012-03-09T02:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Search Results using Java REST API</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88399#M22608</link>
      <description>&lt;P&gt;Hi - You can set the output_mode on your results to json, xml, csv.  Take a look at this code that sets the output mode to JSON.  &lt;/P&gt;

&lt;P&gt;Job job = service.getJobs().create("search index=twitter | head 5");                        &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    while (!job.isDone()) 
    {
           try
           {
                Thread.sleep(2000);
           }
           catch (InterruptedException e) {}

           job.refresh();
    }

    Map&amp;lt;String, Object&amp;gt; outputArgs = new HashMap&amp;lt;String, Object&amp;gt;();
    outputArgs.put("output_mode", "json");

    InputStream stream = job.getResults(outputArgs);

    InputStreamReader reader = new InputStreamReader(stream);
    OutputStreamWriter writer = new OutputStreamWriter(System.out);

    int size = 1024;
    char[] buffer = new char[size];

    try
    {

    while (true) {
        int count = reader.read(buffer);
        if (count == -1) break;
        writer.write(buffer, 0, count);
    }

    writer.write("\n");
    writer.close();
    reader.close();
    }
    catch (Exception e) {}

    job.cancel();
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2012 07:56:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88399#M22608</guid>
      <dc:creator>psanford_splunk</dc:creator>
      <dc:date>2012-03-09T07:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Search Results using Java REST API</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88400#M22609</link>
      <description>&lt;P&gt;@misteryuku  &amp;amp; @psanford_splunk could you please help me in writing a java code to export search results in json format..i have tried the above method but still failing to get the result.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 12:16:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Getting-Search-Results-using-Java-REST-API/m-p/88400#M22609</guid>
      <dc:creator>harikag</dc:creator>
      <dc:date>2019-07-29T12:16:33Z</dc:date>
    </item>
  </channel>
</rss>

