Splunk Dev

Key Error in Python Splunk SDK while accessing KV store

manideveloper4u
Engager

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

Code:

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:
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

Labels (3)
0 Karma

manikandankasi
Explorer

Hi,
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.
i have modified the code below. hope this should work!!!.

     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]
0 Karma
Get Updates on the Splunk Community!

.conf25 Community Recap

Hello Splunkers, And just like that, .conf25 is in the books! What an incredible few days — full of learning, ...

Splunk App Developers | .conf25 Recap & What’s Next

If you stopped by the Builder Bar at .conf25 this year, thank you! The retro tech beer garden vibes were ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...