Spark实⽤议题系列(03)---SparkSQL中的各种类型
(Types)
根据Spark的源码(路径://////////types/),我总结了如下图中的各种类型。
楚虽三户能亡秦
这⾥对图中的各个构件说明:
⼤框的标题是⽬录下各个⽂件的名字,例如AbstractDataType就是指⽂件AbstractDataType.scala。
⼤框中的圆弧⼩框就是各种类型(Types)的类(Class),⽽箭头的⽅向表明了它们的继承关系。
两个黄⾊的⼤框都是⽂件AbstractDataType.scala的内容,⽽这⾥只是为了显⽰更清晰的继承关系⽽将它们分开。
⿊⾊的圆弧⼩框表⽰这个类是private的,灰⾊的是protected,⽽没有颜⾊就是public的了。迷人远足
开学学习计划另外,从StructType,到StructField,到Metadata的虚箭头线,表⽰了这些类型实体的包含关系。
从图中可以看到,所有的类型都是DataType的⼦类。抽象类DataType定义了7个⽅法:defaultSize, typeName, simpleString, catalogString, sql, json, prettyJson,⽽其⼦类会重载这些⽅法以体现各个⼦类的特性。下⾯的例⼦可以得到各种⼦类在执⾏这些⽅法后得到的值:
import org.apache.pes._
ca class TypeMethods (
defaultSize: Int,
typeName: String,
simpleString: String,
catalogString: String,
sql: String,
//prettyJson: String,
json: String
)
val listTypes = Seq(
ByteType,
IntegerType,
LongType,
ShortType,
DecimalType(15,3),
DoubleType,
FloatType,
BinaryType,
BooleanType,
DateType,
TimestampType,
StringType,
刻什么求什么CharType(2),
VarcharType(3),
ArrayType(IntegerType,fal),
MapType(IntegerType,IntegerType, fal),
ObjectType(classOf[Integer]),
CalendarIntervalType,
NullType,
StructType(Array(StructField("id", IntegerType))) )
listTypes.map(s => TypeMethods(
s.defaultSize,
s.simpleString,
s.catalogString,
s.sql,
//s.prettyJson,
s.json
))
.toDF.show(fal)
得到的结果是:汉口学院分数线
+-----------+----------------+-----------------+-----------------+-----------------+-----------------------------------------------------------------------------------------+
|defaultSize|typeName |simpleString |catalogString |sql |json |
如何办理进出口权
+-----------+----------------+-----------------+-----------------+-----------------+-----------------------------------------------------------------------------------------+
|1 |byte |tinyint |tinyint |TINYINT |"byte" |
|4 |integer |int |int |INT |"integer" |
|8 |long |bigint |bigint |BIGINT |"long" |
|2 |short |smallint |smallint |SMALLINT |"short" |
|8 |decimal(15,3) |decimal(15,3) |decimal(15,3) |DECIMAL(15,3) |"decimal(15,3)" |
|8 |double |double |double |DOUBLE |"double" |
|4 |float |float |float |FLOAT |"float" |
|100 |binary |binary |binary |BINARY |"binary" |
|1 |boolean |boolean |boolean |BOOLEAN |"boolean" |
|4 |date |date |date |DATE |"date" |
|8 |timestamp |timestamp |timestamp |TIMESTAMP |"timestamp" |
|20 |string |string |string |STRING |"string" |
|2 |char |char(2) |char(2) |CHAR(2) |"char" |
|3 |varchar |varchar(3) |varchar(3) |VARCHAR(3) |"varchar" |
|4 |array |array<int> |array<int> |ARRAY<INT> |{"type":"array","elementType":"integer","containsNull":fal} |
|8 |map |map<int,int> |map<int,int> |MAP<INT, INT> |{"type":"map","keyType":"integer","valueType":"integer","valueContainsNull":fal} | |4096 |object |java.lang.Integer|java.lang.Integer|JAVA.LANG.INTEGER|"object" |
|16 |calendarinterval|calendarinterval |calendarinterval |CALENDARINTERVAL |"calendarinterval" |
|1 |null |null |null |NULL |"null" |
|4 |struct |struct<id:int> |struct<id:int> |STRUCT<`id`: INT>|{"type":"struct","fields":[{"name":"id","type":"integer","nullable":true,"metadata":{}}]}|
+-----------+----------------+-----------------+-----------------+-----------------+-----------------------------------------------------------------------------------------+
⽽单独来看函数prettyJson的输出:
listTypes.map(_.prettyJson).toDF.show(fal)
结果显⽰:
+--------------------------------------------------------------------------------------------------------------------------------------+ |value |
+--------------------------------------------------------------------------------------------------------------------------------------+ |"byte" | |"integer" |
|"long" |
|"short" |
|"decimal(15,3)" | |"double" |
|"float" |
|"binary" | |"boolean" |
|"date" | |"timestamp" | |"string" |
|"char" | |"varchar" |
|{
"type" : "array",
"elementType" : "integer",
"containsNull" : fal
} |
|{
"type" : "map",数学五年级下
"keyType" : "integer",
"valueType" : "integer",
"valueContainsNull" : fal左边用英语怎么说
} |
|"object" | |"calendarinterval" |
|"null" |
|{
"type" : "struct",
"fields" : [ {
"name" : "id",
"type" : "integer",
"nullable" : true,
"metadata" : { }
} ]
}|
+--------------------------------------------------------------------------------------------------------------------------------------+