共用方式為


查詢 Azure Synapse Analytics 中的資料

您可以使用 Azure Synapse 連接器,從 Azure Databricks 存取 Azure Synapse 的 Azure Synapse,該連接器會使用 COPY Azure Synapse 中的 語句,在 Azure Databricks 叢集與 Azure Synapse 實例之間有效率地傳輸大量數據,並使用 Azure Data Lake Storage 儲存器帳戶進行暫存。

這很重要

舊版查詢聯邦文件已停用,可能不會再更新。 此內容中提及的組態不會由 Databricks 正式認可或測試。 如果 Lakehouse Federation 支援源資料庫,Databricks 建議改用。

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

這很重要

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

備註

COPY 僅適用於 Azure Data Lake Storage 實例。 如果您要尋找使用 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 Catalog中定義的外部位置作為tempDir位置使用。

Databricks 建議您使用最安全的驗證流程。 此範例中所述的驗證流程具有其他流程中不存在的風險。 只有在其他更安全的流程,例如受控識別無法使用時,才應該使用此流程。

程式語言 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 寫入所需的權限:

權限 (插入現有資料表) 權限 (插入新資料表)
管理資料庫批量操作
INSERT
管理資料庫批量操作
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 防火牆規則

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

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

您可以選擇性地針對 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 無預設值 Azure Synapse 使用者名稱。 必須與 password 選項搭配使用。 只有在使用者和密碼未傳入網址的情況下,才能使用。 傳遞這兩個參數會導致錯誤。
password 無預設值 Azure Synapse 密碼。 必須與 user 選項搭配使用。 只有在使用者和密碼未傳入網址的情況下,才能使用。 傳遞這兩個參數會導致錯誤。
url 是的 無預設值 sqlserver 設定為子通訊協定的 JDBC 網址。 建議使用 Azure 入口網站所提供的連接字串。 強烈建議設定 encrypt=true,因為它會啟用 JDBC 連線的 SSL 加密。 如果 userpassword 分別設定,您就不需要將其包含在網址中。
jdbcDriver 由 JDBC URL 的子協定決定 要使用的 JDBC 驅動程式類別名稱。 此類別必須位於 classpath 上。 在大部分情況下,通常不需要指定這個選項,因為適當的驅動程式類別名稱應該由 JDBC URL 的子協議自動決定。
先前支援的 jdbc_driver 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式命名法」名稱。
tempDir 是的 無預設值 一個 abfss URI。 建議您針對 Azure Synapse 使用專用的 Blob 儲存體容器。
先前支援的 tempdir 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式命名法」名稱。
您無法使用在 Unity 目錄中定義的外部位置做為 tempDir 位置。
tempCompression SNAPPY Spark 和 Azure Synapse 所用來壓縮和解壓縮臨時檔案的演算法。 目前支援的值為: UNCOMPRESSEDSNAPPYGZIP
forwardSparkAzureStorageCredentials 假的 如果 true,程式庫會自動探索 Spark 用來連線到 Blob 儲存體容器的儲存體帳戶存取密金認證,並透過 JDBC 將這些認證轉送至 Azure Synapse。 這些認證會作為 JDBC 查詢的一部分傳送。 因此,強烈建議您在使用此選項時啟用 JDBC 連線的 SSL 加密。
當設定儲存體驗證時,您必須將 useAzureMSIforwardSparkAzureStorageCredentials 其中之一設為 true。 或者,您可以將 enableServicePrincipalAuth 設定為 true,並使用服務主體進行 JDBC 和儲存體驗證。 forwardSparkAzureStorageCredentials 選項不支援使用受控服務識別或服務主體對儲存體進行驗證。 僅支援儲存體帳戶存取金鑰。
先前支援的 forward_spark_azure_storage_credentials 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式命名法」名稱。
useAzureMSI 假的 如果 true,則庫將為它所建立的資料庫範圍憑證指定 IDENTITY = 'Managed Service Identity' 而不指定 SECRET
當設定儲存體驗證時,您必須將 useAzureMSIforwardSparkAzureStorageCredentials 其中之一設為 true。 或者,您可以將 enableServicePrincipalAuth 設定為 true,並使用服務主體進行 JDBC 和儲存體驗證。
enableServicePrincipalAuth 假的 如果 true,程式庫會使用所提供的服務主體認證,透過 JDBC 連線到 Azure 儲存體帳戶和 Azure Synapse Analytics。
如果 forward_spark_azure_storage_credentialsuseAzureMSI 設定為 true,該選項會優先於儲存體驗證中的服務主體。
tableOptions CLUSTERED COLUMNSTORE INDEXDISTRIBUTION = ROUND_ROBIN 在建立透過 設定的 Azure Synapse 資料表時,用來指定dbTable的字串。 此字串會以字面方式傳遞至針對 Azure Synapse 發出的 SQL 陳述式的 WITH 子句。
先前支援的 table_options 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式命名法」名稱。
preActions 無預設值 (空字串) 在資料寫入 Azure Synapse 執行個體之前,於 Azure Synapse 中執行的一個以分隔符隔開的 SQL 命令清單。 這些 SQL 命令必須是 Azure Synapse 接受的有效命令。
如果上述任何命令失敗,則會將其視為錯誤,而且不會執行寫入作業。
postActions 無預設值 (空字串) 在連接器成功將資料寫入 Azure Synapse 執行個體後,將執行一個由 ; 分隔的 SQL 命令清單。 這些 SQL 命令必須是 Azure Synapse 接受的有效命令。
如果上述任何命令失敗,則會將其視為錯誤,而且在成功將數據寫入 Azure Synapse 實例之後,您會收到例外狀況。
maxStrLength 256 Spark 中的 StringType 會對應至 Azure Synapse 中的 NVARCHAR(maxStrLength) 類型。 您可以使用 maxStrLength,為 Azure Synapse 中名稱為 NVARCHAR(maxStrLength) 數據表中的所有 dbTable 類型數據行設定字串長度。
先前支援的 maxstrlength 變體已被取代,在未來的版本中會被忽略。 請改用「駝峰式命名法」名稱。
applicationName Databricks-User-Query 每個查詢的連線標籤。 如果未指定或值是空字串,則標記的預設值會新增 JDBC 網址。 預設值可防止 Azure DB 監視工具針對查詢引發虛假的 SQL 插入式警示。
maxbinlength 無預設值 控制 BinaryType 欄位的欄長。 此參數會轉譯為 VARBINARY(maxbinlength)
identityInsert 假的 設定為 true 會啟用 IDENTITY_INSERT 模式,這會在 Azure Synapse 資料表的識別資料行中插入 DataFrame 提供的值。
請參閱明確地將值插入 IDENTITY 欄位
externalDataSource 無預設值 預先佈建的外部資料來源,可從 Azure Synapse 讀取資料。 外部資料來源只能與 PolyBase 搭配使用,並移除 CONTROL 權限需求,因為連接器不需要建立範圍認證和外部資料來源來載入資料。
例如,關於使用外部資料來源時的使用範例和必需的權限清單,請參閱具有外部資料來源選項的 PolyBase 所需 Azure Synapse 權限
maxErrors 0 在載入作業取消之前,讀取和寫入期間被拒絕的資料列數量上限。 將忽略被拒的資料列。 例如,如果十分之二的記錄有錯誤,則只會處理八筆記錄。
請參閱 CREATE EXTERNAL 文件中的 REJECT_VALUE 說明,以及 COPY文件中的 MAXERRORS 說明。
inferTimestampNTZType 假的 如果 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>/。 您可以設定定期作業(使用 Lakeflow 作業功能或其他方式)以遞歸方式刪除比指定臨界值還舊的任何子目錄(例如 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_%'