# NOT RUN {
endp <- cosmos_endpoint("https://myaccount.documents.azure.com:443/", key="mykey")
# importing the Star Wars data from dplyr
cont <- endp %>%
get_cosmos_database(endp, "mydatabase") %>%
create_cosmos_container(db, "mycontainer", partition_key="sex")
bulk_import(cont, dplyr::starwars)
query_documents(cont, "select * from mycontainer")
# removing the Cosmos DB metadata cruft
query_documents(cont, "select * from mycontainer", metadata=FALSE)
# a simple filter
query_documents(cont, "select * from mycontainer c where c.gender = 'masculine'")
# run query for one partition key -- zero rows returned
query_documents(cont, "select * from mycontainer c where c.gender = 'masculine'",
partition_key="female")
# aggregates will fail -- API does not fully support cross-partition queries
try(query_documents(cont, "select avg(c.height) avgheight from mycontainer c"))
# Error in process_cosmos_response.response(response, simplify = as_data_frame) :
# Bad Request (HTTP 400). Failed to complete Cosmos DB operation. Message:
# ...
# run query separately by pkrange and combine the results manually
query_documents(
cont,
"select avg(c.height) avgheight, count(1) n from mycontainer c",
by_pkrange=TRUE
)
# }
Run the code above in your browser using DataLab