<?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: Key Error in Python Splunk SDK while accessing KV store in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Key-Error-in-Python-Splunk-SDK-while-accessing-KV-store/m-p/457715#M8322</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;
you should first check kv store exist or not. if exist then you can access the kv store otherwise you can create. but in your code you try to accessing kvstore before checking kv store exist or not. so the problem here is test kvstore does not exist in splunk collection thats why getting key error.&lt;BR /&gt;
i have modified the code below. hope this should work!!!.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;     collection_name="test"
     if collection_name in splunk_service.kvstore:
         print("{} Already exist !!!".format(collection_name))
     else:
         splunk_service.kvstore.create(collection_name)

     #Adding data to KV store
     collection.data.insert(json.dumps({"ID":"123","sId":1234,"Tech":"text"}))

     collection=splunk_service.kvstore[collection_name]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 19 May 2019 09:10:30 GMT</pubDate>
    <dc:creator>manikandankasi</dc:creator>
    <dc:date>2019-05-19T09:10:30Z</dc:date>
    <item>
      <title>Key Error in Python Splunk SDK while accessing KV store</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Key-Error-in-Python-Splunk-SDK-while-accessing-KV-store/m-p/457714#M8321</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm getting Key Error while accessing splunk kv store using splunk python sdk.&lt;BR /&gt;
Python Version: 2.7&lt;BR /&gt;
Splunk sdk version : 1.6.6&lt;BR /&gt;
OS: Windows 7 &amp;amp; 10&lt;/P&gt;

&lt;P&gt;Code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import sys, json
from splunklib.client import connect 
import getpass


def main():
    #Getting Splunk access details from User
    hostname = raw_input("Enter Splunk Hostname/IP: ")
    splunkUser = raw_input("Enter Splunk Admin Username: ")
    splunkPassword = getpass.getpass("Enter Splunk Admin Password: ")
    HOST = hostname
    PORT = 8089
    USERNAME = splunkUser
    PASSWORD = splunkPassword
    #Connecting splunk server
    splunk_service = connect(host=HOST,port=PORT,username=USERNAME,password=PASSWORD)
    print("Splunk connection succeed !!!!")
    splunk_service.namespace['owner'] = 'Nobody'
    collection_name="test"
    collection=splunk_service.kvstore[collection_name]

    if collection_name in splunk_service.kvstore:
        print("{} Already exist !!!".format(collection_name))
    else:
        splunk_service.kvstore.create(collection_name)

    #Adding data to KV store
    collection.data.insert(json.dumps({"ID":"123","sId":1234,"Tech":"text"}))

    #Retriving data from kv store
    kv_data=json.dumps(collection.data.query(),indent=1)
    print(kv_data)

if __name__ == '__main__':
    main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Error:&lt;BR /&gt;
Traceback (most recent call last):&lt;BR /&gt;
  File "kv_store_opps.py", line 36, in &lt;BR /&gt;
    main()&lt;BR /&gt;
  File "kv_store_opps.py", line 21, in main&lt;BR /&gt;
    collection=splunk_service.kvstore[collection_name]&lt;BR /&gt;
  File "C:\Python27\lib\site-packages\splunklib\client.py", line 1243, in &lt;STRONG&gt;getitem&lt;/STRONG&gt;&lt;BR /&gt;
    raise KeyError(key)&lt;BR /&gt;
KeyError: UrlEncoded('test')&lt;/P&gt;

&lt;P&gt;Any help on this much appreciated. &lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Mani&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:32:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Key-Error-in-Python-Splunk-SDK-while-accessing-KV-store/m-p/457714#M8321</guid>
      <dc:creator>manideveloper4u</dc:creator>
      <dc:date>2020-09-30T00:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Key Error in Python Splunk SDK while accessing KV store</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Key-Error-in-Python-Splunk-SDK-while-accessing-KV-store/m-p/457715#M8322</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
you should first check kv store exist or not. if exist then you can access the kv store otherwise you can create. but in your code you try to accessing kvstore before checking kv store exist or not. so the problem here is test kvstore does not exist in splunk collection thats why getting key error.&lt;BR /&gt;
i have modified the code below. hope this should work!!!.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;     collection_name="test"
     if collection_name in splunk_service.kvstore:
         print("{} Already exist !!!".format(collection_name))
     else:
         splunk_service.kvstore.create(collection_name)

     #Adding data to KV store
     collection.data.insert(json.dumps({"ID":"123","sId":1234,"Tech":"text"}))

     collection=splunk_service.kvstore[collection_name]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 May 2019 09:10:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Key-Error-in-Python-Splunk-SDK-while-accessing-KV-store/m-p/457715#M8322</guid>
      <dc:creator>manikandankasi</dc:creator>
      <dc:date>2019-05-19T09:10:30Z</dc:date>
    </item>
  </channel>
</rss>

