資料類型

支援的資料類型

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 或陣列 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._
在 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 或 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 是 StructFields 的 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 環境 list(type=”map”,keyType=keyType,valueType=valueType,valueContainsNull=[valueContainsNull])
注意:valueContainsNull 的預設值為 TRUE。
StructType 命名清單 list(type=”struct”,fields=fields)
注意:fields 是 StructFields 的 Seq。此外,不允許兩個名稱相同的欄位。
StructField 此欄位資料類型的 R 值類型(例如,資料類型為 IntegerType 的 StructField 為整數) 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 語意

在處理不為數字 (NaN) 時,對於不完全符合標準浮點語意的 floatdouble 類型,會有特殊處理。特別是

範例

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|
+---------+---------+