1. 程式人生 > 其它 >Spark StructType 型別詳解

Spark StructType 型別詳解

技術標籤:sparkscalaspark構造器scalathisStructType

  • StructType 是個case class,一般用於構建schema.
  • 因為是case class,所以使用的時候可以不用new關鍵字

建構函式

可以傳入Seq,java的List,scala的Array,都是可以的~
在這裡插入圖片描述
還可以用無參的構造器,因為它有一個無參的構造器.

例子

  private val schema: StructType = StructType(List(
    StructField("name", DataTypes.StringType),
    StructField(
"age", DataTypes.IntegerType) ))

也可以是

  private val schema: StructType = StructType(Array(
    StructField("name", DataTypes.StringType),
    StructField("age", DataTypes.IntegerType)
  ))

還可以呼叫無參構造器,這麼寫

private val schema = (new StructType)
    .add(StructField("name"
, DataTypes.StringType)) .add(StructField("age", DataTypes.IntegerType))

這個無參的構造器,呼叫了一個有參構造器.this裡面是個方法,這個方法的返回值是Array型別,實際上就是無參構造器呼叫了主構造器

def this() = this(Array.empty[StructField])
case class StructType(fields: Array[StructField]) extends DataType with Seq[StructField] {}