Spark数据类型官网权威详解
文章目录
一、数据类型
Spark SQL和DataFrames支持以下数据类型:
- 数值类型
ByteType
:表示1字节有符号整数。数字范围为-128
到127
。ShortType
:表示2字节有符号整数。数字范围为-32768
到32767
。IntegerType
:表示4字节有符号整数。数字范围为-2147483648
到2147483647
。LongType
:表示8字节有符号整数。数字范围为-9223372036854775808
到9223372036854775807
。FloatType
:表示4字节单精度浮点数。DoubleType
:表示8字节双精度浮点数。DecimalType
:表示任意精度的有符号十进制数。在内部由java.math.BigDecimal
支持。BigDecimal
由一个任意精度的整数无标度值和一个32位整数标度组成。
- 字符串类型
StringType
:表示字符字符串值。
- 二进制类型
BinaryType
:表示字节序列值。
- 布尔类型
BooleanType
:表示布尔值。
- 日期时间类型
TimestampType
:表示由年、月、日、小时、分钟和秒组成的值。DateType
:表示由年、月、日组成的值。
- 复杂类型
ArrayType(elementType, containsNull)
:表示由具有elementType
类型的元素序列组成的值。containsNull
用于指示ArrayType
值中的元素是否可以为null
。MapType(keyType, valueType, valueContainsNull)
:表示由一组键值对组成的值。键的数据类型由keyType
描述,值的数据类型由valueType
描述。对于MapType
值,键不允许有null
值。valueContainsNull
用于指示MapType
值的值是否可以为null
。StructType(fields)
:表示具有由StructField
(fields
)序列描述的结构的值。StructField(name, dataType, nullable)
:表示StructType
中的字段。字段的名称由name
指示。字段的数据类型由dataType
指示。nullable
用于指示此字段的值是否可以为null
。
二、访问源码
Spark SQL的所有数据类型都位于包org.apache.spark.sql.types
中。您可以通过以下方式访问它们:
Scala:
import org.apache.spark.sql.types._
Java:
import org.apache.spark.sql.types.*;
Python:
from pyspark.sql.types import *
R:
library(SparkR)
三、多语言数据类型映射
下表列出了各个语言中的数据类型及其对应的值类型和API:
1. Scala中的数据类型和访问或创建数据类型的API:
数据类型 | 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() |
ArrayType | scala.collection.Seq | ArrayType(elementType, containsNull) |
MapType | scala.collection.Map | MapType(keyType, valueType, valueContainsNull) |
StructType | org.apache.spark.sql.Row | StructType(fields) |
StructField | 数据类型的值类型(例如,Int对于数据类型为IntegerType的StructField) | StructField(name, dataType, nullable) |
2.Java中的数据类型和访问或创建数据类型的API:
数据类型 | Java中的值类型 | 访问或创建数据类型的API |
---|---|---|
ByteType | byte或Byte | DataTypes.ByteType |
ShortType | short或Short | DataTypes.ShortType |
IntegerType | int或Integer | DataTypes.IntegerType |
LongType | long或Long | DataTypes.LongType |
FloatType | float或Float | DataTypes.FloatType |
DoubleType | double或Double | DataTypes.DoubleType |
DecimalType | java.math.BigDecimal | DataTypes.createDecimalType() DataTypes.createDecimalType(precision, scale) |
StringType | String | DataTypes.StringType |
BinaryType | byte[] | DataTypes.BinaryType |
BooleanType | boolean或Boolean | DataTypes.BooleanType |
TimestampType | java.sql.Timestamp | DataTypes.TimestampType |
DateType | java.sql.Date | DataTypes.DateType |
ArrayType | java.util.List | DataTypes.createArrayType(elementType) 注意:containsNull的值将为true DataTypes.createArrayType(elementType, containsNull) |
MapType | java.util.Map | DataTypes.createMapType(keyType, valueType) 注意:valueContainsNull的值将为true DataTypes.createMapType(keyType, valueType, valueContainsNull) |
StructType | org.apache.spark.sql.Row | DataTypes.createStructType(fields) 注意:fields是一个StructFields的List或数组,不允许有相同名称的两个字段 |
StructField | 字段的Java值类型 (例如,整数对应数据类型IntegerType) | DataTypes.createStructField(name, dataType, nullable) |
3.Python中的数据类型和访问或创建数据类型的API:
数据类型 | Python中的值类型 | 访问或创建数据类型的API |
---|---|---|
ByteType | int或long | ByteType() |
ShortType | int或long | ShortType() |
IntegerType | int或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() |
ArrayType | list、tuple或array | ArrayType(elementType, [containsNull]) 注意:containsNull的默认值为True |
MapType | dict | MapType(keyType, valueType, [valueContainsNull]) 注意:valueContainsNull的默认值为True |
StructType | list或tuple | StructType(fields) 注意:fields是一个StructFields的Seq,不允许有相同名称的两个字段 |
StructField | 字段的Python值类型 (例如,整数对应数据类型IntegerType) | StructField(name, dataType, [nullable]) 注意:nullable的默认值为True |
4.R中的数据类型和访问或创建数据类型的API:
数据类型 | R中的值类型 | 访问或创建数据类型的API |
---|---|---|
ByteType | integer | “byte” |
ShortType | integer | “short” |
IntegerType | integer | “integer” |
LongType | integer | “long” |
FloatType | numeric | “float” |
DoubleType | numeric | “double” |
DecimalType | 不支持 | 不支持 |
StringType | character | “string” |
BinaryType | raw | “binary” |
BooleanType | logical | “bool” |
TimestampType | POSIXct | “timestamp” |
DateType | Date | “date” |
ArrayType | vector或list | list(type=“array”, elementType=elementType, containsNull=[containsNull]) 注意:containsNull的默认值为TRUE |
MapType | environment | list(type=“map”, keyType=keyType, valueType=valueType, valueContainsNull=[valueContainsNull]) 注意:valueContainsNull的默认值为TRUE |
StructType | named list | list(type=“struct”, fields=fields) 注意:fields是一个StructFields的Seq,不允许有相同名称的两个字段 |
StructField | 字段的R值类型 (例如,整数对应数据类型IntegerType) | list(name=name, type=dataType, nullable=[nullable]) 注意:nullable的默认值为TRUE |
四、NaN语义:
- NaN = NaN 返回true。
- 在聚合操作中,所有的NaN值会被分组在一起。
- NaN在连接键中被视为正常值。
- 当按升序排列时,NaN值会排在最后,比任何其他数值都大。
五、算术运算:
对于数字类型(除了decimal
),不会检查溢出的情况。这意味着如果一个操作导致溢出,在Java/Scala程序中返回的结果与该操作相同(例如,如果两个整数相加的和超过了可表示的最大值,结果将是一个负数)。