Created
June 14, 2021 12:11
-
-
Save jonuwz/b1a021ed0e8b4cba7783ad4ebbc4b94d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Splunk, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"): you may | |
* not use this file except in compliance with the License. You may obtain | |
* a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package com.splunk.examples.basic_auth; | |
import com.splunk.*; | |
import java.util.Base64; | |
import java.io.InputStream; | |
public class Program { | |
public static void main(String[] args) { | |
Service.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2); | |
Service service = new Service("localhost", 8089); | |
String credentials = "admin:password"; | |
String basicAuthHeader = Base64.getEncoder().encodeToString(credentials.getBytes()); | |
service.setToken("Basic " + basicAuthHeader); | |
JobExportArgs exportArgs = new JobExportArgs(); | |
exportArgs.setEarliestTime("-1h"); | |
exportArgs.setLatestTime("now"); | |
exportArgs.setSearchMode(JobExportArgs.SearchMode.NORMAL); | |
// Run the search with a search query and export arguments | |
String mySearch = "search index=_internal | stats count by sourcetype"; | |
InputStream exportSearch = null; | |
try { | |
exportSearch = service.export(mySearch, exportArgs); | |
} catch (Exception e) { | |
System.out.println("[ERROR] Failed to execute search"); | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// Display results using the SDK's multi-results reader for XML | |
try { | |
MultiResultsReaderXml multiResultsReader = new MultiResultsReaderXml(exportSearch); | |
int counter = 0; // count the number of events | |
for (SearchResults searchResults : multiResultsReader) | |
{ | |
for (Event event : searchResults) { | |
for (String key: event.keySet()) | |
System.out.println(" " + key + ": " + event.get(key)); | |
} | |
} | |
multiResultsReader.close(); | |
} catch (Exception e) { | |
System.out.println("[ERRROR] Failed to print search"); | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment