Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

scala - udf No TypeTag available for type string

I don't understand a behavior of spark.

I create an udf wich return an Integer like below

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

object Show {

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


    val (sc,sqlContext) = iniSparkConf("test")
    val testInt_udf = sqlContext.udf.register("testInt_udf", testInt _)

  }

  def iniSparkConf(appName: String): (SparkContext, SQLContext) = {
    val conf = new SparkConf().setAppName(appName)//.setExecutorEnv("spark.ui.port", "4046")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")
    val sqlContext = new SQLContext(sc)

    (sc, sqlContext)
  }
  def testInt() : Int= {
    return 2
  }
}

I work perfectly but if I change the return type of method test from Int to String

val testString_udf = sqlContext.udf.register("testString_udf", testString _)
def testString() : String = {
  return "myString"
}

I get the following error

Error:(34, 43) No TypeTag available for String
    val testString_udf = sqlContext.udf.register("testString_udf", testString _)
Error:(34, 43) not enough arguments for method register: (implicit evidence$1: reflect.runtime.universe.TypeTag[String])org.apache.spark.sql.UserDefinedFunction.
Unspecified value parameter evidence$1.
    val testString_udf = sqlContext.udf.register("testString_udf", testString _)

here are my embedded jars:

datanucleus-api-jdo-3.2.6
datanucleus-core-3.2.10
datanucleus-rdbms-3.2.9
spark-1.6.1-yarn-shuffle
spark-assembly-1.6.1-hadoop2.6.0
spark-examples-1.6.1-hadoop2.6.0

I am a little bit lost... Do you have any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since I can't reproduce the issue copy-pasting just your example code into a new file, I bet that in your real code String is actually shadowed by something else. To verify this theory you can try to change you signature to

def testString() : scala.Predef.String = {
  return "myString"
}

or

def testString() : java.lang.String = {
  return "myString"
}

If this one compiles, search for "String" to see how you shadowed the standard type. If you use IntelliJ Idea, you can try to use "Ctrl+B" (GoTo) to find it out. The most obvious candidate is that you used String as a name of generic type parameter but there might be some other choices.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...