在本機電腦上執行 U-SQL 指令碼Run U-SQL scripts on your local machine

在開發 U-SQL 指令碼時,您可以藉由在本機執行指令碼來節省時間和金錢。When you develop U-SQL scripts, you can save time and expense by running the scripts locally. Azure Data Lake Tools for Visual Studio 可支援在本機電腦上執行 U-SQL 指令碼。Azure Data Lake Tools for Visual Studio supports running U-SQL scripts on your local machine.

本機執行的基本概念Basic concepts for local runs

以下圖表顯示的是本機執行的元件,以及元件對應至雲端執行的方式。The following chart shows the components for local run and how these components map to cloud run.

元件Component 本機執行Local run 雲端執行Cloud run
儲存體Storage 本機資料根資料夾Local data root folder 預設 Azure Data Lake Store 帳戶Default Azure Data Lake Store account
計算Compute U-SQL 本機執行引擎U-SQL local run engine Azure Data Lake Analytics 服務Azure Data Lake Analytics service
執行環境Run environment 本機電腦上的工作目錄Working directory on local machine Azure Data Lake Analytics 叢集Azure Data Lake Analytics cluster

下列各節會提供關於本機執行元件的詳細資訊。The sections that follow provide more information about local run components.

本機資料根資料夾Local data root folders

本機根資料夾是本機計算帳戶的本機存放區A local data root folder is a local store for the local compute account. 在本機電腦上,本機檔案系統中的任何資料夾都可以作為本機資料根資料夾。Any folder in the local file system on your local machine can be a local data root folder. 它就和 Data Lake Analytics 帳戶的預設 Azure Data Lake Store 帳戶一樣。It's the same as the default Azure Data Lake Store account of a Data Lake Analytics account. 切換到不同的資料根資料夾,就如同切換到不同的預設存放區帳戶。Switching to a different data root folder is just like switching to a different default store account.

資料根資料夾的用途如下︰The data root folder is used as follows:

  • 儲存中繼資料。Store metadata. 範例有資料庫、資料表,資料表值函式及組件。Examples are databases, tables, table-valued functions, and assemblies.
  • 查詢在 U-SQL 指令碼中定義為相對路徑的輸入和輸出路徑。Look up the input and output paths that are defined as relative paths in U-SQL scripts. 藉由使用相對路徑,您可以更輕鬆地將 U-SQL 指令碼部署至 Azure。By using relative paths, it's easier to deploy your U-SQL scripts to Azure.

U-SQL 本機執行引擎U-SQL local run engines

U-SQL 本機執行引擎是 U-SQL 作業的本機計算帳戶A U-SQL local run engine is a local compute account for U-SQL jobs. 使用者能透過 Azure Data Lake Tools for Visual Studio 在本機執行 U-SQL 作業。Users can run U-SQL jobs locally through Azure Data Lake Tools for Visual Studio. 本機執行也支援透過 Azure Data Lake U-SQL SDK 命令列和程式設計介面來進行。Local runs are also supported through the Azure Data Lake U-SQL SDK command-line and programming interfaces. 深入了解 Azure Data Lake U-SQL SDKLearn more about the Azure Data Lake U-SQL SDK.

工作目錄Working directories

在執行 U-SQL 指令碼時,需要有工作目錄資料夾才能快取編譯結果、執行記錄,以及執行其他功能。When you run a U-SQL script, a working directory folder is needed to cache compilation results, run logs, and perform other functions. 在 Azure Data Lake Tools for Visual Studio 中,工作目錄即是 U-SQL 專案的工作目錄。In Azure Data Lake Tools for Visual Studio, the working directory is the U-SQL project’s working directory. 它位於 <U-SQL project root path>/bin/debug> 底下。It's located under <U-SQL project root path>/bin/debug>. 每當觸發新執行時,工作目錄就會清空。The working directory is cleaned every time a new run is triggered.

Microsoft Visual Studio 中的本機執行Local runs in Microsoft Visual Studio

Azure Data Lake Tools for Visual Studio 擁有內建的本機執行引擎。Azure Data Lake Tools for Visual Studio have a built-in local run engine. 工具會以本機計算帳戶的形式呈現引擎。The tools surface the engine as a local compute account. 若要在本機執行 U-SQL 指令碼,請在指令碼的編輯器邊界下拉式功能表中選取 [Local-machine] 或 [Local-project] 帳戶。To run a U-SQL script locally, select the Local-machine or Local-project account in the script’s editor margin drop-down menu. 然後,選取 [提交] 。Then select Submit.

將 U-SQL 指令碼提交至本機帳戶

使用 Local-machine 帳戶進行本機執行Local runs with a Local-machine account

Local-machine 帳戶是共用本機電腦帳戶,擁有一個作為本機存放區帳戶的本機資料根資料夾。A Local-machine account is a shared local compute account with a single local data root folder as the local store account. 資料根資料夾的位置預設為 C:\Users<使用者名稱>\AppData\Local\USQLDataRootBy default, the data root folder is located at C:\Users<username>\AppData\Local\USQLDataRoot. 您也可以透過 [工具] > [Data Lake] > [選項和設定] 來設定此位置。It's also configurable through Tools > Data Lake > Options and Settings.

設定本機資料的根資料夾

本機執行須有 U-SQL 專案。A U-SQL project is required for a local run. U-SQL 專案的工作目錄可以當做 U-SQL 本機執行工作目錄。The U-SQL project’s working directory is used for the U-SQL local run working directory. 本機執行時產生的編譯結果、執行記錄和其他作業執行相關檔案會存放在工作目錄資料夾下。Compilation results, run logs, and other job run-related files are generated and stored under the working directory folder during the local run. 每當您重新執行指令碼時,工作目錄中的所有檔案都會清空並重新產生。Every time you rerun the script, all the files in the working directory are cleaned and regenerated.

使用 Local-project 帳戶進行本機執行Local runs with a Local-project account

Local-project 帳戶是依照專案區分的本機計算帳戶,每個專案都有各自獨立的本機資料根資料夾。A Local-project account is a project-isolated local compute account for each project with an isolated local data root folder. 在 Visual Studio 的 [方案總管] 中開啟的每個作用中 U-SQL 專案,都有對應的 (Local-project: <project name>) 帳戶。Every active U-SQL project that opens in Solution Explorer in Visual Studio has a corresponding (Local-project: <project name>) account. 這些帳戶會在 Visual Studio 的 [伺服器總管] 和 U-SQL 指令碼編輯器邊界中列出。The accounts are listed in both Server Explorer in Visual Studio and the U-SQL script editor margin.

Local-project 帳戶提供了一個精簡又獨立的開發環境。The Local-project account provides a clean and isolated development environment. Local-machine 帳戶具有共用的本機資料根資料夾,可儲存中繼資料以及所有本機作業的輸入和輸出資料。A Local-machine account has a shared local data root folder that stores metadata and input and output data for all local jobs. 但是 Local-project 帳戶會在每次 U-SQL 指令碼執行時,於 U-SQL 專案的工作目錄底下建立暫存的本機資料根資料夾。But a Local-project account creates a temporary local data root folder under a U-SQL project working directory every time a U-SQL script is run. 這個暫存的資料根資料夾會在重建或重新執行發生時清空。This temporary data root folder is cleaned when a rebuild or rerun happens.

U-SQL 專案可透過專案參考和屬性來管理這個隔離的本機執行環境。A U-SQL project manages the isolated local run environment through a project reference and property. 您可以在專案和所參考的資料庫環境中設定 U-SQL 指令碼的輸入資料來源。You can configure the input data sources for U-SQL scripts in both the project and the referenced database environments.

管理 Local-project 帳戶的輸入資料來源Manage the input data source for a Local-project account

U-SQL 專案會建立本機資料根資料夾,並設定 Local-project 帳戶的資料。A U-SQL project creates a local data root folder and sets up data for a Local-project account. 每當重建和本機執行發生時,U-SQL 專案工作目錄下的暫存資料根資料夾都會清空及重新建立。A temporary data root folder is cleaned and recreated under the U-SQL project working directory every time a rebuild and local run happens. 在本機作業執行前,U-SQL 專案設定的所有資料來源都會複製到這個暫存本機資料根資料夾。All data sources that are configured by the U-SQL project are copied to this temporary local data root folder before the local job runs.

您可以設定資料來源的根資料夾。You can configure the root folder of your data sources. 以滑鼠右鍵按一下 [U-SQL 專案] > [屬性] > [測試資料來源] 。Right-click U-SQL project > Property > Test Data Source. Local-project 帳戶執行 U-SQL 指令碼時,[測試資料來源] 資料夾內的所有檔案和子資料夾都會複製到暫存本機資料根資料夾。When you run a U-SQL script on a Local-project account, all files and subfolders in the Test Data Source folder are copied to the temporary local data root folder. 子資料夾底下的檔案也包含在內。Files under subfolders are included. 本機作業執行後,您可以在專案工作目錄的暫存本機資料根資料夾下方找到輸出結果。After a local job runs, output results can also be found under the temporary local data root folder in the project working directory. 在重建及清理專案時,這個輸出內容會全部遭到刪除和清空。All this output is deleted and cleaned when the project gets rebuilt and cleaned.

設定專案的測試資料來源

管理 Local-project 帳戶的參考資料庫環境Manage a referenced database environment for a Local-project account

如果 U-SQL 查詢使用的是 U-SQL 資料庫物件,或是以該物件進行查詢,您必須先在本機將資料庫環境準備妥當,才能在本機執行該 U-SQL 指令碼。If a U-SQL query uses or queries with U-SQL database objects, you must make the database environments ready locally before you run the U-SQL script locally. 對於 Local-project 帳戶,您可以透過 U-SQL 專案參考來管理 U-SQL 資料庫相依性。For a Local-project account, U-SQL database dependencies can be managed by U-SQL project references. 您可以將 U-SQL 資料庫專案參考新增到 U-SQL 專案。You can add U-SQL database project references to your U-SQL project. 在以 Local-project 帳戶執行 U-SQL 指令碼之前,所有參考資料庫都會部署到暫存本機資料根資料夾。Before running U-SQL scripts on a Local-project account, all referenced databases are deployed to the temporary local data root folder. 而在每次執行時,暫存資料根資料夾都會清空,成為全新的隔離環境。And for each run, the temporary data root folder is cleaned as a fresh isolated environment.

請參閱此相關文章:See this related article:

Local-machineLocal-project 帳戶之間的差異The difference between Local-machine and Local-project accounts

Local-machine 帳戶會在使用者的本機電腦上模擬 Azure Data Lake Analytics 帳戶。A Local-machine account simulates an Azure Data Lake Analytics account on users’ local machines. 它的體驗與 Azure Data Lake Analytics 帳戶相同。It shares the same experience with an Azure Data Lake Analytics account. Local-project 帳戶則會提供方便使用者使用的本機開發環境。A Local-project account provides a user-friendly local development environment. 這個環境可協助使用者先在本機部署資料庫參考和輸入資料再執行指令碼。This environment helps users deploy database references and input data before they run scripts locally. Local-machine 帳戶提供共用的永久環境,供使用者透過所有專案存取。A Local-machine account provides a shared permanent environment that can be accessed through all projects. Local-project 帳戶提供每個專案各地獨立的開發環境。A Local-project account provides an isolated development environment for each project. 每次執行時,系統都會重新整理此環境。It's refreshed for each run. Local-project 帳戶能讓使用者迅速套用新變更,因而創造快速的開發體驗。A Local-project account offers a faster development experience by quickly applying new changes.

下表會顯示更多 Local-machineLocal-project 帳戶之間的差異:More differences between Local-machine and Local-project accounts are shown in the following table:

差異角度Difference angle Local-machineLocal-machine Local-projectLocal-project
本機存取Local access 所有專案均可存取。Can be accessed by all projects. 只有對應的專案能存取這個帳戶。Only the corresponding project can access this account.
本機資料根資料夾Local data root folder 永久本機資料夾。A permanent local folder. 透過 [工具] > [Data Lake] > [選項和設定] 設定。Configured through Tools > Data Lake > Options and Settings. 每次本機執行時,會在 U-SQL 專案工作資料夾下建立暫存資料夾。A temporary folder created for each local run under the U-SQL project working directory. 資料夾會在重建或重新執行發生時清空。The folder gets cleaned when a rebuild or rerun happens.
U-SQL 指令碼的輸入資料Input data for a U-SQL script 永久本機資料根資料夾下方的相對路徑。The relative path under the permanent local data root folder. 透過 [U-SQL 專案屬性] > [測試資料來源] 設定。Set through U-SQL project property > Test Data Source. 所有檔案和子資料夾都會在本機執行前複製到暫存資料根資料夾。All files and subfolders are copied to the temporary data root folder before a local run.
U-SQL 指令碼的輸出資料Output data for a U-SQL script 永久本機資料根資料夾下方的相對路徑。Relative path under the permanent local data root folder. 輸出到暫存資料根資料夾。Output to the temporary data root folder. 結果會在重建或重新執行發生時清空。The results are cleaned when a rebuild or rerun happens.
參考資料庫部署Referenced database deployment Local-machine 帳戶執行時,不會自動部署參考資料庫。Referenced databases aren't deployed automatically when running against a Local-machine account. 提交到 Azure Data Lake Analytics 帳戶時也是如此。It's the same for submitting to an Azure Data Lake Analytics account. 參考資料庫會在本機執行前自動部署到 Local-project 帳戶。Referenced databases are deployed to the Local-project account automatically before a local run. 所有資料庫環境都會在重建或重新執行發生時清空及重新部署。All database environments are cleaned and redeployed when a rebuild or rerun happens.

使用 U-SQL SDK 進行本機執行A local run with the U-SQL SDK

您可以在 Visual Studio 中本機執行 U-SQL 指令碼,也可以使用 Azure Data Lake U-SQL SDK,利用命令列和程式設計介面在本機執行 U-SQL 指令碼。You can run U-SQL scripts locally in Visual Studio and also use the Azure Data Lake U-SQL SDK to run U-SQL scripts locally with command-line and programming interfaces. 透過這些介面,您可以將 U-SQL 本機執行和測試自動化。Through these interfaces, you can automate U-SQL local runs and tests.

深入了解 Azure Data Lake U-SQL SDKLearn more about the Azure Data Lake U-SQL SDK.

後續步驟Next steps