|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use azure_data_cosmos::{ |
| 5 | + clients::{ContainerClientMethods, DatabaseClientMethods}, |
| 6 | + CosmosClient, CosmosClientMethods, PartitionKey, |
| 7 | +}; |
| 8 | +use azure_identity::DefaultAzureCredential; |
| 9 | +use clap::Parser; |
| 10 | +use futures::StreamExt; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +/// An example to show querying a Cosmos DB container. |
| 14 | +#[derive(Parser)] |
| 15 | +pub struct Args { |
| 16 | + /// The Cosmos DB endpoint to connect to. |
| 17 | + endpoint: String, |
| 18 | + |
| 19 | + /// The database to query. |
| 20 | + database: String, |
| 21 | + |
| 22 | + /// The container to query. |
| 23 | + container: String, |
| 24 | + |
| 25 | + /// The query to execute. |
| 26 | + #[clap(long, short)] |
| 27 | + query: String, |
| 28 | + |
| 29 | + /// The partition key to use when querying the container. Currently this only supports a single string partition key. |
| 30 | + #[clap(long, short)] |
| 31 | + partition_key: String, |
| 32 | + |
| 33 | + /// An authentication key to use when connecting to the Cosmos DB account. If omitted, the connection will use Entra ID. |
| 34 | + #[clap(long)] |
| 35 | + #[cfg(feature = "key_auth")] |
| 36 | + key: Option<String>, |
| 37 | +} |
| 38 | + |
| 39 | +#[tokio::main] |
| 40 | +pub async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 41 | + let _ = tracing_subscriber::fmt() |
| 42 | + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 43 | + .init(); |
| 44 | + |
| 45 | + let args = Args::parse(); |
| 46 | + |
| 47 | + let client = create_client(&args); |
| 48 | + |
| 49 | + let db_client = client.database_client(&args.database); |
| 50 | + let container_client = db_client.container_client(&args.container); |
| 51 | + |
| 52 | + let pk = PartitionKey::from(args.partition_key); |
| 53 | + let mut items_pager = |
| 54 | + container_client.query_items::<serde_json::Value>(&args.query, pk, None)?; |
| 55 | + |
| 56 | + while let Some(page) = items_pager.next().await { |
| 57 | + let response = page?; |
| 58 | + println!("Results Page"); |
| 59 | + println!(" Query Metrics: {:?}", response.query_metrics); |
| 60 | + println!(" Index Metrics: {:?}", response.index_metrics); |
| 61 | + println!(" Items:"); |
| 62 | + for item in response.items { |
| 63 | + println!(" * {:?}", item); |
| 64 | + } |
| 65 | + } |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(feature = "key_auth")] |
| 70 | +fn create_client(args: &Args) -> CosmosClient { |
| 71 | + if let Some(key) = args.key.as_ref() { |
| 72 | + CosmosClient::with_key(&args.endpoint, key.clone(), None).unwrap() |
| 73 | + } else { |
| 74 | + let cred = DefaultAzureCredential::new().map(Arc::new).unwrap(); |
| 75 | + CosmosClient::new(&args.endpoint, cred, None).unwrap() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(not(feature = "key_auth"))] |
| 80 | +fn create_client(args: &Args) -> CosmosClient { |
| 81 | + let cred = DefaultAzureCredential::new().map(Arc::new).unwrap(); |
| 82 | + CosmosClient::new(&args.endpoint, cred, None).unwrap() |
| 83 | +} |
0 commit comments