数据类型

支持的数据类型

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, 或 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的值类型为Int)
StructField( name , dataType , [ nullable ])
注意: nullable 的默认值为True。

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

import org.apache.spark.sql.types._
Find full example code at "examples/src/main/scala/org/apache/spark/examples/sql/SparkSQLExample.scala" in the Spark repo.
数据类型 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 或 java.sql.Timestamp TimestampType
TimestampNTZType java.time.LocalDateTime TimestampNTZType
DateType java.time.LocalDate 或 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 是一个 StructFields 的 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或java.sql.Timestamp DataTypes.TimestampType
TimestampNTZType java.time.LocalDateTime DataTypes.TimestampNTZType
DateType java.time.LocalDate或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 是一个List或StructFields的数组。同时,不允许有两个同名的字段。
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 or list list(type=”array”, elementType= elementType , containsNull=[ containsNull ])
注意: containsNull 的默认值为 TRUE。
MapType environment list(type=”map”, keyType= keyType , valueType= valueType , valueContainsNull=[ valueContainsNull ])
注意: valueContainsNull 的默认值为 TRUE。
StructType 命名列表 list(type=”struct”, fields= fields )
注意: fields 是 StructFields 的 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
StructType STRUCT
注意: ‘:’ 是可选的。
MapType MAP

浮点数特殊值

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

正/负无穷语义

对正无穷和负无穷有特殊处理。它们具有以下语义:

NaN 语义

在处理不是真正数字(NaN)时,对 float double 类型有特殊处理,这些类型不完全符合标准浮点语义。具体来说:

示例

SELECT double('infinity') AS col;
+--------+
| col|
+--------+
|无限大|
+--------+
SELECT float() AS col;
+---------+
| col|
+---------+
|-无限大|
+---------+
SELECT float() AS col;
+---+
|col|
+---+
|非数字|
+---+
SELECT double() * 0 AS col;
+---+
|col|
+---+
|非数字|
+---+
SELECT double() * (-1234567) AS col;
+--------+
| col|
+--------+
|无限大|
+--------+
SELECT double() < double() AS col;
+----+
| col|
+----+
|true|
+----+
SELECT double() = double() AS col;
+----+
| col|
+----+
|true|
+----+
SELECT double() = double() AS col;
+----+
| col|
+----+
|true|
+----+
CREATE TABLE test (c1 int, c2 double);
INSERT INTO test VALUES (1, double());
INSERT INTO test VALUES (2, double());
INSERT INTO test VALUES (3, double());
INSERT INTO test VALUES (4, double());
INSERT INTO test VALUES (5, double());
INSERT INTO test VALUES (6, double());
INSERT INTO test VALUES (7, double());
SELECT COUNT(*), c2 FROM test GROUP BY c2;
+---------+---------+
| count(1)| c2|
+---------+---------+
| 2| 非数字|
| 2|-无限大|
| 3| 无限大|
+---------+---------+