You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jaylmiller (via GitHub)" <gi...@apache.org> on 2023/02/18 16:53:19 UTC

[GitHub] [arrow-datafusion] jaylmiller commented on a diff in pull request #5326: Add example of catalog API usage (#5291)

jaylmiller commented on code in PR #5326:
URL: https://github.com/apache/arrow-datafusion/pull/5326#discussion_r1111059678


##########
datafusion-examples/examples/catalog.rs:
##########
@@ -0,0 +1,287 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Simple example of a catalog/schema implementation.
+//!
+//! Example requires git submodules to be initialized in repo as it uses data from
+//! the `parquet-testing` repo.
+use async_trait::async_trait;
+use datafusion::{
+    arrow::util::pretty,
+    catalog::{
+        catalog::{CatalogList, CatalogProvider},
+        schema::SchemaProvider,
+    },
+    datasource::{
+        file_format::{csv::CsvFormat, parquet::ParquetFormat, FileFormat},
+        listing::{ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl},
+        TableProvider,
+    },
+    error::Result,
+    execution::context::SessionState,
+    prelude::SessionContext,
+};
+use std::sync::RwLock;
+use std::{
+    any::Any,
+    collections::HashMap,
+    path::{Path, PathBuf},
+    sync::Arc,
+};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+    let repo_dir = std::fs::canonicalize(
+        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+            // parent dir of datafusion-examples = repo root
+            .join(".."),
+    )
+    .unwrap();
+    let mut ctx = SessionContext::new();
+    let state = ctx.state();
+    let catlist = Arc::new(CustomCatalogList::new());
+    // use our custom catalog list for context. each context has a single catalog list.
+    // context will by default have MemoryCatalogList
+    ctx.register_catalog_list(catlist.clone());
+
+    // intitialize our catalog and schemas
+    let catalog = DirCatalog::new();
+    let parquet_schema = DirSchema::create(
+        &state,
+        DirSchemaOpts {
+            format: Arc::new(ParquetFormat::default()),
+            dir: &repo_dir.join("parquet-testing").join("data"),
+            ext: "parquet",
+        },
+    )
+    .await?;
+    let csv_schema = DirSchema::create(
+        &state,
+        DirSchemaOpts {
+            format: Arc::new(CsvFormat::default()),
+            dir: &repo_dir.join("testing").join("data").join("csv"),
+            ext: "csv",
+        },
+    )
+    .await?;
+    // register schemas into catalog
+    catalog.register_schema("parquet", parquet_schema.clone())?;
+    catalog.register_schema("csv", csv_schema.clone())?;
+    // register our catalog in the context
+    ctx.register_catalog("dircat", Arc::new(catalog));
+    {
+        // catalog was passed down into our custom catalog list since we overide the ctx's default
+        let catalogs = catlist.catalogs.read().unwrap();
+        assert!(catalogs.contains_key("dircat"));
+    };
+    let parquet_tables = {
+        let tables = parquet_schema.tables.read().unwrap();
+        tables.keys().take(5).cloned().collect::<Vec<_>>()

Review Comment:
   Good point. 5 was totally arbitrary to be honest. Wanted to show a few tables in the output but not fill up the user's entire screen with table results. I'll add that as a comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org