splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

output_mode=csv when fetching results breaks results parser?

Open parhamfh opened this issue 14 years ago • 6 comments

With reference to JIRA issue SPL-53634 I am translating the Java SDK example described in http://dev.staging.splunk.com/view/SP-CAAAECN#search to a Helmut example. The code can be seen below. It seems to work when I omit writing output_mode='csv'. In case I don't omit it I get the following error.

Traceback (most recent call last):
  File "./fetcheventstest.htest.py", line 37, in <module>
    for r in job.get_results(output_mode='csv',count=max_result_rows,offset=get_offset):
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/manager/jobs/sdk/job.py", line 176, in get_results
    return _build_results_from_sdk_response(response)
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/manager/jobs/sdk/job.py", line 206, in     _build_results_from_sdk_response
    while reader.read():
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/contrib/splunk-sdk-python/splunklib/results.py", line 397, in read
        kind = self._scan()
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/contrib/splunk-sdk-python/splunklib/results.py", line 364, in _scan
    self._reader.read()
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/contrib/splunk-sdk-python/splunklib/results.py", line 197, in read
    self._item = self._scan()
  File "/Users/phashemi/gitdepot/splunk-helmut/helmut/contrib/splunk-sdk-python/splunklib/results.py", line 147, in _scan
    item = self._items.next()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/pulldom.py", line 232, in next
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/pulldom.py", line 265, in getEvent
    self.parser.feed(buf)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/sax/expatreader.py", line 211, in feed
    self._err_handler.fatalError(exc)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/sax/handler.py", line 38, in fatalError
    raise exception
xml.sax._exceptions.SAXParseException: <unknown>:1:5: not well-formed (invalid token)

The code is this:

SPLUNK_HOME = '/Users/phashemi/splh/'
splunk = LocalSplunk(SPLUNK_HOME)

job = splunk.jobs.create('search index=_internal')
job.wait()

max_result_rows = int(splunk.confs['limits']['restapi']['maxresultrows'])
eventcount = int(job.get_event_count())

get_offset = 0
num = 0
while get_offset < eventcount:

    for r in job.get_results(output_mode='csv',count=max_result_rows,offset=get_offset):
        print num
        num = num + 1

    get_offset = get_offset + max_result_rows

print "Done parsing all events."

and in helmut:

def get_results(self, **kwargs):
    response = self.raw_sdk_job.results(**kwargs)
    return _build_results_from_sdk_response(response)

parhamfh avatar Aug 07 '12 20:08 parhamfh

What does _build_results_from_sdk_response do?

itay avatar Aug 07 '12 20:08 itay

The python streaming reader only supports XML streams.

wimcolgate avatar Aug 07 '12 20:08 wimcolgate

Ah ok, thanks @wimcolgate. No plans of implementing this?

@itay: it is just a wrapper function working with the result object's ResultReader.

parhamfh avatar Aug 17 '12 21:08 parhamfh

@parhamfh, No plans for implementing it. We've been working on the assumption that Splunk's output format is the SDK's problem. If it changes in the future, the SDK should hide that fact from the programmer.

madhadron avatar Aug 20 '12 19:08 madhadron

@parhamfh if you're using CSV output, you should just be able to use the csv.DictReader or csv.Reader that are included in the core Python standard library.

itay avatar Aug 20 '12 19:08 itay

Ok, guys, thanks for all the answers! This is not something urgent, just something I stumbled upon when trying to write up an example. I'll close this.

parhamfh avatar Aug 20 '12 20:08 parhamfh