spark3.0教程-SparkSQL:数据类型

说明

类似 MySQL、Oracle数据库中的数据类型,SparkSQL 为了运算,也有 数据类型的概念

官网:
https://spark.apache.org/docs/3.0.0/sql-ref-datatypes.html

数据类型

数字类型

  • ByteType:代表一个字节的整数。范围是-128到127
  • ShortType:代表两个字节的整数。范围是-32768到32767
  • IntegerType:代表4个字节的整数。范围是-2147483648到2147483647
  • LongType:代表8个字节的整数。范围是-9223372036854775808到9223372036854775807
  • FloatType:代表4字节的单精度浮点数
  • DoubleType:代表8字节的双精度浮点数
  • DecimalType:代表任意精度的10进制数据。通过内部的java.math.BigDecimal支持。BigDecimal由一个任意精度的整型非标度值和一个32位整数组成

字符串类型

  • StringType:代表一个字符串值

二进制类型

  • BinaryType:代表一个byte序列值

布尔类型

  • BooleanType:代表boolean值

Datetime类型

  • TimestampType:代表包含字段年,月,日,时,分,秒的值
  • DateType:代表包含字段年,月,日的值

复杂类型

  • ArrayType(elementType, containsNull):代表由elementType类型元素组成的序列值。containsNull用来指明ArrayType中的值是否有null值
  • MapType(keyType, valueType, valueContainsNull):表示包括一组键 - 值对的值。通过keyType表示key数据的类型,通过valueType表示value数据的类型。valueContainsNull用来指明MapType中的值是否有null值
  • StructType(fields):表示一个拥有StructFields (fields)序列结构的值
  • StructField(name, dataType, nullable):代表StructType中的一个字段,字段的名字通过name指定,dataType指定field的数据类型,nullable表示字段的值是否有null值。

scala

导包

import org.apache.spark.sql.types._
数据类型 Scala中的类型 访问或者创建数据类型的API
ByteType Byte ByteType
ShortType Short ShortType
IntegerType Int IntegerType
LongType Long LongType
FloatType Float FloatType
DoubleType Double DoubleType
DecimalType java.math.BigDecimal DecimalType
StringType String StringType
BinaryType Array[Byte] BinaryType
BooleanType Boolean BooleanType
TimestampType java.sql.Timestamp TimestampType
DateType java.sql.Date DateType

案例

spark3.0教程-SparkSQL:RDD转DataFrame

python

导包

from pyspark.sql.types import *
数据类型 python中的类型 访问或者创建数据类型的API
ByteType int or long ByteType()
ShortType int or long ShortType()
IntegerType int or long IntegerType()
LongType long LongType()
FloatType float FloatType()
DoubleType float DoubleType()
DecimalType decimal.Decimal DecimalType()
StringType string StringType()
BinaryType bytearray BinaryType()
BooleanType bool BooleanType()
TimestampType datetime.datetime TimestampType()
DateType datetime.date DateType()

感谢:
https://endymecy.gitbooks.io/spark-programming-guide-zh-cn/content/spark-sql/spark-sql-dataType-reference.html


原文出处:/show_1IX3MFcE7USA.html