java - 如何从 JAVA 向 Splunk Enterprise 发送 JSON 文件?
问题描述
我首先说我是初学者。我正在建立一个收集一些 JSON 文件的系统,我在 JAVA(Spring 批处理)中解析它们,而我被卡住的部分是将这些文件发送到 Splunk 企业中的HTTP EVENT COLLECTOR (HEC)。我尝试在网上搜寻一些适合初学者的指南,但找不到任何东西。我想将 POST 与所述文件一起发送到 Splunk 企业,以便在发送后对它们进行索引。到目前为止,我只能像这样连接到 localhost:8089:
HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);
ServiceArgs connectionArgs = new ServiceArgs();
connectionArgs.setHost("localhost");
connectionArgs.setUsername("AdrianAlter");
connectionArgs.setPassword("mypassword");
connectionArgs.setPort(8089);
connectionArgs.put("scheme","https");
// will login and save the session key which gets put in the HTTP Authorization header
Service splunkService = Service.connect(connectionArgs);
System.out.println("Auth Token : " + splunkService.getToken());
Job info = splunkService.getJobs().create("search index=main");
System.out.println("Info: ");
解决方案
有点不清楚您要做什么。在文本中,您说您正在尝试使用 HTTP 事件收集器 (HEC) 发送数据。但是,示例代码似乎正在尝试执行搜索。
要将数据发送到 Java 中的 HEC endoint,以下代码片段可能是一个合适的起点。
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://<SERVER>:8088/services/collector/event");
httppost.addHeader("Authorization", " Splunk <token id>");
String eventStr = "{sourcetype=_json, index=main, event={ <JSON> }}"
httppost.setEntity(new StringEntity(eventStr);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("response: " + entity);
推荐阅读
- sql - 为日期分配排名
- javascript - 有什么方法可以找到用户何时加入不和谐服务器?我的机器人返回未定义
- c++ - 计算链表的给定总和的所有子集
- android - 为android编译cmake项目时找不到JPEG
- html - 在您的网站中包含 svg 的最佳方式是什么(速度方面)
- javascript - 显示从 express 传递到 ejs 模板的数组的单个项目,并带有下一个按钮以切换单个项目
- javascript - 如何专注于内容可编辑 div 中的新块?
- python - 无法理解这段代码的最后一部分
- javascript - html中的无意义错误
- python - Pylint 消息:日志 - 格式化 - 错误(尝试了很多)