How to Consume data from Azure DocumentDB using API in R -
how consume documentdb json data in r dataframes. tried using simple api consumption.
library("json") web_page=geturl("documentdb uri", userpwd = "abc:psswrd")
i followed link "documentdb web api access r" not able figure out how connect.
to query documentdb resources using rest api, first, need generate azure documentdb auth header rest api calls. more details, can refer official documentation , earlier post. second, can interact documentdb making http request package httr
.
for more information on how use rest query documentdb resources, see https://docs.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api.
here sample code list databases using rest r client:
library(digest) library(base64enc) library(httr) sys.setlocale("lc_time", "english") endpoint = "https://{your-database-account}.documents.azure.com"; masterkey = "atpetgjnv3u7ht9ip2mo..."; # replace master key currentdate <- tolower(format(sys.time(), "%a, %d %b %y %t", tz = "gmt", usetz = true)) generatemasterkeyauthorizationsignature <- function(verb, resourceid, resourcetype) { key <- base64decode(masterkey) text <- sprintf("%s\n", paste(tolower(verb), tolower(resourcetype), resourceid, currentdate, "", sep="\n")) body <- enc2utf8(text) signature <- base64encode(hmac(key, body, algo = "sha256", raw = t)) token <- sprintf("type=master&ver=1.0&sig=%s", signature) return(urlencode(token, reserved = true)) } # list databases verb <- "get" resourcetype <- "dbs" resourcelink <- "dbs" resourceid = "" authheader = generatemasterkeyauthorizationsignature(verb, resourceid, resourcetype) headers <- c("x-ms-documentdb-isquery" = "true", "x-ms-date" = currentdate, "x-ms-version" = "2015-08-06", "authorization" = authheader) r <- get(paste(endpoint, resourcelink, sep = "/"), add_headers(headers)) print(content(r, "text"))
execute query
# execute query databaseid <- "familydb" # replace database id collectionid <- "familycoll" # replace collection id verb <- "post" resourcetype <- "docs" resourcelink <- sprintf("dbs/%s/colls/%s/docs", databaseid, collectionid) resourceid = sprintf("dbs/%s/colls/%s", databaseid, collectionid) authheader = generatemasterkeyauthorizationsignature(verb, resourceid, resourcetype) headers <- c("x-ms-documentdb-isquery" = "true", "x-ms-date" = currentdate, "x-ms-version" = "2015-08-06", "authorization" = authheader, "content-type" = "application/query+json") body = list("query" = "select * c") r <- post(paste(endpoint, resourcelink, sep = "/"), add_headers(headers), body = body, encode = "json") print(content(r, "text"))
Comments
Post a Comment