- 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] {}