use std::{collections::HashMap, sync::Arc};
use datafusion::prelude::SessionContext;
use datafusion::sql::TableReference;
use datafusion_table_providers::{
mongodb::{connection_pool::MongoDBConnectionPool, MongoDBTableFactory},
util::secrets::to_secret_map,
};
#[tokio::main]
async fn main() {
let mongodb_params = to_secret_map(HashMap::from([(
"connection_string".to_string(),
"mongodb://root:password@localhost:27017/mongo_db?authSource=admin&tls=false".to_string(),
)]));
let mongodb_pool = Arc::new(
MongoDBConnectionPool::new(mongodb_params)
.await
.expect("unable to create MongoDB connection pool"),
);
let table_factory = MongoDBTableFactory::new(mongodb_pool.clone());
let ctx = SessionContext::new();
ctx.register_table(
"companies_v2",
table_factory
.table_provider(TableReference::bare("companies"))
.await
.expect("failed to register table provider"),
)
.expect("failed to register table");
let df = ctx
.sql("SELECT * FROM datafusion.public.companies_v2")
.await
.expect("select failed");
df.show().await.expect("show failed");
}