<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I compare different values for fields returned using the python REST API? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-do-I-compare-different-values-for-fields-returned-using-the/m-p/153102#M2093</link>
    <description>&lt;P&gt;This is alot easier using the &lt;A href="http://dev.splunk.com/python"&gt;Splunk Python SDK&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Example code doing mock String compare against the _raw field from the export search results :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.results as results
import splunklib.client as client

def compare(val_a, val_b):
    return val_a == val_b

if __name__ == '__main__':

    service = client.connect(host='localhost',port=8089,username='admin',password='abc')
    kwargs_export = {"earliest_time": "-1h",
                  "latest_time": "now",
                  "search_mode": "normal"}
    searchquery_export = "search index=_internal"

    exportsearch_results = service.jobs.export(searchquery_export, **kwargs_export)

    reader = results.ResultsReader(exportsearch_results)

    foo_field = 'foo'
    for result in reader:
        if isinstance(result, dict):
            raw_field = result['_raw']
            print compare(raw_field,foo_field)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Jun 2015 17:49:57 GMT</pubDate>
    <dc:creator>Damien_Dallimor</dc:creator>
    <dc:date>2015-06-10T17:49:57Z</dc:date>
    <item>
      <title>How do I compare different values for fields returned using the python REST API?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-do-I-compare-different-values-for-fields-returned-using-the/m-p/153101#M2092</link>
      <description>&lt;P&gt;I have just started playing around with the python REST API for a project i have in mind. Please forgive me as this is my first real attempt at scripting/programming anything really. I'm using Python to query Splunk.&lt;/P&gt;

&lt;P&gt;Anyway based on the examples i found on the website, this is a part of the code: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;request = urllib2.Request(base_url + '/servicesNS/%s/search/search/jobs/export' % (username), 
    data = urllib.urlencode({'search': search_query,'output_mode': 'csv'}),
    headers = { 'Authorization': ('Splunk %s' %session_key)})
search_results = urllib2.urlopen(request)
returned_data = search_results.read()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is an example output   (i have on purpose only selected two fields and 3 events for each)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"_time",Service
"2015-06-10 18:09:08.000 BST","dnsmasq-dhcp[472]"
"2015-06-10 18:09:08.000 BST","dnsmasq-dhcp[472]"
"2015-06-10 17:48:04.000 BST","dnsmasq-dhcp[472]"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;When printing the value of returned_data, i can see all the information i expect. However, the variable has a type of string so i need to convert it to something, but not sure what. The end aim is to be able to compare the different values in the fields. Would i need to convert the above output to a dictionary or a list ? Also, should i maybe be trying to export the results from splunk in a different format than csv? &lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2015 17:26:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-do-I-compare-different-values-for-fields-returned-using-the/m-p/153101#M2092</guid>
      <dc:creator>ng87</dc:creator>
      <dc:date>2015-06-10T17:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare different values for fields returned using the python REST API?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-do-I-compare-different-values-for-fields-returned-using-the/m-p/153102#M2093</link>
      <description>&lt;P&gt;This is alot easier using the &lt;A href="http://dev.splunk.com/python"&gt;Splunk Python SDK&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Example code doing mock String compare against the _raw field from the export search results :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.results as results
import splunklib.client as client

def compare(val_a, val_b):
    return val_a == val_b

if __name__ == '__main__':

    service = client.connect(host='localhost',port=8089,username='admin',password='abc')
    kwargs_export = {"earliest_time": "-1h",
                  "latest_time": "now",
                  "search_mode": "normal"}
    searchquery_export = "search index=_internal"

    exportsearch_results = service.jobs.export(searchquery_export, **kwargs_export)

    reader = results.ResultsReader(exportsearch_results)

    foo_field = 'foo'
    for result in reader:
        if isinstance(result, dict):
            raw_field = result['_raw']
            print compare(raw_field,foo_field)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jun 2015 17:49:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-do-I-compare-different-values-for-fields-returned-using-the/m-p/153102#M2093</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2015-06-10T17:49:57Z</dc:date>
    </item>
  </channel>
</rss>

