SQL Server 机器学习调用外部 REST API

时间:2020-12-28 18:48:00

标签: python sql sql-server machine-learning microsoft-machine-learning-server

我正在使用 SQL Server 机器学习测试一些场景。我试图从 SQL Server Management Studio 中的 Python 脚本内部查询外部 Web 服务。像这样:

EXEC sp_execute_external_script
@language =N'Python', 
@script=N'
import os
os.system("python \"C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\Lib\cust\ml.py\"")

OutputDataSet = InputDataSet
print("Say Hello from SQL Server Management Studio")
',
@input_data_1 =N'SELECT 42' 
WITH RESULT SETS (([TheAnswer] int not null));  
GO 

ml.py 脚本看起来像(简化):

#!"C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\python.exe"
import pandas as pd
import urllib3

http = urllib3.PoolManager()
response  = http.request('GET', 'https://example.com/myWS.json')

df = pd.read_csv(response.data)  # Web Service returns a plain csv like payload.

# Keep doing something interesting

当我在控制台上运行 ml.py 时,它运行得很好。但是,当我尝试从 SQL Server Management Studio 运行时,出现以下错误:

Msg 39019, Level 16, State 2, Line 1
An external script error occurred: 
SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
There was an error getting the leads fields: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: /myWS.json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000002076ECACC18>: Failed to establish a new connection: [WinError 10013] An attempt to access a socket in a way forbidden by its access permissions was attempted'))

所以,很明显需要设置一些通信权限,但我不确定应该在哪里设置它们。

我的目标是将 Web 服务的结果直接插入到 SQL 查询中,而无需从外部文件导入。我不确定这是最好的方法,但我只是想测试一下它的可行性。

欢迎提出任何想法。

谢谢!

0 个答案:

没有答案
相关问题