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!

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...