快速入门¶
在这里,我们将简要介绍如何使用 DataFusion 驱动程序来使用 ADBC。
安装¶
添加对 adbc_core 和 adbc_datafusion 的依赖
cargo add adbc_core adbc_datafusion
注意
如果在运行 cargo build 时遇到编译器错误(E0308,类型不匹配)并看到关于依赖图中存在多个版本的 arrow crate 的提示,您可以降级 crate 版本,以确保您的工作区中仅包含一个版本的 arrow crate。
cargo update -p arrow-array@57.0.0 -p arrow-schema@57.0.0 --precise 56.2.0
请注意,上述命令中的具体版本号可能需要根据实际情况更改。请使用 cargo tree 来查找影响您工作区的版本。有关更多信息,请参阅 Cargo 文档中的版本不兼容风险。
加载 DataFusion¶
创建一个驱动程序实例,然后是数据库句柄,最后是连接。(对于 DataFusion 这样的系统来说,这看起来有点多余,但其设计初衷是数据库句柄可以持有多个连接所共享的共有状态。)
// These traits must be in scope
use adbc_core::{Connection, Database, Driver, Statement};
let mut driver = adbc_datafusion::DataFusionDriver {};
let db = driver.new_database().expect("Failed to create database handle");
let mut conn = db.new_connection().expect("Failed to create connection");
运行查询¶
要运行查询,我们可以创建一个语句并设置查询语句
let mut stmt = conn.new_statement().expect("Failed to create statement");
stmt.set_sql_query("SELECT 1").expect("Failed to set SQL query");
然后我们可以执行该查询以获得一个 Arrow RecordBatchReader
let reader = stmt.execute().expect("Failed to execute statement");
for batch in reader {
let batch = batch.expect("Failed to read batch");
println!("{:?}", batch);
}