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

Key Error in Python Splunk SDK while accessing KV store

Open ghost opened this issue 7 years ago • 2 comments

Hi,

I'm getting Key Error while accessing splunk kv store using splunk python sdk. Python Version: 2.7 Splunk sdk version : 1.6.6 OS: Windows 7 & 10

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()
     

Error Details: Traceback (most recent call last): File "kv_store_opps.py", line 36, in main() File "kv_store_opps.py", line 21, in main collection=splunk_service.kvstore[collection_name] File "C:\Python27\lib\site-packages\splunklib\client.py", line 1243, in getitem raise KeyError(key) KeyError: UrlEncoded('test')

Any help on this much appreciated.

Thanks, Mani

ghost avatar May 12 '19 12:05 ghost

Hi @Manikandankasi01,

You're seeing this KeyError because the collection_name doesn't exist in kvstore. You can fix this by wrapping this line in a try/except block.

collection=splunk_service.kvstore[collection_name]

I would refactor your code like so:

try:
    collection=splunk_service.kvstore[collection_name]
    print("{} Already exist !!!".format(collection_name))
except KeyError:
    collection=splunk_service.kvstore.create(collection_name)

Overall diff would become:

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)
+
+     try:
+         collection=splunk_service.kvstore[collection_name]
+         print("{} Already exist !!!".format(collection_name))
+     except KeyError:
+         collection=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()

shakeelmohamed avatar May 13 '19 18:05 shakeelmohamed

Thanks shakeelmohamed. its worked !!!.

ghost avatar May 13 '19 23:05 ghost