Microsoft R Server逐行插入

时间:2017-08-08 17:52:33

标签: sql-server r stored-procedures microsoft-r

我有一个通常写入平面文件的for循环。这样,如果有什么破坏,我可以从我离开的地方开始。我想将此进程转换为直接使用执行我的R代码的SQL Server 2016存储过程中的新RevoScaleR函数从SQL表读取和写入。

这是一个简单的SPROC:

USE [master]
GO

/****** Object:  StoredProcedure [dbo].[Rscript_geocodeUSACities_TEST]    Script Date: 8/8/2017 11:40:40 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Rscript_geocodeUSACities_TEST]
    @usrOutputFilePath varchar(150)
    ,@usrOutputFileName varchar(150)

AS
BEGIN

    SET NOCOUNT ON;

DECLARE @rScript nvarchar(max) = N'

#### USER INPUTS ####

usrOutputFile <- "' + @usrOutputFilePath + @usrOutputFileName + '"


#### ESTABLISH ENVIRONMENT ####

library(data.table)
library(foreach)
library(XML)
library(RCurl)
library(RJSONIO)

##turn off scientific notation
options(scipen=999)

##establish compute context
sqlServerConnString <- "Server=.;Database=External;Trusted_Connection=true"
sqlServerCC <- RxInSqlServer(connectionString=sqlServerConnString)
rxSetComputeContext(sqlServerCC)
print(rxGetComputeContext())


#### GEOCODE ####

print(dfInputData)
rxDataStep(data=dfInputData,outFile=imp.USA_Cities_Map,append="rows")

'

EXECUTE  sp_execute_external_script
                @language = N'R'
              , @script = @rScript
              ,@input_data_1 =N'select 5 as test_insert'
            ,@input_data_1_name =N'dfInputData'
              ;

END

错误输出:

Error in rxDataStep(data = dfInputData, outFile = imp.USA_Cities_Map,  : 
  object 'imp.USA_Cities_Map' not found

1 个答案:

答案 0 :(得分:4)

你走了。您不需要将计算上下文设置为SQL Server。但您必须向运行R外部进程的本地用户授予登录权限。它们都被添加到名为SqlRUserGroup的本地组中,您只需要替换&#39; dbrownebook&#39;使用您的服务器名称。

请注意,您不为sqlrusergroup添加数据库用户,但只添加登录名。 SQL R服务将模拟调用sp_execute_external_script的用户。这在[{3}}

中有解释
use master
go

create login [dbrownebook\sqlrusergroup] from windows

create database [External]

go

use [External]
go

create schema imp
go
create table imp.USA_Cities_Map(test_insert int)
go


/****** Object:  StoredProcedure [dbo].[Rscript_geocodeUSACities_TEST]    Script Date: 8/8/2017 11:40:40 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE OR ALTER PROCEDURE [dbo].[Rscript_geocodeUSACities_TEST]
    @usrOutputFilePath varchar(150)
    ,@usrOutputFileName varchar(150)

AS
BEGIN

    SET NOCOUNT ON;

DECLARE @rScript nvarchar(max) = N'

sqlServerConnString <- "Server=.;Database=External;Trusted_Connection=true"
sqlTable <- RxSqlServerData(table = "imp.USA_Cities_Map", connectionString = sqlServerConnString)

rxDataStep(data=dfInputData,outFile=sqlTable,append="rows")
rxDataStep(data=dfInputData,outFile=sqlTable,append="rows")
rxDataStep(data=dfInputData,outFile=sqlTable,append="rows")

'

EXECUTE  sp_execute_external_script
                @language = N'R'
              , @script = @rScript
              ,@input_data_1 =N'select 5 as test_insert'
            ,@input_data_1_name =N'dfInputData'
              ;

END

GO

exec [Rscript_geocodeUSACities_TEST] '',''

go
select * from imp.USA_Cities_Map