<?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: Java API query syntax failure in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Java-API-query-syntax-failure/m-p/294307#M55920</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Could you please do the following&lt;BR /&gt;
1) please check if the particular host log coming to mentioned sourcetype&lt;BR /&gt;
2)please put only sourcetype in the java search as follow&lt;BR /&gt;
Search sourcetype=WinEventLog:Security | stats count by host&lt;/P&gt;</description>
    <pubDate>Wed, 28 Mar 2018 17:10:15 GMT</pubDate>
    <dc:creator>logloganathan</dc:creator>
    <dc:date>2018-03-28T17:10:15Z</dc:date>
    <item>
      <title>Java API query syntax failure</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Java-API-query-syntax-failure/m-p/294306#M55919</link>
      <description>&lt;P&gt;I''m using Splunk 6.6.3, Java API 1.6.4.0, Java 1.8.0_45, IntelliJ IDE. &lt;/P&gt;

&lt;P&gt;I'm making part of a simple application that checks that a given system is actively logging, where the sourceType, hostname, and minutes back from present are being read from a database and become part of the query.&lt;/P&gt;

&lt;P&gt;An equivalent search query that works as expected in Splunk GUI, with time set as "Last 60 minutes" would be:&lt;/P&gt;

&lt;P&gt;sourcetype=WinEventLog:Security host=abcxyz | head 1&lt;/P&gt;

&lt;P&gt;I'm working from the examples provided, but none seem to show multiple arguments i.e. sourcetype, host, time range. In the code below, if I set:&lt;/P&gt;

&lt;P&gt;String mySearch = "search host="+ lsb.getSystem() + " ";  // just a host String&lt;/P&gt;

&lt;P&gt;It will work for at least some hosts.&lt;/P&gt;

&lt;P&gt;If I try to add the sourcetype, all will fail:&lt;/P&gt;

&lt;P&gt;String mySearch = "search sourcetype=WinEventLog:Security host="+ lsb.getSystem() + " ";&lt;/P&gt;

&lt;P&gt;Note: In the code below, the method minutesBackString() returns a String like: "2018-03-27T12:53:46.626-04:00"&lt;/P&gt;

&lt;P&gt;Can someone suggest a combination that will give the equivalent result of the GUI search? Ideally I would specify the field list, but I can get by without that. Any suggestions very much appreciated. Please Ignore the boolean return for now - it will be dependent on the content returned by the query.&lt;/P&gt;

&lt;P&gt;private boolean oneSystem(LoggingSystemBean lsb) {&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    boolean retval = false;

    String mySearch = "search sourcetype=WinEventLog:Security host="+ lsb.getSystem() + " "; // lsb.system is String
    JobArgs jobargs = new JobArgs();
    jobargs.setExecutionMode(JobArgs.ExecutionMode.NORMAL);
    jobargs.setEarliestTime(minutesBackString(60));
    jobargs.setLatestTime(minutesBackString(0));
    jobargs.setMaximumCount(1);

    Job job = service.getJobs().create(mySearch, jobargs);

    try {
        while ( !job.isDone() ) {

            Thread.sleep(500);
        }
    } catch (InterruptedException ie) {

    }
    // Display results
    InputStream results = job.getResults();
    String line = null;
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(results, "UTF-8"));
        while ( (line = br.readLine()) != null ) {

            System.out.println(line);
        }
        br.close();

    } catch (Exception ex) {

        errLog.severe(ex.getMessage() + "\n" + ExceptionUtils.getStackTrace(ex));
    }
    return (retval);
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;} &lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 18:21:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Java-API-query-syntax-failure/m-p/294306#M55919</guid>
      <dc:creator>tdhealy</dc:creator>
      <dc:date>2018-03-27T18:21:47Z</dc:date>
    </item>
    <item>
      <title>Re: Java API query syntax failure</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Java-API-query-syntax-failure/m-p/294307#M55920</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Could you please do the following&lt;BR /&gt;
1) please check if the particular host log coming to mentioned sourcetype&lt;BR /&gt;
2)please put only sourcetype in the java search as follow&lt;BR /&gt;
Search sourcetype=WinEventLog:Security | stats count by host&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 17:10:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Java-API-query-syntax-failure/m-p/294307#M55920</guid>
      <dc:creator>logloganathan</dc:creator>
      <dc:date>2018-03-28T17:10:15Z</dc:date>
    </item>
  </channel>
</rss>

