|
1 | | - |
| 1 | +[](https://github.com/splunk/splunk-sdk-java/actions/workflows/test.yml) |
2 | 2 | # The Splunk Software Development Kit for Java |
3 | 3 |
|
4 | | -#### Version 1.7.1 |
| 4 | +#### Version 1.8.0 |
5 | 5 |
|
6 | 6 | The Splunk Software Development Kit (SDK) for Java contains library code and |
7 | 7 | examples designed to enable developers to build applications using Splunk. |
@@ -75,7 +75,7 @@ To add the Splunk SDK for Java `.JAR` file as a dependency: |
75 | 75 | <dependency> |
76 | 76 | <groupId>com.splunk</groupId> |
77 | 77 | <artifactId>splunk</artifactId> |
78 | | - <version>1.7.1</version> |
| 78 | + <version>1.8.0</version> |
79 | 79 | </dependency> |
80 | 80 | </dependencies> |
81 | 81 | ``` |
@@ -109,6 +109,161 @@ To build the documentation for the SDK, it is being automatically generated with |
109 | 109 | cd splunk |
110 | 110 | mvn javadoc:javadoc |
111 | 111 |
|
| 112 | +### Usage |
| 113 | +#### Login using username and password |
| 114 | +```java |
| 115 | +import com.splunk.Service; |
| 116 | +import com.splunk.ServiceArgs; |
| 117 | + |
| 118 | +/** |
| 119 | + * Login using username and password |
| 120 | + */ |
| 121 | +public class SplunkLogin { |
| 122 | + |
| 123 | + static Service service = null; |
| 124 | + public static void main(String args[]) { |
| 125 | + ServiceArgs loginArgs = new ServiceArgs(); |
| 126 | + loginArgs.setPort(8089); |
| 127 | + loginArgs.setHost("localhost"); |
| 128 | + loginArgs.setScheme("https"); |
| 129 | + loginArgs.setUsername("USERNAME"); // Use your username |
| 130 | + loginArgs.setPassword("PASSWORD"); // Use your password |
| 131 | + |
| 132 | + // Initialize the SDK client |
| 133 | + service = Service.connect(loginArgs); |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### Login using Session Token |
| 139 | +```java |
| 140 | +import com.splunk.Service; |
| 141 | +import com.splunk.ServiceArgs; |
| 142 | + |
| 143 | +/** |
| 144 | + * Login using Session token |
| 145 | + */ |
| 146 | +public class SplunkLogin { |
| 147 | + |
| 148 | + static Service service = null; |
| 149 | + /** |
| 150 | + * Session Token. |
| 151 | + * Actual token length would be longer than this token length. |
| 152 | + */ |
| 153 | + static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5"; |
| 154 | + |
| 155 | + public static void main(String args[]) { |
| 156 | + ServiceArgs loginArgs = new ServiceArgs(); |
| 157 | + loginArgs.setPort(8089); |
| 158 | + loginArgs.setHost("localhost"); |
| 159 | + loginArgs.setScheme("https"); |
| 160 | + loginArgs.setToken(String.format("Splunk %s", token)); |
| 161 | + |
| 162 | + // Initialize the SDK client |
| 163 | + service = Service.connect(loginArgs); |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | +* Login using username and password will create Session token internally. |
| 168 | +* Login using Credentials (username & password) OR directly using Session token are similar. |
| 169 | +* In above two approaches, there is one limitation that expiration time of Session token cannot be extended. User has to re-login every time when token expires. |
| 170 | +* To overcome this limitation, **Authentication** token is used instead of Session token. |
| 171 | +* In **Authentication** token, user has a provision to set token expiration time. Splunk allows user to set relative/absolute time for token expiration. |
| 172 | +* In other words, **Authentication** token is configurable whereas Session token cannot be configured. |
| 173 | + |
| 174 | +#### Login using Authentication Token (RECOMMENDED) |
| 175 | +```java |
| 176 | +import com.splunk.Service; |
| 177 | +import com.splunk.ServiceArgs; |
| 178 | + |
| 179 | +/** |
| 180 | + * Login using Authentication token |
| 181 | + */ |
| 182 | +public class SplunkLogin { |
| 183 | + |
| 184 | + static Service service = null; |
| 185 | + /** |
| 186 | + * Authentication Token. |
| 187 | + * Actual token length would be longer than this token length. |
| 188 | + */ |
| 189 | + static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5"; |
| 190 | + |
| 191 | + public static void main(String args[]) { |
| 192 | + ServiceArgs loginArgs = new ServiceArgs(); |
| 193 | + loginArgs.setPort(8089); |
| 194 | + loginArgs.setHost("localhost"); |
| 195 | + loginArgs.setScheme("https"); |
| 196 | + loginArgs.setToken(String.format("Bearer %s", token)); |
| 197 | + |
| 198 | + // Initialize the SDK client |
| 199 | + service = Service.connect(loginArgs); |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +#### Example of running a simple search by first creating the search job |
| 205 | +```java |
| 206 | +import com.splunk.Job; |
| 207 | +import com.splunk.ResultsReader; |
| 208 | +import com.splunk.ResultsReaderXml; |
| 209 | +import com.splunk.Service; |
| 210 | +import com.splunk.ServiceArgs; |
| 211 | + |
| 212 | +/** |
| 213 | + * Logged in using Authentication token. |
| 214 | + * Assuming that authentication token is already created from Splunk web. |
| 215 | + * Create Job using search creation. |
| 216 | + * Read results and print _raw fields |
| 217 | + */ |
| 218 | +public class SearchExample { |
| 219 | + |
| 220 | + static Service service = null; |
| 221 | + |
| 222 | + /** |
| 223 | + * Authentication Token. |
| 224 | + * Actual token length would be longer than this token length. |
| 225 | + */ |
| 226 | + static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5"; |
| 227 | + |
| 228 | + public static void main(String args[]) { |
| 229 | + |
| 230 | + ServiceArgs loginArgs = new ServiceArgs(); |
| 231 | + loginArgs.setPort(8089); |
| 232 | + loginArgs.setHost("localhost"); |
| 233 | + loginArgs.setScheme("https"); |
| 234 | + loginArgs.setToken(String.format("Bearer %s", token)); |
| 235 | + |
| 236 | + // Initialize the SDK client |
| 237 | + service = Service.connect(loginArgs); |
| 238 | + |
| 239 | + // Run a simple search by first creating the search job |
| 240 | + Job job = service.getJobs().create("search index=_internal | head 10"); |
| 241 | + |
| 242 | + // Waiting for search results to be ready |
| 243 | + while (!job.isReady()) { |
| 244 | + try { |
| 245 | + Thread.sleep(500); // 500 ms |
| 246 | + } catch (Exception e) { |
| 247 | + // Handle exception here. |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + // Read results |
| 252 | + try { |
| 253 | + ResultsReader reader = new ResultsReaderXml(job.getEvents()); |
| 254 | + |
| 255 | + // Iterate over events and print _raw field |
| 256 | + reader.forEach(event -> System.out.println(event.get("_raw"))); |
| 257 | + |
| 258 | + } catch (Exception e) { |
| 259 | + // Handle exception here. |
| 260 | + } |
| 261 | + } |
| 262 | +} |
| 263 | +``` |
| 264 | + |
| 265 | +For more information on authentication using tokens, please visit [Splunk Docs](https://docs.splunk.com/Documentation/Splunk/latest/Security/Setupauthenticationwithtokens). |
| 266 | + |
112 | 267 | ### Unit tests |
113 | 268 |
|
114 | 269 | The Splunk SDK for Java includes several unit tests that are run at |
|
0 commit comments