|
| 1 | +/** |
| 2 | + * @file unity_catalog_example.cpp |
| 3 | + * @brief Example demonstrating the Databricks Unity Catalog API |
| 4 | + * |
| 5 | + * This example shows how to: |
| 6 | + * 1. List all catalogs in your metastore |
| 7 | + * 2. Get details for a specific catalog |
| 8 | + * 3. List schemas in a catalog |
| 9 | + * 4. List tables in a schema |
| 10 | + */ |
| 11 | + |
| 12 | +#include "databricks/unity_catalog/unity_catalog.h" |
| 13 | +#include "databricks/core/config.h" |
| 14 | +#include <iostream> |
| 15 | +#include <exception> |
| 16 | + |
| 17 | +int main() { |
| 18 | + try { |
| 19 | + // Load configuration from environment |
| 20 | + databricks::AuthConfig auth = databricks::AuthConfig::from_environment(); |
| 21 | + |
| 22 | + std::cout << "Connecting to: " << auth.host << std::endl; |
| 23 | + std::cout << "======================================\n" << std::endl; |
| 24 | + |
| 25 | + // Create Unity Catalog API client (uses API version 2.1 by default) |
| 26 | + databricks::UnityCatalog uc(auth); |
| 27 | + |
| 28 | + // =================================================================== |
| 29 | + // Example 1: List all catalogs |
| 30 | + // =================================================================== |
| 31 | + std::cout << "1. Listing all catalogs:" << std::endl; |
| 32 | + std::cout << "------------------------" << std::endl; |
| 33 | + |
| 34 | + auto catalogs = uc.list_catalogs(); |
| 35 | + std::cout << "Found " << catalogs.size() << " catalogs:\n" << std::endl; |
| 36 | + |
| 37 | + for ( int i = 0; i < std::min(static_cast<int>(catalogs.size()), 10); i++ ) { |
| 38 | + const auto& catalog = catalogs[i]; |
| 39 | + std::cout << " Catalog: " << catalog.name << std::endl; |
| 40 | + std::cout << " Owner: " << catalog.owner << std::endl; |
| 41 | + std::cout << " Type: " << catalog.catalog_type << std::endl; |
| 42 | + std::cout << " Metastore: " << catalog.metastore_id << std::endl; |
| 43 | + if (!catalog.comment.empty()) { |
| 44 | + std::cout << " Comment: " << catalog.comment << std::endl; |
| 45 | + } |
| 46 | + std::cout << std::endl; |
| 47 | + } |
| 48 | + |
| 49 | + // =================================================================== |
| 50 | + // Example 2: Get details for a specific catalog |
| 51 | + // =================================================================== |
| 52 | + if (!catalogs.empty()) { |
| 53 | + std::string catalog_name = catalogs[0].name; |
| 54 | + |
| 55 | + std::cout << "\n2. Getting details for catalog '" << catalog_name << "':" << std::endl; |
| 56 | + std::cout << "-----------------------------------------------------" << std::endl; |
| 57 | + |
| 58 | + auto catalog_details = uc.get_catalog(catalog_name); |
| 59 | + std::cout << " Name: " << catalog_details.name << std::endl; |
| 60 | + std::cout << " Full Name: " << catalog_details.full_name << std::endl; |
| 61 | + std::cout << " Owner: " << catalog_details.owner << std::endl; |
| 62 | + std::cout << " Type: " << catalog_details.catalog_type << std::endl; |
| 63 | + std::cout << " Created At: " << catalog_details.created_at << std::endl; |
| 64 | + std::cout << " Updated At: " << catalog_details.updated_at << std::endl; |
| 65 | + |
| 66 | + if (!catalog_details.properties.empty()) { |
| 67 | + std::cout << " Properties:" << std::endl; |
| 68 | + for (const auto& [key, value] : catalog_details.properties) { |
| 69 | + std::cout << " " << key << ": " << value << std::endl; |
| 70 | + } |
| 71 | + } |
| 72 | + std::cout << std::endl; |
| 73 | + |
| 74 | + // =============================================================== |
| 75 | + // Example 3: List schemas in the catalog |
| 76 | + // =============================================================== |
| 77 | + std::cout << "\n3. Listing schemas in catalog '" << catalog_name << "':" << std::endl; |
| 78 | + std::cout << "-----------------------------------------------------------" << std::endl; |
| 79 | + |
| 80 | + auto schemas = uc.list_schemas(catalog_name); |
| 81 | + std::cout << "Found " << schemas.size() << " schemas:\n" << std::endl; |
| 82 | + |
| 83 | + for (const auto& schema : schemas) { |
| 84 | + std::cout << " Schema: " << schema.name << std::endl; |
| 85 | + std::cout << " Full Name: " << schema.full_name << std::endl; |
| 86 | + std::cout << " Owner: " << schema.owner << std::endl; |
| 87 | + if (!schema.comment.empty()) { |
| 88 | + std::cout << " Comment: " << schema.comment << std::endl; |
| 89 | + } |
| 90 | + std::cout << std::endl; |
| 91 | + } |
| 92 | + |
| 93 | + // =============================================================== |
| 94 | + // Example 4: List tables in the first schema |
| 95 | + // =============================================================== |
| 96 | + if (!schemas.empty()) { |
| 97 | + std::string schema_name = schemas[0].name; |
| 98 | + |
| 99 | + std::cout << "\n4. Listing tables in '" << catalog_name << "." << schema_name << "':" << std::endl; |
| 100 | + std::cout << "----------------------------------------------------------------" << std::endl; |
| 101 | + |
| 102 | + auto tables = uc.list_tables(catalog_name, schema_name); |
| 103 | + std::cout << "Found " << tables.size() << " tables:\n" << std::endl; |
| 104 | + |
| 105 | + for (const auto& table : tables) { |
| 106 | + std::cout << " Table: " << table.name << std::endl; |
| 107 | + std::cout << " Full Name: " << table.full_name << std::endl; |
| 108 | + std::cout << " Type: " << table.table_type << std::endl; |
| 109 | + std::cout << " Format: " << table.data_source_format << std::endl; |
| 110 | + std::cout << " Owner: " << table.owner << std::endl; |
| 111 | + std::cout << " Columns: " << table.columns.size() << std::endl; |
| 112 | + if (!table.comment.empty()) { |
| 113 | + std::cout << " Comment: " << table.comment << std::endl; |
| 114 | + } |
| 115 | + std::cout << std::endl; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + std::cout << "\n======================================" << std::endl; |
| 121 | + std::cout << "Unity Catalog API example completed successfully!" << std::endl; |
| 122 | + |
| 123 | + } catch (const std::exception& e) { |
| 124 | + std::cerr << "Error: " << e.what() << std::endl; |
| 125 | + return 1; |
| 126 | + } |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
0 commit comments