TIPS & TRICKS

Alternate JVM languages and the Splunk Java SDK

Speaking of Java as a language as opposed to the JVM platform, James Gosling, the Father of Java, said “Most people talk about Java the language, and this may sound odd coming from me, but I could hardly care less.”
He went on to explain, “What I really care about is the Java Virtual Machine as a concept, because that is the thing that ties it all together.”
– Quoted from theserverside.com

Alternate JVM languages are gaining momentum all over the place. These are languages that are focused on a particular paradigm, semantic ,programming style  or perhaps fill a niche roll that is better suited to coding in Java.

  • Scala
  • Groovy
  • Clojure
  • Jython
  • JRuby
  • Javascript(Rhino)
  • More here

We are  spoiled for choice with JVM languages .We do not need to get overly religious about “what is the best language” anymore, as so many online debates tend to descend into.I say choose the right tool for the job.You might have a hefty investment in some legacy Java coded librarys , but your new web framework might be written in a more object functional style using Scala, and perhaps a DSL(Domain Specific Language) and your test suites are written in Groovy , all co-existing harmoniously as Java bytecodes executing in the JVM.

The guys at dev.splunk.com have been doing some great work building various language SDK’s over the past few months.

And the Java SDK currently has very thorough Splunk REST endpoint coverage.

What’s great about this is that here you have one SDK that can actually become many when imported and used by other JVM languages.

Here are some trivial examples showing usage of the Java SDK from Groovy and Scala.
Both examples simply connect to the “/server/info” endpoint and dump the server info to STD OUT.
I’m not trying to win any prizes for code elegance here, just enough to show that I’m actually using the mentioned languages( and yes, the examples DO compile and run)

Using the Java SDK from Scala Code


import com.splunk.Service._
import scala.collection.mutable.HashMap
import scala.collection.JavaConversions._

object SplunkJavaSDKWrapper  {

  def main(args: Array[String]) = {

    //Connection to Service
    val connectionArgs = HashMap[String, Object]("host"->"somehost","username" -> "fred","password" ->"sweetmoves" )
    val service = connect(connectionArgs)  

    //get Splunk Server info
    val info = service.getInfo

    //Scala/Java Set conversion
    val javaSet = info.keySet
    val scalaSet = javaSet.toSet

    //print out Splunk Server info
    for(key <- scalaSet)
      println(key+":"+info.get(key))

  }

}

Using the Java SDK from Groovy Code


import com.splunk.Entity;
import com.splunk.Service;
import com.splunk.ServiceInfo;
import com.splunk.sdk.Command;

class SplunkJavaSDKWrapper {

  //groovy map
  static def splunkInfo = [:]

  static main(args) {

    //read in settings from .splunkrc
    Command command = Command.splunk()
    Service service = Service.connect(command.opts)

    //get Splunk Server info
    ServiceInfo info = service.getInfo()

    for (key in info.keySet())
      splunkInfo.put(key,info.get(key))

    printSplunkInfo()

  }

  //print out Splunk Server info
  static printSplunkInfo() {

    println "Info"
    splunkInfo.each { key, value ->println key + " : " + value}

  }

}

----------------------------------------------------
Thanks!
Damien Dallimore

Splunk
Posted by

Splunk

Join the Discussion