Forum Discussion
Help with posting content to azure ml web services
- 9 years ago
Think I figured this out.
1. Create a new query from blank query
2. Immediately change the name of the query to the respecitive name for the function code (i.e. ToAzureMLJson)
- This name is the very last line in the code (after "in")
3. Paste the code in and it should work
I use a R Script step for this, below is the code (default R code from Azure ML with modifications. Green is add, Red is delete, Orange is modify:
library("RCurl")
library("rjson")
createList <- function(dataset)
{
temp <- apply(dataset, 1, function(x) as.vector(paste(x, sep = "")))
colnames(temp) <- NULL
temp <- apply(temp, 2, function(x) as.list(x))
return(temp)
}
# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
h = basicTextGatherer()
hdr = basicHeaderGatherer()
req = list(
Inputs = list(
"input1"= list(
list(
'Customer' = "",
'Contact' = "",
'Owner' = "",
'EstValue' = "1",
'EstCloseYear' = "1",
'EstCloseMonth' = "1",
'EstCloseQuarter' = "",
'ServiceOfferingCode' = "1",
'StateCode' = "1"
)
)
),
GlobalParameters = setNames(fromJSON('{}'), character(0))
)
req = list(
Inputs = list(
"input1" = list(
"ColumnNames" = list("Customer", "Contact", "Owner", "EstValue", "EstCloseYear", "EstCloseMonth", "EstCloseQuarter", "ServiceOfferingCode", "StateCode"),
"Values" = createList(dataset)
) ),
GlobalParameters = setNames(fromJSON('{}'), character(0))
)
body = enc2utf8(toJSON(req))
api_key = "abc123" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')
h$reset()
curlPerform(url = "https://ussouthcentral.services.azureml.net/subscriptions/[guid]/services/[guid]/execute?api-version=2.0&details=true",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)
headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))
# Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure
print(headers)
}
print("Result:")
result = h$value()
print(fromJSON(result))
##Return results back
inter <- do.call("rbind", finalResult$Results$output1$value$Values)
MyFinalResults <- data.frame(inter)
names(MyFinalResults) <- finalResult$Results$output1$value$ColumnNames
rm(list=setdiff(ls(), "MyFinalResults "))
The original code for this comes from Justyna Lucznik's blog article here:
https://powerbi.microsoft.com/en-us/blog/power-bi-azure-ml/
Have you tried this code? It does not work. Look at the end, you did not define the variable finalResult. It gives an error on this. I tried by several ways this code, trying to fix these problems and nothing.. it did not work. It returns a blank table. I am sure that post by Justyna and the webinar by David Brown are missing some points. Would be glad if someone came with the correct code
@smoupre wrote:I use a R Script step for this, below is the code (default R code from Azure ML with modifications. Green is add, Red is delete, Orange is modify:
library("RCurl")
library("rjson")
createList <- function(dataset)
{
temp <- apply(dataset, 1, function(x) as.vector(paste(x, sep = "")))
colnames(temp) <- NULL
temp <- apply(temp, 2, function(x) as.list(x))
return(temp)
}
# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
h = basicTextGatherer()
hdr = basicHeaderGatherer()
req = list(
Inputs = list(
"input1"= list(
list(
'Customer' = "",
'Contact' = "",
'Owner' = "",
'EstValue' = "1",
'EstCloseYear' = "1",
'EstCloseMonth' = "1",
'EstCloseQuarter' = "",
'ServiceOfferingCode' = "1",
'StateCode' = "1"
)
)
),
GlobalParameters = setNames(fromJSON('{}'), character(0))
)
req = list(
Inputs = list(
"input1" = list(
"ColumnNames" = list("Customer", "Contact", "Owner", "EstValue", "EstCloseYear", "EstCloseMonth", "EstCloseQuarter", "ServiceOfferingCode", "StateCode"),
"Values" = createList(dataset)
) ),
GlobalParameters = setNames(fromJSON('{}'), character(0))
)
body = enc2utf8(toJSON(req))
api_key = "abc123" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')
h$reset()
curlPerform(url = "https://ussouthcentral.services.azureml.net/subscriptions/[guid]/services/[guid]/execute?api-version=2.0&details=true",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)
headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))
# Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure
print(headers)
}
print("Result:")
result = h$value()
print(fromJSON(result))
##Return results back
inter <- do.call("rbind", finalResult$Results$output1$value$Values)
MyFinalResults <- data.frame(inter)
names(MyFinalResults) <- finalResult$Results$output1$value$ColumnNames
rm(list=setdiff(ls(), "MyFinalResults "))
The original code for this comes from Justyna Lucznik's blog article here:
https://powerbi.microsoft.com/en-us/blog/power-bi-azure-ml/