Splunk Dev

Problem running search Sample from SDK - Query error

wajihullahbaig
Explorer

I am having problems running the search sample provided with the Java SDK. Which resides on my disk here

H:\splunk-splunk-sdk-java-0.1.0-3-gc7767c5\splunk-splunk-sdk-java-c7767c5\examples\search\com\splunk\sdk\search

Now, when I try to run the program I keep getting the and exception is caught.

    static void run(String[] args) throws IOException {
        Command command = Command.splunk("search");
        command.addRule("count", Integer.class, resultsCount);
        command.addRule("earliest_time", String.class, earliestTime);
        command.addRule("field_list", String.class, fieldListText);
        command.addRule("latest_time", String.class, latestTime);
        command.addRule("offset", Integer.class, offset);
        command.addRule("output", String.class, outputText);
        command.addRule("output_mode", String.class, outputModeText);
        command.addRule("status_buckets", Integer.class, statusBucketsText);
        command.addRule("verbose", "Display search progress");


       command.parse(args);

        if (command.args.length != 1)
            Command.error("Search e

xpression required");
    String query = command.args[0];

    int resultsCount = 100;
    if (command.opts.containsKey("count"))
        resultsCount = (Integer)command.opts.get("count");

    String earliestTime = null;
    if (command.opts.containsKey("earliest_time"))
        earliestTime = (String)command.opts.get("earliest_time");

    String fieldList = null;
    if (command.opts.containsKey("field_list"))
        fieldList = (String)command.opts.get("field_list");

    String latestTime = null;
    if (command.opts.containsKey("latest_time"))
        earliestTime = (String)command.opts.get("latest_time");

    int offset = 0;
    if (command.opts.containsKey("offset"))
        offset = (Integer)command.opts.get("offset");

    String output = "results";
    if (command.opts.containsKey("output")) {
        output = (String)command.opts.get("output");
        if (!Arrays.asList(outputChoices).contains(output))
            Command.error("Unsupported output: '%s'", output);
    }

    String outputMode = "xml";
    if (command.opts.containsKey("output_mode"))
        outputMode = (String)command.opts.get("output_mode");

    int statusBuckets = 0;
    if (command.opts.containsKey("status_buckets"))
        statusBuckets = (Integer)command.opts.get("status_buckets");

    boolean verbose = command.opts.containsKey("verbose");

    Service service = Service.connect(command.opts);

    // Check the syntax of the query.
    try {
        Args parseArgs = new Args("parse_only", true);
        service.parse(query, parseArgs);
    }
    catch (HttpException e) {
        String detail = e.getDetail();
        Command.error("query '%s' is invalid: %s", query, detail);
    }

With error printed
Error: query 'search=search error | head 10 -d output_mode=csv' is invalid:

What is the correct query? Looks like I am providing the wrong query in the command line arguments. Using NetBeans 7.1 IDE.

Please guide

Tags (3)
0 Karma
1 Solution

apruneda_splunk
Splunk Employee
Splunk Employee

This page http://dev.splunk.com/view/SP-CAAAEFF#search has examples of how to form the search query when using the Java SDK command-line examples. For example:

java -jar search.jar 'search error | head 10' --output_mode=csv

View solution in original post

apruneda_splunk
Splunk Employee
Splunk Employee

This page http://dev.splunk.com/view/SP-CAAAEFF#search has examples of how to form the search query when using the Java SDK command-line examples. For example:

java -jar search.jar 'search error | head 10' --output_mode=csv

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Value Insights: Now Generally Available in the CMC

Organizations are under pressure to move faster, control cost, expand AI adoption, and prove value with more ...

What’s New in Splunk AI: Volume 02

Welcome to the second edition of “What’s New in Splunk AI” where we look at the latest and greatest updates, ...

Splunk App Dev Quarterly Roundup: AI, Agents, and Innovation!

Another quarter, another wave of innovation. From complex integrations to pushing the limits ...