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

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...