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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...