spark源码阅读笔记Dataset(三)structField、structType、schame

1、structField

源码结构:

[java]  view plain  copy
  1. case class StructField(  
  2.     name: String,  
  3.     dataType: DataType,  
  4.     nullable: Boolean = true,  
  5.     metadata: Metadata = Metadata.empty) {}  

-----A field inside a StructType
name:The name of this field.
dataType:The data type of this field.
nullable:Indicates if values of this field can be null values.
metadataThe metadata of this field. The metadata should be preserved during transformation if the content of the column is not modified, e.g, in selection.

一个结构体内部的 一个StructField就像一个SQL中的一个字段一样,它包含了這个字段的具体信息,可以看如下列子:
[java]  view plain  copy
  1. def schema_StructField()={  
  2. /** 
  3.   * StructField 是 一个 case class ,其中是否可以为空,默认是 true,初始元信息是为空 
  4.   * 它是作为描述 StructType中的一个字段 
  5.   */  
  6.     val sf = new StructField("b",IntegerType)  
  7.     println(sf.name)//b  
  8.     println(sf.dataType)//IntegerType  
  9.     println(sf.nullable)//true  
  10.     println(sf.metadata)//{}  
  11. }  

2、structType
A StructType object can be constructed by
[java]  view plain  copy
  1. StructType(fields: Seq[StructField])  
一个StructType对象,可以有多个StructField,同时也可以用名字(name)来提取,就想当于Map可以用key来提取value,但是他StructType提取的是整条字段的信息
在源码中structType是一个case class,如下:
[java]  view plain  copy
  1. case class StructType(fields: Array[StructField]) extends DataType with Seq[StructField] {}  
它是继承Seq的,也就是说Seq的操作,它都拥有,但是从形式上来说,每个元素是用  StructField包住的。
[java]  view plain  copy
  1. package Dataset  
  2.   
  3. import org.apache.spark.sql.types._  
  4.   
  5.   
  6. /** 
  7.   * Created by root on 9/21/16. 
  8.   */  
  9. object schemaAnalysis {  
  10.   //--------------------------------------------------StructType analysis---------------------------------------  
  11.   val struct = StructType(  
  12.     StructField("a", IntegerType) ::  
  13.       StructField("b", LongType, false) ::  
  14.       StructField("c", BooleanType, false) :: Nil)  
  15.   
  16.   def schema_StructType()={  
  17.     /** 
  18.       * 一个scheme是 
  19.       */  
  20.     import org.apache.spark.sql.types.StructType  
  21.     val schemaTyped = new StructType()  
  22.       .add("a","int").add("b","string")  
  23.     schemaTyped.foreach(println)  
  24.     /** 
  25.       * StructField(a,IntegerType,true) 
  26.       * StructField(b,StringType,true) 
  27.       */  
  28.   }  
  29.   def structType_extracted()={  
  30.   
  31.     // Extract a single StructField.  
  32.     val singleField_a = struct("a")  
  33.     println(singleField_a)  
  34.     //省却的清空下表示:可以为空的,  
  35.     //StructField(a,IntegerType,true)  
  36.     val singleField_b = struct("b")  
  37.     println(singleField_b)  
  38.     //StructField(b,LongType,false)  
  39.   
  40.     //val nonExisting = struct("d")  
  41.     //println(nonExisting)  
  42.     //java.lang.IllegalArgumentException: Field "d" does not exist.  
  43.   
  44.     // Extract multiple StructFields. Field names are provided in a set.  
  45.     // A StructType object will be returned.  
  46.     val twoFields = struct(Set("b""c"))  
  47.     println(twoFields)  
  48.   
  49.   
  50.     //StructType(StructField(b,LongType,false), StructField(c,BooleanType,false))  
  51.     // Any names without matching fields will be ignored.  
  52.     // For the case shown below, "d" will be ignored and  
  53.     // it is treated as struct(Set("b", "c")).  
  54.     val ignoreNonExisting = struct(Set("b""c""d"))  
  55.     println(ignoreNonExisting)  
  56.     // ignoreNonExisting: StructType =  
  57.     //   StructType(List(StructField(b,LongType,false), StructField(c,BooleanType,false)))  
  58.   
  59.     //值得注意的是:当没有存在的字段的时候,官方文档说:单个返回的是null,多个返回的是当没有那个字段  
  60.     //但是实验的时候,报错---Field d does not exist  
  61.     //源码调用的是apply方法,确实还没有处理好这部分功能  
  62.     //我是用的是spark2.0初始版本  
  63.   
  64.   }  
  65.   def structType_opration()={  
  66.   
  67.     /** 
  68.       * 源码:case class StructType(fields: Array[StructField]) extends DataType with Seq[StructField] { 
  69.       * 它是继承与Seq的,也就是说 Seq的操作,StructType都有 
  70.       * 可以查看scala的Seq的操作:http://www.scala-lang.org/api/current/#scala.collection.Seq 
  71.       */  
  72.     val tmpStruct = StructType(StructField("d", IntegerType)::Nil)  
  73.     //集合与集合的操作  
  74.     println(struct++tmpStruct)  
  75.     // println(struct++:tmpStruct)  
  76.     //List(StructField(a,IntegerType,true), StructField(b,LongType,false), StructField(c,BooleanType,false), StructField(d,IntegerType,true))  
  77.   
  78.     //集合与元素的操作  
  79.     println(struct :+ StructField("d", IntegerType))  
  80.   
  81.     //可以用add来进行  
  82.   
  83.     println(struct.add("e",IntegerType))  
  84.     //StructType(StructField(a,IntegerType,true), StructField(b,LongType,false), StructField(c,BooleanType,false), StructField(e,IntegerType,true))  
  85.   
  86.     //head 部分的元素  
  87.     println(struct.head)  
  88.     //StructField(a,IntegerType,true)  
  89.   
  90.   
  91.     //last 部分的元素  
  92.     println(struct.last)  
  93.     //StructField(c,BooleanType,false)  
  94.   
  95.     println(struct.apply("a"))  
  96.     //StructField(a,IntegerType,true)  
  97.   
  98.     println(struct.treeString)  
  99.   
  100.     /** 
  101.       * root 
  102.        |-- a: integer (nullable = true) 
  103.        |-- b: long (nullable = false) 
  104.        |-- c: boolean (nullable = false) 
  105.       */  
  106.   
  107.     println(struct.contains(StructField("f", IntegerType)))  
  108.     //false  
  109.   
  110.     println(struct.mkString)  
  111.     //StructField(a,IntegerType,true)StructField(b,LongType,false)StructField(c,BooleanType,false)  
  112.   
  113.     println(struct.prettyJson)  
  114.   
  115.     /** 
  116.       * { 
  117.           "type" : "struct", 
  118.           "fields" : [ { 
  119.             "name" : "a", 
  120.             "type" : "integer", 
  121.             "nullable" : true, 
  122.             "metadata" : { } 
  123.           }, { 
  124.             "name" : "b", 
  125.             "type" : "long", 
  126.             "nullable" : false, 
  127.             "metadata" : { } 
  128.           }, { 
  129.             "name" : "c", 
  130.             "type" : "boolean", 
  131.             "nullable" : false, 
  132.             "metadata" : { } 
  133.           } ] 
  134.         } 
  135.       */  
  136.     //更多操作可以查看API:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.types.StructType  
  137.   }  
  138.   
  139.   
  140.   
  141.   def main(args: Array[String]) {  
  142.     //schema_StructType()  
  143.     //structType_extracted()  
  144.     structType_opration()  
  145.   }  
  146. }  

3、Schema
---------Schema就是我们数据的 数据结构描述
一个Schema是一个数据结构的描述(比如描述一个Json文件),它可以是在运行的时候隐式导入,或者在编译的时候就导入。 它是用一个StructField集合对象的StructType描述(用一个三元tuple,内部是:name,type.nullability),本来有四个信息的为什么会说是三元数组? 其实metadata,你是可以调出来。

[java]  view plain  copy
  1. def schema_op()={  
  2.   case class Person(name: String, age: Long)  
  3.   val sparkSession = SparkSession.builder().appName("data set example")  
  4.     .master("local").getOrCreate()  
  5.   import sparkSession.implicits._  
  6.   val rdd = sparkSession.sparkContext.textFile("hdfs://master:9000/src/main/resources/people.txt")  
  7.   val dataSet = rdd.map(_.split(",")).map(p =>Person(p(0),p(1).trim.toLong)).toDS()  
  8.   println(dataSet.schema)  
  9.   //StructType(StructField(name,StringType,true), StructField(age,LongType,false))  
  10.   
  11.   
  12.   /** 
  13.     * def schema: StructType = queryExecution.analyzed.schema 
  14.     * 
  15.     * def apply(name: String): StructField = { 
  16.     * nameToField.getOrElse(name, 
  17.     * throw new IllegalArgumentException(s"""Field "$name" does not exist.""")) 
  18.     * } 
  19.     */  
  20.   val tmp: StructField = dataSet.schema("name")  
  21.   println(tmp)  
  22.   //StructField(name,StringType,true)  
  23.   
  24.   
  25.   println(tmp.name)//name  
  26.   println(tmp.dataType)//StringType  
  27.   println(tmp.nullable)//true  
  28.   println(tmp.metadata)//{}  



猜你喜欢

转载自blog.csdn.net/u014236541/article/details/80277192