翻譯|使用教程|編輯:黃竹雯|2019-01-11 11:52:52.000|閱讀 403 次
概述:本文主要介紹面向SQL開發人員的數據庫內存分析之如何調用R函數瀏覽和可視化數據
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Microsoft SQL Server是一款強大的關系型數據庫管理系統,可以將原始數據轉化為可發送到任何設備的有意義報告。SQL Server在TPC-E OLTP工作負載、TPC-H數據倉庫工作負載和實際應用程序性能基準方面都有很好的表現。
本文將通過查看示例數據,利用RevoScaleR和R基礎Hist函數(這些R函數已經包含在R數據庫內)中的rxHistogram生成一些平面圖,來介紹如何在Microsoft SQL Server中使用R語言,以及演示如何調用R函數從Transact-SQL的存儲過程中保存應用程序文件的結果。
開發數據科學解決方案通常包括深入的數據探索和數據可視化。在原始的公共數據集中,出租車標識符和行程記錄在單獨的文件中提供。不過為了使示例數據易于使用,兩個原始數據集和medallion列、hack_license以及pickup_datetime進行了連接。所形成的低采樣率數據集有1703957行和23列。
派生列名稱 | 規則 |
---|---|
tipped | 如果tip_amount > 0,則tipped = 1;否則tipped = 0 |
tip_class | class 0:tip_amount = $0 |
class 1:tip_amount > $0 且 tip_amount <= $5 | |
class 2:tip_amount > $5 且 tip_amount <= $10 | |
class 3:tip_amount > $10 且 tip_amount <= $20 | |
class 4:tip_amount > $20 |
使用RevoScaleR提供的增強型R函數中的rxHistogram來創建繪圖,這一步繪制的直方圖是基于Transact-SQL查詢,可以將此函數包裝在存儲過程中。
在SQL Server Management Studio中,右鍵單擊NYCTaxi_Sample數據庫并選擇New Query。粘貼以下腳本來創建一個存儲的過程,繪制直方圖。
CREATE PROCEDURE [dbo].[RxPlotHistogram] AS BEGIN SET NOCOUNT ON; DECLARE @query nvarchar(max) = N'SELECT tipped FROM nyctaxi_sample' EXECUTE sp_execute_external_script @language = N'R', @script = N' image_file = tempfile(); jpeg(filename = image_file); #Plot histogram rxHistogram(~tipped, data=InputDataSet, col=''lightgreen'', title = ''Tip Histogram'', xlab =''Tipped or not'', ylab =''Counts''); dev.off(); OutputDataSet <- data.frame(data=readBin(file(image_file, "rb"), what=raw(), n=1e6)); ', @input_data_1 = @query WITH RESULT SETS ((plot varbinary(max))); END GO
此腳本中的關鍵點包括:
該存儲過程返回的圖像是一個varbinary數據流,顯然無法直接查看該圖像。但是可以使用bcp實用工具獲取此varbinary數據,并將其保存為客戶端計算機上的圖像文件。
EXEC [dbo].[RxPlotHistogram]
bcp "exec RxPlotHistogram" queryout "plot.jpg" -S <SQL Server instance name> -d NYCTaxi_Sample -U <user name> -P <password> -T
Enter the file storage type of field plot [varbinary(max)]: Enter prefix-length of field plot [8]: 0 Enter length of field plot [0]: Enter field terminator [none]: Do you want to save this format information in a file? [Y/n] Host filename [bcp.fmt]:結果
Starting copy... 1 rows copied. Network packet size (bytes): 4096 Clock Time (ms.) Total : 3922 Average : (0.25 rows per sec.)
通常情況下,數據科學家生成多個可視化數據從不同的角度來了解數據。在此示例中,您將創建名為RPlotHist的存儲過程用于編寫直方圖、散點圖和其他R圖形到JPG和PDF格式。此存儲過程使用Hist函數來創建直方圖,為例如JPG、PDF和PNG等常用格式導出二進制數據。
在SQL Server Management Studio中,右鍵單擊NYCTaxi_Sample數據庫并選擇New Query。粘貼以下腳本來創建一個存儲的過程,繪制直方圖。此示例中名為RPlotHist。
CREATE PROCEDURE [dbo].[RPlotHist] AS BEGIN SET NOCOUNT ON; DECLARE @query nvarchar(max) = N'SELECT cast(tipped as int) as tipped, tip_amount, fare_amount FROM [dbo].[nyctaxi_sample]' EXECUTE sp_execute_external_script @language = N'R', @script = N' # Set output directory for files and check for existing files with same names mainDir <- ''C:\\temp\\plots'' dir.create(mainDir, recursive = TRUE, showWarnings = FALSE) setwd(mainDir); print("Creating output plot files:", quote=FALSE) # Open a jpeg file and output histogram of tipped variable in that file. dest_filename = tempfile(pattern = ''rHistogram_Tipped_'', tmpdir = mainDir) dest_filename = paste(dest_filename, ''.jpg'',sep="") print(dest_filename, quote=FALSE); jpeg(filename=dest_filename); hist(InputDataSet$tipped, col = ''lightgreen'', xlab=''Tipped'', ylab = ''Counts'', main = ''Histogram, Tipped''); dev.off(); # Open a pdf file and output histograms of tip amount and fare amount. # Outputs two plots in one row dest_filename = tempfile(pattern = ''rHistograms_Tip_and_Fare_Amount_'', tmpdir = mainDir) dest_filename = paste(dest_filename, ''.pdf'',sep="") print(dest_filename, quote=FALSE); pdf(file=dest_filename, height=4, width=7); par(mfrow=c(1,2)); hist(InputDataSet$tip_amount, col = ''lightgreen'', xlab=''Tip amount ($)'', ylab = ''Counts'', main = ''Histogram, Tip amount'', xlim = c(0,40), 100); hist(InputDataSet$fare_amount, col = ''lightgreen'', xlab=''Fare amount ($)'', ylab = ''Counts'', main = ''Histogram, Fare amount'', xlim = c(0,100), 100); dev.off(); # Open a pdf file and output an xyplot of tip amount vs. fare amount using lattice; # Only 10,000 sampled observations are plotted here, otherwise file is large. dest_filename = tempfile(pattern = ''rXYPlots_Tip_vs_Fare_Amount_'', tmpdir = mainDir) dest_filename = paste(dest_filename, ''.pdf'',sep="") print(dest_filename, quote=FALSE); pdf(file=dest_filename, height=4, width=4); plot(tip_amount ~ fare_amount, data = InputDataSet[sample(nrow(InputDataSet), 10000), ], ylim = c(0,50), xlim = c(0,150), cex=.5, pch=19, col=''darkgreen'', main = ''Tip amount by Fare amount'', xlab=''Fare Amount ($)'', ylab = ''Tip Amount ($)''); dev.off();', @input_data_1 = @query END
此存儲過程內SELECT查詢的輸出存儲在默認的R數據幀InputDataSet中,然后,可以調用各種R繪圖函數來生成實際的圖形文件。
所有文件將都保存到本地文件夾 C:\temp\Plots。此目標文件夾作為存儲過程的一部分提供給R腳本的參數定義。可以通過更改變量mainDir的值更改此目標文件夾。
若要將文件輸出到另一個文件夾,請更改存儲過程中嵌入的R腳本中mainDir的變量值。并且還可以修改腳本以輸出不同格式等等。
運行以下語句將二進制繪圖數據導出到JPEG和PDF文件格式。
EXEC RPlotHist
結果
STDOUT message(s) from external script: [1] Creating output plot files:[1] C:\temp\plots\rHistogram_Tipped_18887f6265d4.jpg[1] C:\temp\plots\rHistograms_Tip_and_Fare_Amount_1888441e542c.pdf[1] C:\temp\plots\rXYPlots_Tip_vs_Fare_Amount_18887c9d517b.pdf
文件名稱中的數字是隨機生成,以確保在嘗試寫入到現有文件時,不會發生錯誤。
若要查看該繪圖,請打開目標文件夾并查看創建的存儲過程中R代碼的文件。
更多SQL Server實用教程敬請關注!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn