【问题标题】:SQL R Services (jsonlite, curl)SQL R 服务(jsonlite、curl)
【发布时间】:2016-11-03 14:25:12
【问题描述】:

概述

我想使用 SQL R Services 调用 API 并将数据直接拉入 SQL Server。我一直在尝试在 R 中使用 jsonlite 和 curl,它在 R GUI 中是成功的,但是在通过 SQL Server T-SQL 进行调用时失败。

R 脚本

library(jsonlite,curl);
citibike <- fromJSON('http://citibikenyc.com/stations/json');
stations <- citibike$stationBeanList;
stations[,c(2,10)];

SQL 脚本

DECLARE @Rscript NVARCHAR(MAX);
SET @Rscript = N'library(jsonlite, curl);
    citibike <- fromJSON('+''''+'http://citibikenyc.com/stations/json'+''''+');
     stations <- citibike$stationBeanList;
     OutputDataSet <- subset(stations, select=c("stationName", "stAddress1"))';
EXEC sp_execute_external_script
     @language = N'R',
     @script = @Rscript
    WITH RESULT SETS(([stationName] VARCHAR(500), [stAddress1] VARCHAR(500)));

SQL 错误

Msg 39004, Level 16, State 20, Line 0
A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 1, Line 0
An external script error occurred: 
Error in open.connection(con, "rb") : Couldn't connect to server
Calls: source ... fromJSON_string -> parseJSON -> parse_con -> open -> open.connection
In addition: Warning message:
package 'jsonlite' was built under R version 3.3.2 

Error in ScaleR.  Check the output for more information.
Error in eval(expr, envir, enclos) : 
  Error in ScaleR.  Check the output for more information.
Calls: source -> withVisible -> eval -> eval -> .Call
Execution halted
Msg 11536, Level 16, State 1, Line 8
EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time.

问题

我是否应该考虑另一种方法,或者是否有一些简单的错误阻止了它在 SQL R Services 中的工作?

【问题讨论】:

  • 请注意,library(jsonlite,curl)加载jsonlite
  • 是的,我并不是在暗示这是您的问题的根源,而是值得修复的额外问题。

标签: sql-server json r curl jsonlite


【解决方案1】:

问题原来是防火墙规则,它只影响 SQL R 服务,但不影响通过 R GUI 或 R CLI 在同一台机器上运行相同的脚本。

【讨论】:

  • 是的,我们设置了防火墙规则,默认情况下阻止从 SQL Server 内部执行的 R 脚本的所有出站网络访问。这主要是出于安全原因,而且生产数据库系统通常没有任何互联网访问权限(这也是我们推荐的设置)。通常不建议从 T-SQL 过程访问外部资源,并且如果出现性能问题(由于网站或网络共享速度较慢),则很难进行故障排除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
相关资源
最近更新 更多