Splunk Dev

Is there a way to run map function in a custom command reporting command map, when only reduce executes??

munang
Path Finder


I am developing a Reporting command. However, a problem was found in the search command I made. To explain the problem, I have attached some code snippets of splunk_python_sdk.

0. First, the final query I want to run is

index=splunk_example 
| table test, test_results
| customcommand(reporting command)

However, the command was not executed properly. However, the strange thing was that this command was executed using the index stored in the search header. However, the command was not executed on the index stored in the indexer.

 

1. found out
- So what I found out was that the map phase runs on the indexer and the rest of the reduce phase runs on the search header.

- And I checked the source code provided by the SDK, and I thought that the data extracted from the indexer can be passed as records when the map function is executed after checking the following related to the map function.

-> Q) Execution of the reporting command is done in two stages: map and reduce, but only  the reduce function actually executes. Is there any way to run the map function?

 

 

 

## 1-1. Here is the code provided by the sdk. 

def map(self, records):
        """ Override this method to compute partial results.
        :param records:
        :type records:
        You must override this method, if :code:`requires_preop=True`.
        """
        return NotImplemented

    def prepare(self):

        phase = self.phase

        if phase == 'map':
            # noinspection PyUnresolvedReferences
            self._configuration = self.map.ConfigurationSettings(self)
            return

        if phase == 'reduce':
            streaming_preop = chain((self.name, 'phase="map"', str(self._options)), self.fieldnames)
            self._configuration.streaming_preop = ' '.join(streaming_preop)
            return

        raise RuntimeError('Unrecognized reporting command phase: {}'.format(json_encode_string(six.text_type(phase))))

    def reduce(self, records):
        """ Override this method to produce a reporting data structure.
        You must override this method.
        """
        raise NotImplementedError('reduce(self, records)')

 

 

 

 

2. how i tried

- In this code, it is suggested to enable the requires_preop option to true to run the map function.

 

 

 

@Configuration()
    class TestReportingCommand(ReportingCommand):

@Configuration(requires_preop = True)
def map(self, records):
...

def reduce(self, records):

 

 

 

 

- commands.conf (add)

requires_preop = true

 

So, I tried both methods, but when I took a log, only the reduce function was executed and the map function was not executed.

 

If you know how to use map, please share.

I used a translator, so there may be some awkwardness in the text.

Labels (2)
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

All Work and No Play? Not at .conf26! Unwind at These Evening Events

Between hands-on technical sessions, keynote reveals, and diving into live architectures, .conf26 is packed ...

Join the Hackathon at .conf26 and build a No-Code AI agent

Join us for the AI Agent Buildathon, an in-person, three-hour hands-on Hackathon where you’ll use Splunk Agent ...

Level Up Your Workflow: Mastering Splunk Cloud Management via Terraform

Tech Talk Recap   From Chaos to Control: Scaling Splunk Cloud with Infrastructure as Code Managing apps in ...