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!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...