移轉指南:SQL、資料集和資料框架

從 Spark SQL 3.4 升級到 3.5

從 Spark SQL 3.3 升級到 3.4

從 Spark SQL 3.2 升級到 3.3

從 Spark SQL 3.1 升級到 3.2

從 Spark SQL 3.0 升級至 3.1

從 Spark SQL 3.0.1 升級至 3.0.2

從 Spark SQL 3.0 升級至 3.0.1

從 Spark SQL 2.4 升級至 3.0

Dataset/DataFrame API

DDL 語法

UDF 及內建函數

查詢引擎

資料來源

其他

從 Spark SQL 2.4.7 升級至 2.4.8

從 Spark SQL 2.4.5 升級至 2.4.6

從 Spark SQL 2.4.4 升級至 2.4.5

從 Spark SQL 2.4.3 升級至 2.4.4

從 Spark SQL 2.4 升級至 2.4.1

從 Spark SQL 2.3 升級至 2.4

從 Spark SQL 2.2 升級至 2.3

</thead> <tr> <td> NullType </td> <td>NullType</td> <td>IntegerType</td> <td>LongType</td> <td>DecimalType(38,0)</td> <td>DoubleType</td> <td>DateType</td> <td>TimestampType</td> <td>StringType</td> </tr> <tr> <td> IntegerType </td> <td>IntegerType</td> <td>IntegerType</td> <td>LongType</td> <td>DecimalType(38,0)</td> <td>DoubleType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> </tr> <tr> <td> LongType </td> <td>LongType</td> <td>LongType</td> <td>LongType</td> <td>DecimalType(38,0)</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> </tr> <tr> <td> DecimalType(38,0)* </td> <td>DecimalType(38,0)</td> <td>DecimalType(38,0)</td> <td>DecimalType(38,0)</td> <td>DecimalType(38,0)</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> </tr> <tr> <td> DoubleType </td> <td>DoubleType</td> <td>DoubleType</td> <td>StringType</td> <td>StringType</td> <td>DoubleType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> </tr> <tr> <td> DateType </td> <td>DateType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>DateType</td> <td>TimestampType</td> <td>StringType</td> </tr> <tr> <td> TimestampType </td> <td>TimestampType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>TimestampType</td> <td>TimestampType</td> <td>StringType</td> </tr> <tr> <td> StringType </td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> <td>StringType</td> </tr> </table>

Note that, for <b>DecimalType(38,0)*</b>, the table above intentionally does not cover all other combinations of scales and precisions because currently we only infer decimal type like `BigInteger`/`BigInt`. For example, 1.1 is inferred as double type.

從 Spark SQL 2.1 升級至 2.2

從 Spark SQL 2.0 升級至 2.1

從 Spark SQL 1.6 升級至 2.0

從 Spark SQL 1.5 升級至 1.6

   ./sbin/start-thriftserver.sh \
     --conf spark.sql.hive.thriftServer.singleSession=true \
     ...
   

從 Spark SQL 1.4 升級至 1.5

從 Spark SQL 1.3 升級至 1.4

DataFrame 資料讀取器/寫入器介面

根據使用者的回饋,我們建立了一個新的、更流暢的 API 來讀取資料(SQLContext.read)和寫出資料(DataFrame.write),並棄用舊的 API(例如 SQLContext.parquetFileSQLContext.jsonFile)。

請參閱 SQLContext.read ( ScalaJavaPython ) 和 DataFrame.write ( ScalaJavaPython ) 的 API 文件,以取得更多資訊。

DataFrame.groupBy 保留群組欄

根據使用者的回饋,我們已將 DataFrame.groupBy().agg() 的預設行為變更為在結果 DataFrame 中保留群組欄。若要保留 1.3 中的行為,請將 spark.sql.retainGroupColumns 設定為 false

import pyspark.sql.functions as func

# In 1.3.x, in order for the grouping column "department" to show up,
# it must be included explicitly as part of the agg function call.
df.groupBy("department").agg(df["department"], func.max("age"), func.sum("expense"))

# In 1.4+, grouping column "department" is included automatically.
df.groupBy("department").agg(func.max("age"), func.sum("expense"))

# Revert to 1.3.x behavior (not retaining grouping column) by:
sqlContext.setConf("spark.sql.retainGroupColumns", "false")
// In 1.3.x, in order for the grouping column "department" to show up,
// it must be included explicitly as part of the agg function call.
df.groupBy("department").agg($"department", max("age"), sum("expense"))

// In 1.4+, grouping column "department" is included automatically.
df.groupBy("department").agg(max("age"), sum("expense"))

// Revert to 1.3 behavior (not retaining grouping column) by:
sqlContext.setConf("spark.sql.retainGroupColumns", "false")
// In 1.3.x, in order for the grouping column "department" to show up,
// it must be included explicitly as part of the agg function call.
df.groupBy("department").agg(col("department"), max("age"), sum("expense"));

// In 1.4+, grouping column "department" is included automatically.
df.groupBy("department").agg(max("age"), sum("expense"));

// Revert to 1.3 behavior (not retaining grouping column) by:
sqlContext.setConf("spark.sql.retainGroupColumns", "false");

DataFrame.withColumn 的行為變更

在 1.4 之前,DataFrame.withColumn() 僅支援新增欄位。即使結果 DataFrame 中可能已有同名的欄位,該欄位仍會以指定的欄位名稱新增為新欄位。自 1.4 起,DataFrame.withColumn() 支援新增與所有現有欄位名稱不同的欄位,或取代同名的現有欄位。

請注意,此變更僅適用於 Scala API,不適用於 PySpark 和 SparkR。

從 Spark SQL 1.0-1.2 升級至 1.3

在 Spark 1.3 中,我們移除了 Spark SQL 的「Alpha」標籤,並作為此變更的一部分清理了可用的 API。從 Spark 1.3 起,Spark SQL 將提供與 1.X 系列中其他版本的二進位相容性。此相容性保證不包括明確標示為不穩定的 API(例如 DeveloperAPI 或 Experimental)。

SchemaRDD 改名為 DataFrame

使用者在升級到 Spark SQL 1.3 時會注意到的最大變更,就是 SchemaRDD 已改名為 DataFrame。這主要是因為 DataFrames 不再直接繼承 RDD,而是透過自己的實作提供 RDD 提供的大部分功能。DataFrames 仍可透過呼叫 .rdd 方法轉換為 RDD。

在 Scala 中,SchemaRDD 有別名 DataFrame,可提供部分使用案例的原始相容性。仍建議使用者更新其程式碼,改用 DataFrame。Java 和 Python 使用者需要更新其程式碼。

Java 和 Scala API 的統一

在 Spark 1.3 之前,有獨立的 Java 相容類別(JavaSQLContextJavaSchemaRDD),它們反映了 Scala API。在 Spark 1.3 中,Java API 和 Scala API 已統一。兩種語言的使用者都應該使用 SQLContextDataFrame。一般來說,這些類別會嘗試使用兩種語言都能使用的型別(例如 Array,而不是特定語言的集合)。在某些不存在共用型別的情況下(例如,傳遞封閉或 Map),會改用函式重載。

此外,已移除 Java 特定的型別 API。Scala 和 Java 的使用者都應該使用 org.apache.spark.sql.types 中的類別,以透過程式描述架構。

隔離隱式轉換,並移除 dsl 套件(僅限 Scala)

在 Spark 1.3 之前的許多程式碼範例都從 import sqlContext._ 開始,這會將 sqlContext 中的所有函式納入範圍。在 Spark 1.3 中,我們已將用於將 RDD 轉換為 DataFrame 的隱式轉換隔離到 SQLContext 內的物件中。使用者現在應該撰寫 import sqlContext.implicits._

此外,隱式轉換現在只會擴充由 Product(例如,案例類別或元組)組成的 RDD,並使用 toDF 方法,而不是自動套用。

在 DSL 內使用函式時(現在已替換為 DataFrame API),使用者會匯入 org.apache.spark.sql.catalyst.dsl。改為使用公開的資料框函式 API:import org.apache.spark.sql.functions._

移除 org.apache.spark.sql 中 DataType 的型別別名(僅限 Scala)

Spark 1.3 移除 DataType 的基本 sql 套件中存在的型別別名。使用者改為匯入 org.apache.spark.sql.types 中的類別

UDF 註冊移至 sqlContext.udf(Java 和 Scala)

用於註冊 UDF 的函式(用於 DataFrame DSL 或 SQL)已移至 SQLContext 中的 udf 物件。

sqlContext.udf.register("strLen", (s: String) => s.length())
sqlContext.udf().register("strLen", (String s) -> s.length(), DataTypes.IntegerType);

Python UDF 註冊保持不變。

與 Apache Hive 相容

Spark SQL 設計成與 Hive Metastore、SerDes 和 UDF 相容。目前,Hive SerDes 和 UDF 是基於內建的 Hive,而 Spark SQL 可以連接到不同版本的 Hive Metastore(從 0.12.0 到 2.3.9 和 3.0.0 到 3.1.3。另請參閱 與不同版本的 Hive Metastore 互動)。

在現有的 Hive 倉庫中部署

Spark SQL Thrift JDBC 伺服器設計成「開箱即用」,與現有的 Hive 安裝相容。您不需要修改現有的 Hive Metastore 或變更資料配置或資料表的分割。

支援的 Hive 功能

Spark SQL 支援絕大多數的 Hive 功能,例如

不支援的 Hive 功能

以下是我們尚未支援的 Hive 功能清單。這些功能大多很少在 Hive 部署中使用。

深奧的 Hive 功能

Hive 輸入/輸出格式

Hive 最佳化

一些 Hive 最佳化尚未包含在 Spark 中。其中一些(例如索引)由於 Spark SQL 的記憶體中運算模式而較不重要。其他則已排定在 Spark SQL 的未來版本中發布。

Hive UDF/UDTF/UDAF

Spark SQL 不支援 Hive UDF/UDTF/UDAF 的所有 API。以下是未支援的 API

不相容的 Hive UDF

以下是 Hive 和 Spark 產生不同結果的場景