瞭解如何使用 Visual Studio Code (VS Code) 透過 U-SQL 撰寫 Python、R 和 C# 程式代碼,並將作業提交至 Azure Data Lake 服務。 如需 Azure Data Lake Tools for VS Code 的詳細資訊,請參閱 使用 Azure Data Lake Tools for Visual Studio Code。
在撰寫自訂程式碼之前,您需要在 VS Code 中開啟資料夾或工作區。
Python 和 R 的必要條件
為您的 ADL 帳戶註冊 Python 和 R 擴充套件集。
在入口網站中開啟您的帳戶。
- 選取 概觀。
- 選取 範例文稿。
選擇更多。
選擇[安裝 U-SQL 延伸模組。
確認訊息會顯示在安裝U-SQL擴充功能之後。
備註
如需 Python 和 R 語言服務的最佳體驗,請安裝 VSCode Python 和 R 延伸模組。
開發 Python 檔案
請在工作區中選擇 [新檔案]。
在 U-SQL 中撰寫您的程式代碼。 以下是程式代碼範例。
REFERENCE ASSEMBLY [ExtPython]; @t = SELECT * FROM (VALUES ("D1","T1","A1","@foo Hello World @bar"), ("D2","T2","A2","@baz Hello World @beer") ) AS D( date, time, author, tweet ); @m = REDUCE @t ON date PRODUCE date string, mentions string USING new Extension.Python.Reducer("pythonSample.usql.py", pyVersion : "3.5.1"); OUTPUT @m TO "/tweetmentions.csv" USING Outputters.Csv();
以滑鼠右鍵按下腳本檔案,然後選取 [ADL:生成 Python 程式碼後置檔案]。
xxx.usql.py 檔案會在工作資料夾中產生。 在 Python 檔案中撰寫程式代碼。 以下是程式代碼範例。
def get_mentions(tweet): return ';'.join( ( w[1:] for w in tweet.split() if w[0]=='@' ) ) def usqlml_main(df): del df['time'] del df['author'] df['mentions'] = df.tweet.apply(get_mentions) del df['tweet'] return df
在 USQL 檔案中按下滑鼠右鍵,您可以選取 [編譯腳本] 或 [提交作業] 至執行中作業。
開發 R 檔案
在工作區中選擇 [新增檔案]。
在 U-SQL 檔案中撰寫程式代碼。 以下是程式代碼範例。
DEPLOY RESOURCE @"/usqlext/samples/R/my_model_LM_Iris.rda"; DECLARE @IrisData string = @"/usqlext/samples/R/iris.csv"; DECLARE @OutputFilePredictions string = @"/my/R/Output/LMPredictionsIris.txt"; DECLARE @PartitionCount int = 10; @InputData = EXTRACT SepalLength double, SepalWidth double, PetalLength double, PetalWidth double, Species string FROM @IrisData USING Extractors.Csv(); @ExtendedData = SELECT Extension.R.RandomNumberGenerator.GetRandomNumber(@PartitionCount) AS Par, SepalLength, SepalWidth, PetalLength, PetalWidth FROM @InputData; // Predict Species @RScriptOutput = REDUCE @ExtendedData ON Par PRODUCE Par, fit double, lwr double, upr double READONLY Par USING new Extension.R.Reducer(scriptFile : "RClusterRun.usql.R", rReturnType : "dataframe", stringsAsFactors : false); OUTPUT @RScriptOutput TO @OutputFilePredictions USING Outputters.Tsv();
以滑鼠右鍵按下 USQL 檔案,然後選取 ADL:生成 R 程式碼後端檔案。
xxx.usql.r 檔案會在您的工作資料夾中產生。 在 R 檔案中撰寫程式代碼。 以下是程式代碼範例。
load("my_model_LM_Iris.rda") outputToUSQL=data.frame(predict(lm.fit, inputFromUSQL, interval="confidence"))
在 USQL 檔案中按下滑鼠右鍵,您可以選取 [編譯腳本] 或 [提交作業] 至執行中作業。
開發 C# 檔案
後端程式碼檔案是與單一 U-SQL 腳本相關的 C# 程式碼檔案。 您可以在程式代碼後置檔案中定義專用於 UDO、UDA、UDT 和 UDF 的腳本。 UDO、UDA、UDT 和 UDF 可以在腳本中直接使用,而不需要先註冊組件。 後置程式碼檔案會放在與其相關的 U-SQL 腳本檔案相同的資料夾中。 如果腳本名為 xxx.usql,則後端代碼名稱為 xxx.usql.cs。 如果您手動刪除程式代碼後置檔案,則會停用其相關聯 U-SQL 腳本的程式代碼後置功能。 如需撰寫 U-SQL 腳本客戶程式代碼的詳細資訊,請參閱在 U-SQL 中撰寫和使用自訂程式碼 :User-Defined Functions。
選取工作區中的 [新增檔案]。
在 U-SQL 檔案中撰寫程式代碼。 以下是程式代碼範例。
@a = EXTRACT Iid int, Starts DateTime, Region string, Query string, DwellTime int, Results string, ClickedUrls string FROM @"/Samples/Data/SearchLog.tsv" USING Extractors.Tsv(); @d = SELECT DISTINCT Region FROM @a; @d1 = PROCESS @d PRODUCE Region string, Mkt string USING new USQLApplication_codebehind.MyProcessor(); OUTPUT @d1 TO @"/output/SearchLogtest.txt" USING Outputters.Tsv();
在 USQL 檔案中按下滑鼠右鍵,然後選取 [ADL:生成 CS 背後代碼文件]。
xxx.usql.cs 檔案會在工作資料夾中產生。 在 CS 檔案中撰寫程式代碼。 以下是程式代碼範例。
namespace USQLApplication_codebehind { [SqlUserDefinedProcessor] public class MyProcessor : IProcessor { public override IRow Process(IRow input, IUpdatableRow output) { output.Set(0, input.Get<string>(0)); output.Set(1, input.Get<string>(0)); return output.AsReadOnly(); } } }
在 USQL 檔案中按下滑鼠右鍵,您可以選取 [編譯腳本] 或 [提交作業] 至執行中作業。
後續步驟
- 使用 Azure 大數據湖工具(適用於 Visual Studio Code)
- 使用 Visual Studio Code U-SQL 本機執行和本機偵錯
- 使用 PowerShell 開始使用 Data Lake Analytics
- 使用 Azure 入口網站開始使用 Data Lake Analytics
- 使用 Data Lake Tools for Visual Studio 來開發 U-SQL 應用程式
- 使用 Data Lake Analytics(U-SQL)資料目錄