Splunk Dev

Javascript SDK Normal Search: Use return instead of console.log

mginsbu
Engager

Hi,

I have used the Search_normal.js example that runs a normal search, prints the job statistics and search results. How do I return the search results (instead of logging to console) so that I can pass the result data into another js file?

The code:

 

var splunkjs = require('splunk-sdk');
var Async  = splunkjs.Async;

exports.main = function(opts, callback) {
    opts = opts || {};
    
    var username = opts.username    || "*******";
    var password = opts.password    || "********";
    var scheme   = opts.scheme      || "https";
    var host     = opts.host        || "localhost";
    var port     = opts.port        || "8089";
    var version  = opts.version     || "default";
    
    var service = new splunkjs.Service({
        username: username,
        password: password,
        scheme: scheme,
        host: host,
        port: port,
        version: version
    });

    Async.chain([
            // Login
            function(done) {
                service.login(done);
            },
            // Perform the search
            function(success, done) {
                if (!success) {
                    done("Error logging in");
                }
                
                service.search("search index=cog-censor-allow", {}, done);
            },
            // Wait until the job is done
            function(job, done) {
                job.track({}, function(job) {
                    // Ask the server for the results
                    job.results({}, done);
                });
            },
            // Print out the statistics and get the results
            function(results, job, done) {
                // Print out the statistics
                /*console.log("Job Statistics: ");
                console.log("  Event Count: " + job.properties().eventCount);
                console.log("  Disk Usage: " + job.properties().diskUsage + " bytes");
                console.log("  Priority: " + job.properties().priority);*/

                // Find the index of the fields we want
                var rawIndex = results.fields.indexOf("_raw");
                var sourcetypeIndex = results.fields.indexOf("sourcetype");
                var userIndex = results.fields.indexOf("user");
                
                // Print out each result and the key-value pairs we want
                console.log("Results: ");
                for(var i = 0; i < results.rows.length; i++) {
                    console.log("  Result " + i + ": ");
                    console.log("    sourcetype: " + results.rows[i][sourcetypeIndex]);
                    console.log("    user: " + results.rows[i][userIndex]);
                    console.log("    _raw: " + results.rows[i][rawIndex]);

                }
                
                job.cancel(done);
            }
        ],
        function(err) {
            callback(err);        
        }
    );
};

if (module === require.main) {
    exports.main({}, function() {});
}

 

My example code is located in the /splunk-sdk-javascript/examples/node directory. https://dev.splunk.com/enterprise/docs/javascript/sdk-javascript/sdkjavascriptexamples/cmdlinedkjava...

Labels (1)

ccmoeaung
Engager

HI mginsbu,

Did u got the right answer. I am also encounter like this problem. How did u solve ,can you please elaborate  that ?
Thanks

0 Karma

Salah2019
Observer

Hi, I am also interested, Did any one figure it out? 

0 Karma
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 ...