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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

.conf26 Registration is Live: Secure Your Early Bird Pass Now

  Lock in Your Spot: Registration Open for .conf26 in Denver Hello Splunkers, I have exciting news! Your ...

Mile High Learning with Splunk University, Denver, Colorado

If Denver is known for its mile-high elevation, Splunk University is about to raise the bar on technical ...

IT Service Intelligence 5.0 Series: Your Guide to the June Launch

We are excited to announce the June release of Splunk IT Service Intelligence (ITSI) 5.0. This update ...