如何在 R 中使用 API 从 Azure DocumentDB 使用数据
How to Consume data from Azure DocumentDB using API in R
如何在 R 数据帧中使用 documentdb JSON 数据。我尝试使用简单的 API 消费。
library("JSON")
web_page=getURL("documentdb URI", userpwd = "abc:psswrd")
我也跟着link“”但是不知道怎么连接。
要使用 REST API 查询 DocumentDB 资源,首先,您需要为 REST API 调用生成 Azure documentDB 身份验证 header。详情请参考official documentation and my earlier post. Second, you can interact with DocumentDB by making an HTTP request with the package httr
.
有关如何使用 REST 查询 DocumentDB 资源的详细信息,请参阅 https://docs.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api。
下面是使用 R 客户端的 REST 列出所有数据库的示例代码:
library(digest)
library(base64enc)
library(httr)
Sys.setlocale("LC_TIME", "English")
endpoint = "https://{your-database-account}.documents.azure.com";
masterKey = "aTPETGJNV3u7ht9Ip2mo..."; # replace with your 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 all 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 a query
databaseId <- "FamilyDB" # replace with your database ID
collectionId <- "FamilyColl" # replace with your 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 * FROM c")
r <- POST(paste(endpoint, resourceLink, sep = "/"), add_headers(headers), body = body, encode = "json")
print(content(r, "text"))
如何在 R 数据帧中使用 documentdb JSON 数据。我尝试使用简单的 API 消费。
library("JSON")
web_page=getURL("documentdb URI", userpwd = "abc:psswrd")
我也跟着link“
要使用 REST API 查询 DocumentDB 资源,首先,您需要为 REST API 调用生成 Azure documentDB 身份验证 header。详情请参考official documentation and my earlier post. Second, you can interact with DocumentDB by making an HTTP request with the package httr
.
有关如何使用 REST 查询 DocumentDB 资源的详细信息,请参阅 https://docs.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api。
下面是使用 R 客户端的 REST 列出所有数据库的示例代码:
library(digest)
library(base64enc)
library(httr)
Sys.setlocale("LC_TIME", "English")
endpoint = "https://{your-database-account}.documents.azure.com";
masterKey = "aTPETGJNV3u7ht9Ip2mo..."; # replace with your 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 all 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 a query
databaseId <- "FamilyDB" # replace with your database ID
collectionId <- "FamilyColl" # replace with your 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 * FROM c")
r <- POST(paste(endpoint, resourceLink, sep = "/"), add_headers(headers), body = body, encode = "json")
print(content(r, "text"))