【发布时间】:2015-08-26 06:47:36
【问题描述】:
我正在尝试从 spark sql 导入类型,如下所示
导入 org.apache.spark.sql.types._
但我收到错误消息,例如:“未找到:值 DataType”、“未找到:类型 ByteType”
完整代码是
import java.math.BigDecimal
import java.sql.{Timestamp, Date}
import org.apache.spark.sql.types._
/**
* Utility functions for type casting
*/
object TypeCast {
/**
* Casts given string datum to specified type.
* Currently we do not support complex types (ArrayType, MapType, StructType).
*
* @param datum string value
* @param castType SparkSQL type
*/
def castTo(datum: String, castType: DataType): Any = {
castType match {
case _: ByteType => datum.toByte
case _: ShortType => datum.toShort
case _: IntegerType => datum.toInt
case _: LongType => datum.toLong
case _: FloatType => datum.toFloat
case _: DoubleType => datum.toDouble
case _: BooleanType => datum.toBoolean
case _: DecimalType => new BigDecimal(datum.replaceAll(",", ""))
case _: TimestampType => Timestamp.valueOf(datum)
case _: DateType => Date.valueOf(datum)
case _: StringType => datum
case _ => throw new RuntimeException(s"Unsupported type: ${castType.typeName}")
}
}
【问题讨论】:
-
大概您/没有收到导入错误,但稍后。向我们展示代码。并且“未找到:value DataType”表明编译器此时不期望类型...
-
更新了原来的问题
-
我没有收到任何“未找到:值 DataType”错误,另外从源 IntType 等看是案例对象,而不是类型/类。