I've created a model in R, published into SQL Server table and validated the model by calling it into SQL Server.
However, I'm failing in an attempt to use the model for prediction over new data.
Here's my script:
DROP PROCEDURE IF EXISTS predict_risk_new_data;
GO
CREATE OR ALTER PROCEDURE predict_risk_new_data (#q nvarchar(max))
AS
BEGIN
DECLARE #model varchar(30) = 'risk_rxLogit'
DECLARE #rx_model varbinary(max) = (SELECT model FROM rx_models WHERE model_name = #model);
EXEC sp_execute_external_script
#language = N'R'
,#script = N'
require("RevoScaleR");
input_data = InputDataSet;
model <- unserialize(rx_model);
prediction <- rxPredict(model, input_data, writeModelVars = TRUE);
OutputDataSet <- cbind(predictions[1], predictions[2]);'
,#input_data_1 = #q
,#parallel = 1
,#params = N'#rx_model varbinary(max), #r_rowsPerRead int'
,#input_data_1_name = N'InputDataSet'
,#rx_model = #rx_model
,#r_rowsPerRead = 100
WITH result sets (("Risk_Pred" float, "ZIP" int));
END
GO;
/*
EXEC predict_risk 'SELECT TOP 100 [ZIP], [Week], [Age], [Risk] FROM dbo.Risk'
*/
Here's the error output:
Msg 39004, Level 16, State 20, Line 223 A 'R' script error occurred
during execution of 'sp_execute_external_script' with HRESULT
0x80004004. Msg 39019, Level 16, State 2, Line 223 An external script
error occurred: Error in unserialize(rx_model) : read error Calls:
source -> withVisible -> eval -> eval -> unserialize
Error in execution. Check the output for more information. Error in
eval(expr, envir, enclos) : Error in execution. Check the output
for more information. Calls: source -> withVisible -> eval -> eval ->
.Call Execution halted
New to R/ML in SQL Server, help would be aprreciated.
Thanks in advance.
When I did something like this I had to add as.raw to the model.
Try this
model <- unserialize(as.raw(rx_model));
Related
Here is my function
getSQL <- function(server="server name", database="database name", Uid="
user name", Pwd="password", Query){
conlink <- paste('driver={SQL Server};server=', server,';database=',database,';Uid=', Uid,
';Pwd=', Pwd,';Encrypt=True;TrustServerCertificate=False', sep="")
conn <- odbcDriverConnect(conlink)
dat <- sqlQuery(channel= conn, Query, stringsAsFactors = F)
odbcCloseAll()
return(dat)
}
When I call the function using
query.cut = "SELECT [measurename]
,[OrgType]
,[year_session]
,[Star]
,[cutvalue]
,[Date]
,[File]
FROM [database name].[dbo].[DST_Merged_Cutpoint]
ORDER BY [year_session] DESC
"
getSQL(Query=query.cut)
I get this error:
Error in sqlQuery(conn, Query, stringsAsFactors = F) :
first argument is not an open RODBC channel
In addition: Warning messages:
1: In odbcDriverConnect(conlink) :
[RODBC] ERROR: state 28000, code 18456, message [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user ' insightm8'.
2: In odbcDriverConnect(conlink) :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
3: In odbcDriverConnect(conlink) :
Error in sqlQuery(conn, Query, stringsAsFactors = F) :
first argument is not an open RODBC channel
How can I fix these errors? Thanks in advance
Take care not to add spaces to UID:
Server]Login failed for user ' insightm8'.
Reproducing this on an SQL Server connection creates the same error.
Try using paste0 instead of paste :
conlink <- paste0('driver={SQL Server};server=', server,';database=',database,';Uid=', Uid,
';Pwd=', Pwd,';Encrypt=True;TrustServerCertificate=False', sep="")
The (R) code below works on my machine and when executed in R on the database server. I am also pretty sure that the security is set correctly. Unfortunately, when run as SQL script (see below), I get this message:
cannot change working directory
Calls: source -> withVisible -> eval -> eval -> setwd
Btw, I can setwd and read/write directly on the server using:
setwd("X:/somewhere_amazing/xyz")
So I am bit lost, as to what the issue may be? Any feedback would be very much appreciated! Thanks.
EXEC sp_execute_external_script #language = N'R',
#script = N'
df <- data.frame(x = c("a", "b"))
setwd("\\\\bla\\somemore\\another_subfolder\\")
write.csv(df, "test.csv", row.names=F)
'
I also tried:
EXEC sp_execute_external_script #language = N'R',
#script = N'
df <- data.frame(x = c("a", "b"))
write.csv(df, "\\\\bla\\somemore\\another_subfolder\\test.csv", row.names=F)
'
Airflow-pandas-read-sql-query to dataframe
i am trying to connect to SQL server local to get data from a table and process the data using pandas operations but i m failing to figure out how to pass the select query results to a data frame
the below works to clear data in the table
``` sql_command = """ DELETE FROM [TestDB].[dbo].[PythonTestData] """
t3 = MsSqlOperator( task_id = 'run_test_proc',
mssql_conn_id = 'mssql_local',
sql = sql_command,
dag = dag,
database = 'TestDB',
autocommit = True) ```
the intended pandas is
query = 'SELECT * FROM [ClientData] '#where product_name='''+i+''''''
df = pd.read_sql(query, conn)
pn_list = df['ClientID'].tolist()
#print("The original pn_list is : " + str(pn_list))
for i in pn_list:
varw= str(i)
queryw = 'SELECT * FROM [ClientData] where [ClientID]='''+varw+''
dfw = pd.read_sql(queryw, conn)
dfw = dfw.applymap(str)
cols=['product_id','product_name','brand_id']
x=dfw.values.tolist()
x=x[0]
ClientID=x[0]
Name=x[1]
Org=x[2]
Email=x[3]
#print('Name :'+Name+' ,'+'Org :'+Org+' ,'+'Email :'+Email+' ,'+'ClientID :'+ClientID)
salesData_qry= 'SELECT * FROM [TestDB].[dbo].[SalesData] where [ClientID]='''+ClientID+''
salesData_df= pd.read_sql(salesData_qry, conn)
salesData_df['year1'] = salesData_df['Order Date'].dt.strftime('%Y')
salesData_df['OrderMonth'] = salesData_df['Order Date'].dt.strftime('%b')
filename ='Daily_Campaign_Report_'+Name+'_'+Org+'_'+datetime.now().strftime("%Y%m%d_%H%M%S")
p = Path('C:/Users/user/Documents/WorkingData/')
salesData_df.to_csv(Path(p, filename + '.csv'))```
Please point me to correct approach as i m new to airflow
I'm not so clear on how you generate the query code but in order to get dataframe from MsSQL you need to use MsSqlHook:
from airflow.providers.microsoft.mssql.hooks.mssql import MsSqlHook
def mssql_func(**kwargs):
hook = MsSqlHook(conn_id='mssql_local')
df = hook.get_pandas_df(sql="YOUR_QUERY")
#do whatever you need on the df
run_this = PythonOperator(
task_id='mssql_task',
python_callable=mssql_func,
dag=dag
)
this is the code i am using for the dag
def mssql_func(**kwargs):
conn = MsSqlHook.get_connection(conn_id="mssql_local")
hook = conn.get_hook()
df = hook.get_pandas_df(sql="SELECT * FROM [TestDB].[dbo].[ClientData]")
#do whatever you need on the df
print(df)
run_this = PythonOperator(
task_id='mssql_task',
python_callable=mssql_func,
dag=dag
)
Error Log
[2021-01-12 16:07:15,114] {providers_manager.py:159} WARNING - The provider for package 'apache-airflow-providers-imap' could not be registered from because providers for that package name have already been registered
[2021-01-12 16:07:15,618] {base.py:65} INFO - Using connection to: id: mssql_local. Host: localhost, Port: 1433, Schema: dbo, Login: sa, Password: XXXXXXXX, extra: None
[2021-01-12 16:07:15,626] {taskinstance.py:1396} ERROR - (18456, b"Login failed for user 'sa'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\n")
Traceback (most recent call last):
File "src/pymssql.pyx", line 636, in pymssql.connect
File "src/_mssql.pyx", line 1964, in _mssql.connect
File "src/_mssql.pyx", line 682, in _mssql.MSSQLConnection.__init__
File "src/_mssql.pyx", line 1690, in _mssql.maybe_raise_MSSQLDatabaseException
_mssql.MSSQLDatabaseException: (18456, b"Login failed for user 'sa'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost)\n")
Cron Job is not working after deployment on shinyapp.io
We are trying to schedule some jobs in our shinyapp through cronR, it is working on local system but no as soon as when we are deploying it on shinyapp.io server it is showing a below error.
An error has occurred
The application failed to start (exited with code 1).
Attaching package: ‘DT’
The following objects are masked from ‘package:shiny’:
dataTableOutput, renderDataTable
Adding cronjob:
---------------
# cronR job
# id: temp_data_fetch
# tags: lab, xyz
# desc: temp Data Loading
0-59 * * * * /opt/R/3.5.3/lib/R/bin/Rscript '/srv/connect/apps/Temp/ETL.R' >> '/srv/connect/apps/Temp/ETL.log' 2>&1
Error in value[[3L]](cond) : error in running command
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
#Cron Script
if(!(file.exists("/srv/connect/apps/Temp/scripts_scheduled.rds"))){
cmd <- cronR::cron_rscript(rscript = '/srv/connect/apps/Temp/ETL.R')
cronR::cron_add(cmd, frequency = 'minutely', id = 'temp_data_fetch',description = 'temp Data Loading',tags = c('lab', 'xyz'))
TEXT<-"temp_data_fetch"
saveRDS(TEXT,"/srv/connect/apps/Temp/scripts_scheduled.rds")
}
#ETL.R
trigger_time <- readRDS(file = "/srv/connect/apps/Temp/trigger_time.rds")
trigger_time <- c(trigger_time,paste(Sys.time()))
saveRDS(trigger_time,file = "/srv/connect/apps/Temp/trigger_time.rds")
I am a newbie in Oracle R embedded execution.
well, I have following code registered as
BEGIN
sys.rqScriptDrop('TSFORECAST');
SYS.RQSCRIPTCREATE('TSFORECAST',
'function(dat){
require(ORE)
require(forecast)
myts <- ts(dat,frequency=12)
model <- auto.arima(myts)
fmodel <- forecast(model)
fm = data.frame(fmodel$mean, fmodel$upper,fmodel$lower)
names(fm) <- c("mean","l80","l95","u80","u95")
return(fm)
}'
);
END;
as I execute the function for the first time with this code:
select *
from table(
rqTableEval(
cursor(select balance from tmp_30),
cursor(select 1 as "ore.connect" from dual),
'select 1 mean, 1 l80, 1 l95, 1 u80, 1 u95 from dual',
'TSFORECAST'
)
)
it generates the results I expected. But after that it will never produce any result but instead it raises this error:
ORA-20000: RQuery error
Error in (function () :
unused arguments (width = 480, bg = "white", type = "raster")
ORA-06512: at "RQSYS.RQTABLEEVALIMPL", line 112
ORA-06512: at "RQSYS.RQTABLEEVALIMPL", line 109
20000. 00000 - "%s"
*Cause: The stored procedure 'raise_application_error'
was called which causes this error to be generated.
*Action: Correct the problem as described in the error message or contact
the application administrator or DBA for more information.
I have searched this error but could not find anything helpful. Can anyone help me with this error?