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
Get Updates on the Splunk Community!

Splunk Answers Content Calendar, June Edition

Get ready for this week’s post dedicated to Splunk Dashboards! We're celebrating the power of community by ...

What You Read The Most: Splunk Lantern’s Most Popular Articles!

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

See your relevant APM services, dashboards, and alerts in one place with the updated ...

As a Splunk Observability user, you have a lot of data you have to manage, prioritize, and troubleshoot on a ...