SQL R Services (jsonlite, curl)

1

Overview

I would like to use SQL R Services to make a call to an API and pull data directly into SQL Server. I've been trying to use jsonlite and curl within R and it's successful from the R GUI, but fails when making the call through SQL Server T-SQL.

R Script

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

SQL Script

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 Error

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.

Question

Is there another method I should be looking at or is there some simple mistake that is stopping this from working within SQL R Services?

sql-server
json
r
curl
jsonlite
asked on Stack Overflow Nov 3, 2016 by Scott Faculak

1 Answer

0

The problem turned out to be a firewall rule that only effects SQL R Services, but does not effect running the same script through R GUI or R CLI on the same machine.

answered on Stack Overflow Nov 4, 2016 by Scott Faculak

User contributions licensed under CC BY-SA 3.0