可空的意義是什么?
case class StructField(
name: String,
dataType: DataType,
nullable: Boolean = true,
metadata: Metadata = Metadata.empty) {
從檔案來看,
StructField(name, dataType, nullable): 表示 StructType 中的一個欄位。欄位的名稱由名稱指示。欄位的資料型別由 dataType 指示。nullable 用于指示此欄位的值是否可以為空值。
僅作指示用嗎?因為我看不到它正在強制執行非空值(或者我錯過了什么?)
程式 :
val cols = "firstName:String:false,middlename:String:true,lastName:String:false,zipCode:String:false,sex:String:false,salary:Int:true"
def inferType(field: String): StructField = {
val splits = field.split(":")
val colName = splits(0)
val nullable = splits(2).toBoolean
val dataType = splits(1).toUpperCase() match {
case "INT" => IntegerType
case "DOUBLE" => DoubleType
case "STRING" => StringType
case _ => StringType
}
StructField(colName, dataType, nullable)
}
val schema: StructType = StructType(cols
.split(",")
.map(col => inferType(col)))
val simpleData = Seq(
Row("Soumya","","Kole","36636","M",-1),
Row("Foo","Bar","","","",9000)
)
val rdd = spark.sparkContext.parallelize(simpleData)
val df = spark.createDataFrame(rdd, schema)
df.printSchema()
df.show()
輸出:
root
|-- firstName: string (nullable = false)
|-- middlename: string (nullable = true)
|-- lastName: string (nullable = false)
|-- zipCode: string (nullable = false)
|-- sex: string (nullable = false)
|-- salary: integer (nullable = true)
--------- ---------- -------- ------- --- ------
|firstName|middlename|lastName|zipCode|sex|salary|
--------- ---------- -------- ------- --- ------
| Soumya| | Kole| 36636| M| -1|
| Foo| Bar| | | | 9000|
--------- ---------- -------- ------- --- ------
uj5u.com熱心網友回復:
空白是empty strings,不是NULLs。它們是不同的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338805.html
標籤:斯卡拉 数据框 阿帕奇火花 apache-spark-sql 模式
下一篇:依賴型別的型別類實體
