通过类型的大小写匹配根据SparkSQL中的类型转换值 [英] Convert value depending on a type in SparkSQL via case matching of type

查看:326
本文介绍了通过类型的大小写匹配根据SparkSQL中的类型转换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Scala中匹配参数类型?可以说我有一个接收两个参数的函数:valuetype.我想使用模式匹配来进行类型转换.

Is it possible to match a parametric type in Scala? Lets say I have a function that receives two parameters: a value and a type. I would like to use pattern matching to do a type conversion.

类似这样的东西:

datatype match {
  case IntegerType => return value.toInt
  case FloatType => return value.toFloat
  case StringType => return value
  case DecimalType(_,_) => return BigDecimal(value) // this is not working
  case _ => return strrepr
}

此处DecimalType接受两个参数以指定精度和所需精度.例如:

Here DecimalType accepts two parameters to specify precision the required precision. It can be for example:

org.apache.spark.sql.types.DecimalType = DecimalType(10,2)

我尝试了几种选择,但似乎无济于事:

I have tried several options and nothing seems to be working:

  • 对于 case DecimalType => return BigDecimal(value) ,我得到:

  • For case DecimalType => return BigDecimal(value) I get:

error: pattern type is incompatible with expected type;
   found   : org.apache.spark.sql.types.DecimalType.type
   required: org.apache.spark.sql.types.DataType
   Note: if you intended to match against the class, try `case DecimalType(_,_)`

  • 对于 case DecimalType(_,_) => return BigDecimal(value) ,我得到:

  • For case DecimalType(_,_) => return BigDecimal(value) I get:

    error: result type Boolean of unapply defined in method unapply in object DecimalType does not conform to Option[_] or Boolean
    

  • 对于 case DecimalType[_,_] => return BigDecimal(value) ,我得到:

  • For case DecimalType[_,_] => return BigDecimal(value) I get:

    error: org.apache.spark.sql.types.DecimalType does not take type parameters
    

  • 推荐答案

    问题是您的代码中使用了return.您说过您在某处的函数中使用了此代码段.该函数的返回类型是什么?显然,您打算有时是Integer,有时是String,有时是BigDecimal.但是如果使用return,它将查找返回对象的类型,以确定函数的返回类型.通常,您应该强烈避免在Scala代码中使用return.返回函数主体中的最后一个评估值.使用return的唯一情况是要在函数主体中的其他位置强制返回值.但是,更好的方法是将返回对象保存在变量中,然后仅在函数体的最后一行评估该变量.永远不要使用return!

    The problem is the use of the return in your code. You said you use this code snippet in a function somewhere. What is the return type of that function? Obviously, you intend that sometimes it is Integer, sometimes String, sometimes BigDecimal; but if you use return, it will look to the type of the returned object to determine the return type of the function. In general, you should strongly avoid using return in Scala code. The last evaluated value in the function body is returned. The only case for using a return is when you want to force returning a value somewhere else in the function body. But still, a better way would be to save the return object in a variable and just evaluate that variable in the last line of your function body. And never use return!

    在没有return的情况下有效

    Without return it works

    scala> val datatype = DecimalType(10, 2)
    datatype: org.apache.spark.sql.types.DecimalType = DecimalType(10,2)
    
    scala> val value = BigDecimal(10)
    value: scala.math.BigDecimal = 10
    
    scala> datatype match {case DecimalType(_,_) => value}
    res150: scala.math.BigDecimal = 10
    

    **返回问题**

    scala> def test = {datatype match {case DecimalType(_,_) => return value}}
    <console>:138: error: method test has return statement; needs result type
           def test = {datatype match {case DecimalType(_,_) => return value}}
    
    scala> def test:BigDecimal = {datatype match {case DecimalType(_,_) => return value}}
    test: BigDecimal
    
    scala> def test:DataType = {datatype match {case DecimalType(_,_) => return value}}
    <console>:138: error: type mismatch;
     found   : scala.math.BigDecimal
     required: org.apache.spark.sql.types.DataType
           def test:DataType = {datatype match {case DecimalType(_,_) => return value}}
    
    scala> def test3 = {datatype match {case DecimalType(_,_) => value}}
    test3: scala.math.BigDecimal
    

    这篇关于通过类型的大小写匹配根据SparkSQL中的类型转换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆