Quantcast
Channel: Questions in topic: "splunk-enterprise"
Viewing all articles
Browse latest Browse all 47296

How to write to kvstore with java sdk?

$
0
0
We are trying to inject JSON directly into our KV Store instance while using a defined _key inside the JSON object. We are not able to connect because of the following error: 2016-09-23 10:25:01 ERROR stderr:71 - com.splunk.HttpException: HTTP 400 -- 2016-09-23 10:25:01 ERROR stderr:71 - 2016-09-23 10:25:01 ERROR stderr:71 - 2016-09-23 10:25:01 ERROR stderr:71 - Must supply 'Content-Type' header set to 'application/json' 2016-09-23 10:25:01 ERROR stderr:71 - 2016-09-23 10:25:01 ERROR stderr:71 - We have looked for a way to add message headers to the RequestMessage object but we do not see a method in the source. Where do we inject message headers? The method breaks on the following line: response = service.send(kvStorePath, requestMessage); Below is our method: private void writeToKVStore(File uglyJsonFile) throws IOException { String nextJSON = ""; ResponseMessage response = null; BufferedReader br = null; ServiceArgs serviceArgs = null; Service service = null; RequestMessage requestMessage = null; int port = params.getKvStorePort(); String kvStorePath = params.getKvStorePath(); String kvStoreScheme = params.getKvStoreScheme(); String kvStoreUsername = params.getkvStoreUsername(); String kvStorePassword = params.getkvStorePassword(); String kvStoreHost = params.getKvStoreHost(); String kvStoreApp = params.getKvStoreApp(); try { HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2); serviceArgs = new ServiceArgs(); serviceArgs.setScheme(kvStoreScheme); serviceArgs.setUsername(kvStoreUsername); serviceArgs.setPassword(kvStorePassword); serviceArgs.setHost(kvStoreHost); serviceArgs.setPort(port); serviceArgs.setApp(kvStoreApp); // kvStoreApp = "kvstore" service = Service.connect(serviceArgs); // TODO set up requestMessage // Must supply 'Content-Type' header set to 'application/json' requestMessage = new RequestMessage("POST"); // service.send(params.getKvStorePath(), requestMessage); br = new BufferedReader(new FileReader(uglyJsonFile)); while ((nextJSON = br.readLine()) != null) { requestMessage.setContent(nextJSON); response = service.send(kvStorePath, requestMessage); } } catch (IOException e) { LOGGER.error(e.getMessage()); e.printStackTrace(); } catch (Exception ex) { LOGGER.error(ex.getMessage()); ex.printStackTrace(); if (response != null) { String nextLine = ""; LOGGER.error("Status: " + response.getStatus()); BufferedReader responseContent = new BufferedReader(new InputStreamReader(response.getContent(), "UTF-8")); while((nextLine = responseContent.readLine()) != null) { LOGGER.error(nextLine); } } } finally { try { if (br != null) { br.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); e.printStackTrace(); throw e; } } }

Viewing all articles
Browse latest Browse all 47296

Trending Articles