共用方式為


查詢 Azure Synapse Analytics 中的資料

您可以使用 Azure Synapse 連接器,從 Azure Databricks 存取 Azure Synapse,Azure Synapse 連接器會使用 Azure Synapse 中的 COPY 陳述式,在 Azure Databricks 叢集與使用 Azure Data Lake Storage Gen2 儲存體帳戶進行暫存的 Azure Synapse 執行個體之間有效率地傳輸大量資料。

重要

本文所述的設定為實驗性質。 實驗性功能是以現況提供,且無法透過客戶技術支援來支援 Databricks。 若要取得完整的查詢同盟支援,您應該改用 Lakehouse 同盟,這可讓您的 Azure Databricks 使用者利用 Unity 目錄語法和資料控管工具

Azure Synapse Analytics 是雲端式企業資料倉儲,使用大量平行處理 (MPP) 以迅速對數以 PB 計的資料執行複雜查詢。

重要

此連接器僅適用於 Synapse 專用集區執行個體,且與其他 Synapse 元件不相容。

注意

COPY 目前僅在 Azure Data Lake Storage Gen2 上可用。 如果您要尋找使用 Polybase 的詳細資訊,請參閱將 Azure Databricks 和 Azure Synapse 與 PolyBase 連線(舊版)

Synapse 的範例語法

您可以在 Scala、Python、SQL 和 R 中查詢 Synapse。下列程式碼範例使用儲存體帳戶密鑰,並將儲存體認證從 Azure Databricks 轉寄至 Synapse。

注意

使用 Azure 入口網站所提供的連接字串,它可透過 JDBC 連線為 Spark 驅動程式與 Azure Synapse 執行個體之間傳送的所有資料啟用安全通訊端層 (SSL) 加密。 若要確認已啟用 SSL 加密,可在連接字串中搜尋 encrypt=true

重要

不支援將在 Unity 目錄中定義的外部位置作為 tempDir 位置。

Scala


// Set up the storage account access key in the notebook session conf.
spark.conf.set(
  "fs.azure.account.key.<your-storage-account-name>.dfs.core.windows.net",
  "<your-storage-account-access-key>")

// Get some data from an Azure Synapse table. The following example applies to Databricks Runtime 11.3 LTS and above.
val df: DataFrame = spark.read
  .format("sqldw")
  .option("host", "hostname")
  .option("port", "port") /* Optional - will use default port 1433 if not specified. */
  .option("user", "username")
  .option("password", "password")
  .option("database", "database-name")
  .option("dbtable", "schema-name.table-name") /* If schemaName not provided, default to "dbo". */
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")
  .option("forwardSparkAzureStorageCredentials", "true")
  .load()

// Get some data from an Azure Synapse table. The following example applies to Databricks Runtime 10.4 LTS and below.
val df: DataFrame = spark.read
  .format("com.databricks.spark.sqldw")
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>")
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")
  .option("forwardSparkAzureStorageCredentials", "true")
  .option("dbTable", "<your-table-name>")
  .load()

// Load data from an Azure Synapse query.
val df: DataFrame = spark.read
  .format("com.databricks.spark.sqldw")
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>")
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")
  .option("forwardSparkAzureStorageCredentials", "true")
  .option("query", "select x, count(*) as cnt from table group by x")
  .load()

// Apply some transformations to the data, then use the
// Data Source API to write the data back to another table in Azure Synapse.

df.write
  .format("com.databricks.spark.sqldw")
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>")
  .option("forwardSparkAzureStorageCredentials", "true")
  .option("dbTable", "<your-table-name>")
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")
  .save()

Python


# Set up the storage account access key in the notebook session conf.
spark.conf.set(
  "fs.azure.account.key.<your-storage-account-name>.dfs.core.windows.net",
  "<your-storage-account-access-key>")

# Get some data from an Azure Synapse table. The following example applies to Databricks Runtime 11.3 LTS and above.
df = spark.read
  .format("sqldw")
  .option("host", "hostname")
  .option("port", "port") # Optional - will use default port 1433 if not specified.
  .option("user", "username")
  .option("password", "password")
  .option("database", "database-name")
  .option("dbtable", "schema-name.table-name") # If schemaName not provided, default to "dbo".
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")
  .option("forwardSparkAzureStorageCredentials", "true")
  .load()

# Get some data from an Azure Synapse table. The following example applies to Databricks Runtime 10.4 LTS and below.
df = spark.read \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("dbTable", "<your-table-name>") \
  .load()

# Load data from an Azure Synapse query.
df = spark.read \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("query", "select x, count(*) as cnt from table group by x") \
  .load()

# Apply some transformations to the data, then use the
# Data Source API to write the data back to another table in Azure Synapse.

df.write \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("dbTable", "<your-table-name>") \
  .option("tempDir", "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>") \
  .save()

SQL


-- Set up the storage account access key in the notebook session conf.
SET fs.azure.account.key.<your-storage-account-name>.dfs.core.windows.net=<your-storage-account-access-key>;

-- Read data using SQL. The following example applies to Databricks Runtime 11.3 LTS and above.
CREATE TABLE example_table_in_spark_read
USING sqldw
OPTIONS (
  host '<hostname>',
  port '<port>' /* Optional - will use default port 1433 if not specified. */
  user '<username>',
  password '<password>',
  database '<database-name>'
  dbtable '<schema-name>.<table-name>', /* If schemaName not provided, default to "dbo". */
  forwardSparkAzureStorageCredentials 'true',
  tempDir 'abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>'
);

-- Read data using SQL. The following example applies to Databricks Runtime 10.4 LTS and below.
CREATE TABLE example_table_in_spark_read
USING com.databricks.spark.sqldw
OPTIONS (
  url 'jdbc:sqlserver://<the-rest-of-the-connection-string>',
  forwardSparkAzureStorageCredentials 'true',
  dbtable '<your-table-name>',
  tempDir 'abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>'
);

-- Write data using SQL.
-- Create a new table, throwing an error if a table with the same name already exists:

CREATE TABLE example_table_in_spark_write
USING com.databricks.spark.sqldw
OPTIONS (
  url 'jdbc:sqlserver://<the-rest-of-the-connection-string>',
  forwardSparkAzureStorageCredentials 'true',
  dbTable '<your-table-name>',
  tempDir 'abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>'
)
AS SELECT * FROM table_to_save_in_spark;

R

# Load SparkR
library(SparkR)

# Set up the storage account access key in the notebook session conf.
conf <- sparkR.callJMethod(sparkR.session(), "conf")
sparkR.callJMethod(conf, "set", "fs.azure.account.key.<your-storage-account-name>.dfs.core.windows.net", "<your-storage-account-access-key>")

# Get some data from an Azure Synapse table.
df <- read.df(
   source = "com.databricks.spark.sqldw",
   url = "jdbc:sqlserver://<the-rest-of-the-connection-string>",
   forward_spark_azure_storage_credentials = "true",
   dbTable = "<your-table-name>",
   tempDir = "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")

# Load data from an Azure Synapse query.
df <- read.df(
   source = "com.databricks.spark.sqldw",
   url = "jdbc:sqlserver://<the-rest-of-the-connection-string>",
   forward_spark_azure_storage_credentials = "true",
   query = "select x, count(*) as cnt from table group by x",
   tempDir = "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")

# Apply some transformations to the data, then use the
# Data Source API to write the data back to another table in Azure Synapse.

write.df(
  df,
  source = "com.databricks.spark.sqldw",
  url = "jdbc:sqlserver://<the-rest-of-the-connection-string>",
  forward_spark_azure_storage_credentials = "true",
  dbTable = "<your-table-name>",
  tempDir = "abfss://<your-container-name>@<your-storage-account-name>.dfs.core.windows.net/<your-directory-name>")

Azure Databricks 與 Synapse 之間的驗證如何運作?

Azure Synapse 連接器使用三種類型的網路連線:

  • Spark 驅動程式到 Azure Synapse
  • Spark 叢集至 Azure 儲存體帳戶
  • Azure Synapse 到 Azure 儲存體帳戶

設定 Azure 儲存體的存取

Azure Databricks 和 Synapse 都需要 Azure 儲存體帳戶的特殊權限存取,才能用於暫存資料儲存體。

Azure Synapse 不支援使用 SAS 進行儲存體帳戶存取。 您可以執行下列其中一項來設定這兩項服務的存取:

需要 Azure Synapse 權限

由於 Azure Synapse 連接器在背景中使用 COPY,它會要求 JDBC 連線使用者有權在連線的 Azure Synapse 執行個體中執行下列命令:

如果目的地資料表不存在於 Azure Synapse 中,除了上述命令之外,還需要執行下列命令的權限:

下表摘要說明使用 COPY 寫入所需的權限:

權限 (插入現有資料表) 權限 (插入新資料表)
ADMINISTER DATABASE BULK OPERATIONS

INSERT
ADMINISTER DATABASE BULK OPERATIONS

INSERT

CREATE TABLE

ALTER ON SCHEMA :: dbo

網路設定

如果您在 Azure Synapse 上設定防火牆,則必須設定網路設定,以允許 Azure Databricks 連線到 Azure Synapse。 首先,在 Azure 虛擬網路中部署 Azure Databricks (VNet 插入)之後,請確定您的 Azure Databricks 工作區已部署在您自己的虛擬網路中。 接著,您可以在 Azure Synpase 上設定 IP 防火牆規則,以允許從子網路連線到 Synapse 帳戶。 請參閱 Azure Synapse Analytics IP 防火牆規則

使用服務主體設定從 Azure Databricks 連線到 Synapse 與 OAuth 2.0 的連線

您可以使用具有基礎儲存體帳戶存取的服務主體向 Azure Synapse Analytics 進行驗證。 如需有關使用服務主體憑證來存取 Azure 儲存體帳戶的詳細資訊,請參閱 連接到 Azure Data Lake Storage Gen2 和 Blob Storage。 您必須在連線設定 Azure Databricks Synapse 連接器選項參考中將 enableServicePrincipalAuth 選項設定為 true,讓連接器能夠向服務主體進行驗證。

您可以選擇性地針對 Azure Synapse Analytics 連線使用不同的服務主體。 下列範例會設定儲存體帳戶的服務主體認證,以及 Synapse 的選用服務主體認證:

ini

; Defining the Service Principal credentials for the Azure storage account
fs.azure.account.auth.type OAuth
fs.azure.account.oauth.provider.type org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider
fs.azure.account.oauth2.client.id <application-id>
fs.azure.account.oauth2.client.secret <service-credential>
fs.azure.account.oauth2.client.endpoint https://login.microsoftonline.com/<directory-id>/oauth2/token

; Defining a separate set of service principal credentials for Azure Synapse Analytics (If not defined, the connector will use the Azure storage account credentials)
spark.databricks.sqldw.jdbc.service.principal.client.id <application-id>
spark.databricks.sqldw.jdbc.service.principal.client.secret <service-credential>

Scala

// Defining the Service Principal credentials for the Azure storage account
spark.conf.set("fs.azure.account.auth.type", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type",  "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id", "<application-id>")
spark.conf.set("fs.azure.account.oauth2.client.secret", "<service-credential>")
spark.conf.set("fs.azure.account.oauth2.client.endpoint", "https://login.microsoftonline.com/<directory-id>/oauth2/token")

// Defining a separate set of service principal credentials for Azure Synapse Analytics (If not defined, the connector will use the Azure storage account credentials)
spark.conf.set("spark.databricks.sqldw.jdbc.service.principal.client.id", "<application-id>")
spark.conf.set("spark.databricks.sqldw.jdbc.service.principal.client.secret", "<service-credential>")

Python

# Defining the service principal credentials for the Azure storage account
spark.conf.set("fs.azure.account.auth.type", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type",  "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id", "<application-id>")
spark.conf.set("fs.azure.account.oauth2.client.secret", "<service-credential>")
spark.conf.set("fs.azure.account.oauth2.client.endpoint", "https://login.microsoftonline.com/<directory-id>/oauth2/token")

# Defining a separate set of service principal credentials for Azure Synapse Analytics (If not defined, the connector will use the Azure storage account credentials)
spark.conf.set("spark.databricks.sqldw.jdbc.service.principal.client.id", "<application-id>")
spark.conf.set("spark.databricks.sqldw.jdbc.service.principal.client.secret", "<service-credential>")

R

# Load SparkR
library(SparkR)
conf <- sparkR.callJMethod(sparkR.session(), "conf")

# Defining the service principal credentials for the Azure storage account
sparkR.callJMethod(conf, "set", "fs.azure.account.auth.type", "OAuth")
sparkR.callJMethod(conf, "set", "fs.azure.account.oauth.provider.type",  "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
sparkR.callJMethod(conf, "set", "fs.azure.account.oauth2.client.id", "<application-id>")
sparkR.callJMethod(conf, "set", "fs.azure.account.oauth2.client.secret", "<service-credential>")
sparkR.callJMethod(conf, "set", "fs.azure.account.oauth2.client.endpoint", "https://login.microsoftonline.com/<directory-id>/oauth2/token")

# Defining a separate set of service principal credentials for Azure Synapse Analytics (If not defined, the connector will use the Azure storage account credentials)
sparkR.callJMethod(conf, "set", "spark.databricks.sqldw.jdbc.service.principal.client.id", "<application-id>")
sparkR.callJMethod(conf, "set", "spark.databricks.sqldw.jdbc.service.principal.client.secret", "<service-credential>")

批次寫入支援的儲存模式

Azure Synapse 連接器支援 ErrorIfExistsIgnoreAppendOverwrite 儲存模式,預設模式為 ErrorIfExists。 如需 Apache Spark 中支援之儲存模式的詳細資訊,請參閱儲存模式的 Spark SQL 文件

Azure Databricks Synapse 連接器選項參考

Spark SQL 中提供的 OPTIONS 支援下列設定:

參數 必要 預設 備註
dbTable 是,除非已指定 query 無預設值 在 Azure Synapse 中建立或讀取的資料表。 將資料儲存回 Azure Synapse 時,需要此參數。

您也可以使用 {SCHEMA NAME}.{TABLE NAME} 來存取指定結構描述中的資料表。 如果未提供結構描述名稱,則會使用與 JDBC 使用者相關聯的預設結構描述。

先前支援的 dbtable 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。
query 是,除非已指定 dbTable 無預設值 在 Azure Synapse 中讀取的查詢。

對於查詢中參考的資料表,您也可以使用 {SCHEMA NAME}.{TABLE NAME} 來存取指定結構描述中的資料表。 如果未提供結構描述名稱,則會使用與 JDBC 使用者相關聯的預設結構描述。
user No 無預設值 Azure Synapse 使用者名稱。 必須與 password 選項搭配使用。 只有在使用者和密碼未傳入網址時,才能使用。 傳遞這兩者會導致錯誤。
password No 無預設值 Azure Synapse 密碼。 必須與 user 選項搭配使用。 只有在使用者和密碼未傳入網址時,才能使用。 傳遞這兩者會導致錯誤。
url Yes 無預設值 sqlserver 設定為子通訊協定的 JDBC 網址。 建議使用 Azure 入口網站所提供的連接字串。 設定
強烈建議使用 encrypt=true,因為它會啟用 JDBC 連線的 SSL 加密。 如果 userpassword 分別設定,您就不需要將其包含在網址中。
jdbcDriver No 由 JDBC 網址的子通訊協定決定 要使用的 JDBC 驅動程式類別名稱。 此類別必須位於 classpath 上。 在大部分情況下,指定這個選項不應該為必要條件,因為適當的驅動程式類別名稱應該由 JDBC 網址的子通訊協定自動判定。

先前支援的 jdbc_driver 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。
tempDir Yes 無預設值 abfss URI。 建議您針對 Azure Synapse 使用專用的 Blob 儲存體容器。

先前支援的 tempdir 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。

您無法使用在 Unity 目錄中定義的外部位置做為 tempDir 位置。
tempCompression No SNAPPY Spark 和 Azure Synapse 要用來編碼/解碼暫存的壓縮演算法。 目前支援的值為: UNCOMPRESSEDSNAPPYGZIP
forwardSparkAzureStorageCredentials No false 如果 true,程式庫會自動探索 Spark 用來連線到 Blob 儲存體容器的儲存體帳戶存取密金認證,並透過 JDBC 將這些認證轉送至 Azure Synapse。 這些認證會作為 JDBC 查詢的一部分傳送。 因此,強烈建議您在使用此選項時啟用 JDBC 連線的 SSL 加密。

設定儲存體驗證時,您必須將 useAzureMSIforwardSparkAzureStorageCredentials 其中之一設定為 true。 或者,您可以將 enableServicePrincipalAuth 設定為 true,並使用服務主體進行 JDBC 和儲存體驗證。 forwardSparkAzureStorageCredentials 選項不支援使用受控服務識別或服務主體對儲存體進行驗證。 僅支援儲存體帳戶存取金鑰。

先前支援的 forward_spark_azure_storage_credentials 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。
useAzureMSI No false 如果 true,媒體櫃將會針對它所建立的資料庫範圍認證指定為 IDENTITY = 'Managed Service Identity' 而非 SECRET

設定儲存體驗證時,您必須將 useAzureMSIforwardSparkAzureStorageCredentials 其中之一設定為 true。 或者,您可以將 enableServicePrincipalAuth 設定為 true,並使用服務主體進行 JDBC 和儲存體驗證。
enableServicePrincipalAuth No false 如果 true,程式庫會使用所提供的服務主體認證,透過 JDBC 連線到 Azure 儲存體帳戶和 Azure Synapse Analytics。

如果 forward_spark_azure_storage_credentialsuseAzureMSI 設定為 true,該選項會優先於儲存體驗證中的服務主體。
tableOptions No CLUSTERED COLUMNSTORE INDEXDISTRIBUTION = ROUND_ROBIN 建立透過 dbTable 設定的 Azure Synapse 資料表時,用來指定資料表選項的字串。 此字串會以字面方式傳遞至 WITH 針對 Azure Synapse 發出的 CREATE TABLE SQL 陳述式子句。

先前支援的 table_options 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。
preActions No 無預設值 (空字串) ; 將資料寫入 Azure Synapse 執行個體之前,要於 Azure Synapse 中執行之 SQL 命令的分隔清單。 這些 SQL 命令必須是 Azure Synapse 接受的有效命令。

如果上述任何命令失敗,則會將其視為錯誤,而且不會執行寫入作業。
postActions No 無預設值 (空字串) 連接器成功將資料寫入 Azure Synapse 執行個體之後,要在 Azure Synapse 中執行的 SQL 命令 ; 分隔清單。 這些 SQL 命令必須是 Azure Synapse 接受的有效命令。

如果上述任何命令失敗,則會將其視為錯誤,並且在成功將資料寫入 Azure Synapse 執行個體之後,您會收到例外狀況。
maxStrLength No 256 Spark 中的 StringType 會對應至 Azure Synapse 中的 NVARCHAR(maxStrLength) 類型。 您可以使用 maxStrLength 來設定資料表中具有名稱之所有 NVARCHAR(maxStrLength) 類型資料行的字串長度
Azure Synapse 中的 dbTable

先前支援的 maxstrlength 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式大小寫」名稱。
applicationName No Databricks-User-Query 每個查詢的連接標記。 如果未指定或值是空字串,則標記的預設值會新增 JDBC 網址。 預設值可防止 Azure DB 監視工具針對查詢引發虛假的 SQL 插入式警示。
maxbinlength No 無預設值 控制 BinaryType 資料行的資料行長度。 此參數會轉譯為 VARBINARY(maxbinlength)
identityInsert No false 設定為 true 會啟用 IDENTITY_INSERT 模式,這會在 Azure Synapse 資料表的識別資料行中插入 DataFrame 提供的值。

請參閱明確地將值插入 IDENTITY 資料行
externalDataSource No 無預設值 預先佈建的外部資料來源,可從 Azure Synapse 讀取資料。 外部資料來源只能與 PolyBase 搭配使用,並移除 CONTROL 權限需求,因為連接器不需要建立範圍認證和外部資料來源來載入資料。

如需使用外部資料來源時所需的使用方式和權限清單,請參閱具有外部資料來源選項的 PolyBase 必要 Azure Synapse 權限
maxErrors No 0 在載入作業取消之前,可以在讀取和寫入期間拒絕的資料列數目上限。 將會忽略拒絕的資料列。 例如,如果十分之二的記錄有錯誤,則只會處理八筆記錄。

請參閱 CREATE EXTERNAL TABLE 中的 REJECT_VALUE 文件COPY 中的 MAXERRORS 文件
inferTimestampNTZType No false 如果 true,則 Azure Synapse TIMESTAMP 類型的值會在讀取期間解譯為 TimestampNTZType (不含時區的時間戳記)。 否則,不論基礎 Azure Synapse 資料表中的類型為何,所有時間戳記都會解譯為 TimestampType

注意

  • tableOptionspreActionspostActionsmaxStrLength 只有在將資料從 Azure Databricks 寫入 Azure Synapse 中的新資料表時才會相關。
  • 儘管所有資料來源選項名稱都不區分大小寫,但為了清楚起見,我們建議您使用「駝峰式大小寫」來指定。

查詢下推至 Azure Synapse

Azure Synapse 連接器會實作一組最佳化規則,將下列運算子向下推送至 Azure Synapse:

  • Filter
  • Project
  • Limit

ProjectFilter 運算子支援下列運算式:

  • 大多數布林邏輯運算子
  • 比較
  • 基本算術運算子
  • 數值和字串轉換

針對 Limit 運算子,只有在沒有指定順序時,才支援下推。 例如:

SELECT TOP(10) * FROM table,但不是 SELECT TOP(10) * FROM table ORDER BY col

注意

Azure Synapse 連接器不會下推在字串、日期或時間戳記上運作的運算式。

預設會啟用使用 Azure Synapse 連接器建置的查詢下推。 您可以將 spark.databricks.sqldw.pushdown 設定為 false,以將其停用。

暫存資料管理

Azure Synapse 連接器 不會 刪除它在 Azure 儲存體容器中建立的暫存檔案。 Databricks 建議您定期刪除使用者提供之 tempDir 位置下的臨時檔案。

為了方便資料清理,Azure Synapse 連接器不會直接儲存在 tempDir 下的資料檔案,而是會建立表單的子目錄:<tempDir>/<yyyy-MM-dd>/<HH-mm-ss-SSS>/<randomUUID>/。 您可以設定定期作業 (使用 Azure Databricks 作業功能或其他方式),以遞迴方式刪除比指定閾值還舊的任何子目錄 (例如 2 天),並需假設沒有 Spark 作業執行的時間超過該閾值。

更簡單的替代方案是定期卸除整個容器,並建立具有相同名稱的新容器。 這需要您將專用的容器用於 Azure Synapse 連接器所產生的暫存資料,而且您可以找到一個時間範圍,以確保沒有涉及連接器的查詢正在執行。

暫存物件管理

Azure Synapse 連接器會將 Azure Databricks 叢集與 Azure Synapse 執行個體之間的資料傳輸自動化。 為了從 Azure Synapse 資料表或查詢讀取資料,或將資料寫入 Azure Synapse 資料表,Azure Synapse 連接器會在幕後建立臨時物件,包括 DATABASE SCOPED CREDENTIALEXTERNAL DATA SOURCEEXTERNAL FILE FORMATEXTERNAL TABLE。 這些物件只會在對應 Spark 工作的期間內持續存在,而且會自動卸除。

當叢集使用 Azure Synapse 連接器執行查詢時,如果 Spark 驅動程式程序當機或已強制重新啟動,或叢集已強制終止或重新啟動,則可能不會卸除暫存物件。 為了輔助識別和手動刪除這些物件,Azure Synapse 連接器會在 Azure Synapse 執行個體中建立的所有中繼暫存物件名稱前面加上下列格式標籤:tmp_databricks_<yyyy_MM_dd_HH_mm_ss_SSS>_<randomUUID>_<internalObject>

建議您定期使用查詢來尋找缺失的物件,如下所示:

  • SELECT * FROM sys.database_scoped_credentials WHERE name LIKE 'tmp_databricks_%'
  • SELECT * FROM sys.external_data_sources WHERE name LIKE 'tmp_databricks_%'
  • SELECT * FROM sys.external_file_formats WHERE name LIKE 'tmp_databricks_%'
  • SELECT * FROM sys.external_tables WHERE name LIKE 'tmp_databricks_%'