Home > Back-end >  How can I use this Java function in Scala in a UDF?
How can I use this Java function in Scala in a UDF?

Time:11-06

I have created a UDF in Scala (that im using with Spark btw) in order to get as a parameter a string and output a BeiderMorseEncoder string. I am using the org.apache.commons.codec.language.bm.BeiderMorseEncoder Java function from Apache Commons

import org.apache.commons.codec.language.bm.BeiderMorseEncoder
class BeiderMorseEncode extends UDF1[String, String] {
  override def call(input: String): String = {
    val m = new BeiderMorseEncoder()
    m.encode(input)
  }
}

object BeiderMorseEncode {
  def apply(): BeiderMorseEncode = {
    new BeiderMorseEncode()
  }
}

It works great! However I want to also use the following function (click to see signature) org.apache.commons.codec.language.bm.Lang.guessLanguage

if I try to create a similar UDF for this function in Scala like this :

import org.apache.commons.codec.language.bm.Lang
class guessNameLanguage extends UDF1[String, String] {
  override def call(input: String): String = {


    val m = new Lang()
    m.guessLanguage(input)
  }
}

object guessNameLanguage {
  def apply(): guessNameLanguage = {
    new guessNameLanguage()
  }
}

I am getting

org.apache.commons.codec.language.bm.Lang does not have a constructor

Any ideas on how can I make this work? I undersntand that I need to instantiate an object that has a constructor first ... but having had a look on the class hierarchy I dont see what object that would be. (it is obviously not Lang)

Apologies for my cringy Scala.

CodePudding user response:

If you look closely at the Javadoc you will see that the class provides two static methods to get instances of it.

So your code should end up being like this:

import org.apache.commons.codec.language.bm.{Lang, NameType}

val m = Lang.instance(NameType.GENERIC)
m.guessLanguage(input)
  • Related