首页 > 解决方案 > 如何从 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: ");

标签: javajsonrestsplunk

解决方案


有点不清楚您要做什么。在文本中,您说您正在尝试使用 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);

推荐阅读