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

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...