Splunk Dev

How do I format the _key with splunk-python-sdk KVStoreCollectionData.insert data parameter?

organus
Explorer

I am sending an insert request via the splunk-python-sdk using the class splunklib.client.KVStoreCollectionData(collection) described here: https://docs.splunk.com/Documentation/PythonSDK. But I am throwing an error that is inconsistent with the documentation. Does the insert method require a specific syntax for the _id pair?

The reference documentation for the insert method states, "...An _id field will be generated if not assigned in the data."

When I try to insert a new record with a populated _id field, however, I get a stack trace.

Here is my abbreviated code:

    try:
        kvStoreConn = client.connect(**kwargs)
        print("Connected to KVStore Successfully")
        collection = kvStoreConn.kvstore[props.get('dcCollection')]
        kvData = collection.data
        #print(json.dumps(kvData.query()))
        #kvStoreConn.logout()
    except AuthenticationError:
        print("Failed to connect")
        sys.exit(1)

    for record in allRecords:
        recID = record.get('_id')
        print(recID)
        kvRecord = 'None'
        try:
            kvData.insert(json.dumps(record))
            sys.exit(1)
        except splunklib.binding.HTTPError as herror:
            print ('Found HTTPError: ', herror)
            print('No record found.')
            sys.exit(1)

My print methods return this:

Found HTTPError:  HTTP 400 Bad Request -- The _id field is a reserved field and may not be present in a document or query.
No record found.

My POST looks like this [edited for clarity]

{
  "_id": "5bb27674d6a97e32ed36fc82",
  "submissionName": "U.S. Voluntary Green Power Market Data 2017",
  "abstract": "This data book provides certain data behind figures and tables found in the NREL report Status and Trends in the U.S. Voluntary Green Power Market 2017 Data. These data reflect estimates based on the best available data.",
  "sectors": [
    "Bioenergy",
    "Solar Power",
    "Wind Energy"
  ],
  "disciplines": [
    "Photovoltaics",
    "Solar Energy"
  ],
  "nrelCenter": "6A20",
  "publisher": "National Renewable Energy Laboratory",
  "fundingOrganization": "DOE",
  "projectTitle": "Green Power Markets",
  "fundingOffice": "Office of Strategic Programs (EE-SP)",
  "projectNumber": "GO28308",
  "pubId": "72204",
  "hasConsent": "1",
 "moratorium": "2018-10-07 18:00:00",
  "keywords": [
    "NREL",
    "energy",
    "data",
    "green power sales",
    "hydropower",
    "energy certificates",
    "community solar",
    "solar",
    "wind",
    "biomass",
    "geothermal"
  ],
  "created": "2018-10-01 13:33:08",
  "publicationDate": "2018-10-01 13:33:08",
  "updated": [
    "2018-10-08 10:10:15",
    "2018-10-08 10:09:53"
  ],
  "status": "Publicly accessible",
  "appName": "dc",
  "userId": "user",
  "submitted": "2018-10-01 14:01:06",
  "DOI": "10.7799/1476631"
}
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!

Index This | What travels the world but is also stuck in place?

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

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...