数据类型

支持的数据类型

Spark SQL 和 DataFrames 支持以下数据类型

Spark SQL 的所有数据类型都位于 pyspark.sql.types 包中。您可以通过执行以下操作来访问它们

from pyspark.sql.types import *
数据类型 Python 中的值类型 访问或创建数据类型的 API
ByteType int 或 long
注意: 数字将在运行时转换为 1 字节有符号整数。请确保数字在 -128 到 127 的范围内。
ByteType()
ShortType int 或 long
注意: 数字将在运行时转换为 2 字节有符号整数。请确保数字在 -32768 到 32767 的范围内。
ShortType()
IntegerType int 或 long IntegerType()
LongType long
注意: 数字在运行时将被转换为 8 字节的有符号整数。请确保数字在 -9223372036854775808 到 9223372036854775807 的范围内。 否则,请将数据转换为 decimal.Decimal 并使用 DecimalType。
LongType()
FloatType float
注意: 数字在运行时将被转换为 4 字节单精度浮点数。
FloatType()
DoubleType float DoubleType()
DecimalType decimal.Decimal DecimalType()
StringType string StringType()
BinaryType bytearray BinaryType()
BooleanType bool BooleanType()
TimestampType datetime.datetime TimestampType()
TimestampNTZType datetime.datetime TimestampNTZType()
DateType datetime.date DateType()
DayTimeIntervalType datetime.timedelta DayTimeIntervalType()
ArrayType list, tuple, or array (列表,元组或数组) ArrayType(elementType, [containsNull])
注意:containsNull 的默认值为 True。
MapType dict MapType(keyType, valueType, [valueContainsNull])
注意:valueContainsNull 的默认值为 True。
StructType list or tuple (列表或元组) StructType(fields)
注意: fields 是 StructField 的 Seq。此外,不允许使用相同名称的两个字段。
StructField 此字段的数据类型的 Python 值类型
(例如,对于数据类型为 IntegerType 的 StructField,值为 Int)
StructField(name, dataType, [nullable])
注意: nullable 的默认值为 True。

Spark SQL 的所有数据类型都位于 org.apache.spark.sql.types 包中。您可以通过以下方式访问它们:

import org.apache.spark.sql.types._
在 Spark 仓库的 "examples/src/main/scala/org/apache/spark/examples/sql/SparkSQLExample.scala" 中查找完整的示例代码。
数据类型 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.time.Instant or java.sql.Timestamp TimestampType
TimestampNTZType java.time.LocalDateTime TimestampNTZType
DateType java.time.LocalDate or java.sql.Date DateType
YearMonthIntervalType java.time.Period YearMonthIntervalType
DayTimeIntervalType java.time.Duration DayTimeIntervalType
ArrayType scala.collection.Seq ArrayType(elementType, [containsNull])
注意: containsNull 的默认值为 true。
MapType scala.collection.Map MapType(keyType, valueType, [valueContainsNull])
注意: valueContainsNull 的默认值为 true。
StructType org.apache.spark.sql.Row StructType(fields)
注意: fields 是 StructField 的 Seq。此外,不允许使用相同名称的两个字段。
StructField 此字段的数据类型的 Scala 值类型(例如,对于数据类型为 IntegerType 的 StructField,值为 Int) StructField(name, dataType, [nullable])
注意: nullable 的默认值为 true。

Spark SQL 的所有数据类型都位于 org.apache.spark.sql.types 包中。 要访问或创建数据类型,请使用 org.apache.spark.sql.types.DataTypes 中提供的工厂方法。

数据类型 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.time.Instant or java.sql.Timestamp DataTypes.TimestampType
TimestampNTZType java.time.LocalDateTime DataTypes.TimestampNTZType
DateType java.time.LocalDate or java.sql.Date DataTypes.DateType
YearMonthIntervalType java.time.Period DataTypes.YearMonthIntervalType
DayTimeIntervalType java.time.Duration DataTypes.DayTimeIntervalType
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 是 StructField 的 List 或数组。此外,不允许使用相同名称的两个字段。
StructField 此字段的数据类型的 Java 值类型(例如,对于数据类型为 IntegerType 的 StructField,值为 int) DataTypes.createStructField(name, dataType, nullable)
数据类型 R 中的值类型 访问或创建数据类型的 API
ByteType integer
注意: 数字将在运行时转换为 1 字节有符号整数。请确保数字在 -128 到 127 的范围内。
“byte”
ShortType integer
注意: 数字将在运行时转换为 2 字节有符号整数。请确保数字在 -32768 到 32767 的范围内。
“short”
IntegerType integer “integer”
LongType integer
注意: 数字在运行时将被转换为 8 字节的有符号整数。请确保数字在 -9223372036854775808 到 9223372036854775807 的范围内。 否则,请将数据转换为 decimal.Decimal 并使用 DecimalType。
“long”
FloatType numeric
注意: 数字在运行时将被转换为 4 字节单精度浮点数。
“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 是 StructField 的 Seq。此外,不允许使用相同名称的两个字段。
StructField 此字段的数据类型的 R 值类型(例如,对于数据类型为 IntegerType 的 StructField,值为 integer) list(name=name, type=dataType, nullable=[nullable])
注意: nullable 的默认值为 TRUE。

下表显示了 Spark SQL 解析器中用于每种数据类型的类型名称以及别名。

数据类型 SQL 名称
BooleanType BOOLEAN
ByteType BYTE, TINYINT
ShortType SHORT, SMALLINT
IntegerType INT, INTEGER
LongType LONG, BIGINT
FloatType FLOAT, REAL
DoubleType DOUBLE
DateType DATE
TimestampType TIMESTAMP, TIMESTAMP_LTZ
TimestampNTZType TIMESTAMP_NTZ
StringType STRING
BinaryType BINARY
DecimalType DECIMAL, DEC, NUMERIC
YearMonthIntervalType INTERVAL YEAR, INTERVAL YEAR TO MONTH, INTERVAL MONTH
DayTimeIntervalType INTERVAL DAY, INTERVAL DAY TO HOUR, INTERVAL DAY TO MINUTE, INTERVAL DAY TO SECOND, INTERVAL HOUR, INTERVAL HOUR TO MINUTE, INTERVAL HOUR TO SECOND, INTERVAL MINUTE, INTERVAL MINUTE TO SECOND, INTERVAL SECOND
ArrayType ARRAY<element_type>
StructType STRUCT<field1_name: field1_type, field2_name: field2_type, …>
注意: ‘:’ 是可选的。
MapType MAP<key_type, value_type>

浮点特殊值

Spark SQL 以不区分大小写的方式支持几个特殊的浮点值

正/负无穷大语义

正无穷大和负无穷大有特殊的处理方式。它们具有以下语义

NaN 语义

在处理与标准浮点语义不完全匹配的 floatdouble 类型时,会对非数字 (NaN) 进行特殊处理。 具体来说

示例

SELECT double('infinity') AS col;
+--------+
|     col|
+--------+
|Infinity|
+--------+

SELECT float('-inf') AS col;
+---------+
|      col|
+---------+
|-Infinity|
+---------+

SELECT float('NaN') AS col;
+---+
|col|
+---+
|NaN|
+---+

SELECT double('infinity') * 0 AS col;
+---+
|col|
+---+
|NaN|
+---+

SELECT double('-infinity') * (-1234567) AS col;
+--------+
|     col|
+--------+
|Infinity|
+--------+

SELECT double('infinity') < double('NaN') AS col;
+----+
| col|
+----+
|true|
+----+

SELECT double('NaN') = double('NaN') AS col;
+----+
| col|
+----+
|true|
+----+

SELECT double('inf') = double('infinity') AS col;
+----+
| col|
+----+
|true|
+----+

CREATE TABLE test (c1 int, c2 double);
INSERT INTO test VALUES (1, double('infinity'));
INSERT INTO test VALUES (2, double('infinity'));
INSERT INTO test VALUES (3, double('inf'));
INSERT INTO test VALUES (4, double('-inf'));
INSERT INTO test VALUES (5, double('NaN'));
INSERT INTO test VALUES (6, double('NaN'));
INSERT INTO test VALUES (7, double('-infinity'));
SELECT COUNT(*), c2 FROM test GROUP BY c2;
+---------+---------+
| count(1)|       c2|
+---------+---------+
|        2|      NaN|
|        2|-Infinity|
|        3| Infinity|
+---------+---------+