View a markdown version of this page

Usa l'API dei metadati del driver Amazon Redshift per applicazioni e strumenti - Amazon Redshift

Amazon Redshift non supporterà più la creazione di nuove UDF Python a partire dalla Patch 198. Le UDF Python esistenti continueranno a funzionare fino al 30 giugno 2026. Per ulteriori informazioni, consulta il post del blog.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Usa l'API dei metadati del driver Amazon Redshift per applicazioni e strumenti

Per le applicazioni e gli strumenti che si connettono ad Amazon Redshift, come uno strumento di business intelligence o un editor di query, ti consigliamo di utilizzare l'API dei metadati dei driver fornita dai driver Amazon Redshift JDBC 2.x, ODBC 2.x o Python per scoprire i metadati sugli oggetti del tuo data warehouse, inclusi database, schemi, tabelle, colonne e tipi di dati. In alternativa, puoi utilizzare i comandi Amazon RedshiftSHOW.

Utilizza l'API dei metadati dei driver per i seguenti vantaggi:

  • Specification-compliant. I driver JDBC e ODBC implementano interfacce di metadati standard (DatabaseMetaDatain JDBC e in ODBC). SQLTables SQLColumns Poiché Python's DB-API (PEP 249) non definisce una specifica API per i metadati, il driver Amazon Redshift Python segue le specifiche DatabaseMetaData JDBC, fornendo metodi equivalenti come, e. get_tables() get_columns() get_schemas() Queste API seguono specifiche ben definite, quindi il codice di integrazione è portatile. Man mano che Amazon Redshift evolve le sue tabelle di sistema interne, non è necessario modificare l'applicazione.

  • Performance-optimized. L'API dei metadati dei driver è ottimizzata per restituire i metadati in modo efficiente. AWS continua a investire nelle prestazioni delle API per i metadati dei driver.

  • Forward-compatible. Amazon Redshift rispetta le specifiche dei connettori JDBC, ODBC e Python. Quando esegui il codice utilizzando queste API standard, l'applicazione è protetta dalle modifiche alla struttura del catalogo di sistema sottostante.

Esempio: utilizzo di JDBC DatabaseMetaData.getTables () per recuperare i metadati delle tabelle

DatabaseMetaData dbmd = connection.getMetaData(); // getTables(catalog, schemaPattern, tableNamePattern, types) // catalog: "test" — filters to the database named "test" // schemaPattern: "test_pattern" — filters schemas matching this pattern (supports SQL wildcards % and _) // tableNamePattern: null — no filter, returns all table names // types: {"TABLE", "EXTERNAL TABLE"} — only return regular tables and external tables ResultSet rs = dbmd.getTables("test", "test_pattern", null, new String[] {"TABLE", "EXTERNAL TABLE"});

Esempio: utilizzo di Python cursor.get_columns () per recuperare i metadati delle colonne

cursor: redshift_connector.Cursor = conn.cursor() # get_columns(catalog, schema_pattern, table_name_pattern, column_name_pattern) # catalog: 'test' — filters to the database named "test" # schema_pattern: 'test_pattern' — filters schemas matching this pattern (supports SQL wildcards % and _) # table_name_pattern: 'testabc' — filters to the table named "testabc" # column_name_pattern: '%' — wildcard, returns all columns in the matching table result: tuple = cursor.get_columns('test', 'test_pattern', 'testabc', '%')

Esempio: utilizzo di ODBC () per recuperare i metadati della chiave primaria SQLPrimaryKeys

// SQLPrimaryKeys(hstmt, catalog, catalog_len, schema, schema_len, table, table_len) // catalog: "test" — filters to the database named "test" // schema: "test_schema" — filters to the schema named "test_schema" // table: "test_table" — retrieves primary key columns for this table // Note: Unlike getTables/getColumns, SQLPrimaryKeys does NOT support wildcard patterns. retcode = SQLPrimaryKeys(hstmt, (SQLCHAR *)"test", SQL_NTS, (SQLCHAR *)"test_schema", SQL_NTS, (SQLCHAR *)"test_table", SQL_NTS); while (SQL_SUCCEEDED(retcode = SQLFetch(hstmt))) { for (i = 1; i <= columns; i++) { retcode = SQLGetData(hstmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator); } }

Esempio: utilizzo di ODBC SQLTables () per elencare database e schemi

L'API ODBC non fornisce funzioni separate per elencare cataloghi o schemi. Si utilizzano invece speciali convenzioni di chiamata SQLTables() per recuperare queste informazioni.

Per elencare tutti i database (cataloghi)

Chiama SQLTables() con CatalogName set to. SQL_ALL_CATALOGS Imposta SchemaName e TableName su stringhe vuote. Il set di risultati restituisce valori validi solo nella TABLE_CAT colonna. Tutte le altre colonne contengono valori NULL.

// List all catalogs (databases) available on the data source. retcode = SQLTables(hstmt, (SQLCHAR *)SQL_ALL_CATALOGS, SQL_NTS, // CatalogName = "%" (SQL_ALL_CATALOGS) (SQLCHAR *)"", 0, // SchemaName = "" (empty string) (SQLCHAR *)"", 0, // TableName = "" (empty string) NULL, 0); // TableType = NULL (not filtered)

Per elencare tutti gli schemi

Chiama SQLTables() con SchemaName set to. SQL_ALL_SCHEMAS Imposta CatalogName e TableName su stringhe vuote.

// List all schemas available on the data source. retcode = SQLTables(hstmt, (SQLCHAR *)"", 0, // CatalogName = "" (empty string) (SQLCHAR *)SQL_ALL_SCHEMAS, SQL_NTS, // SchemaName = "%" (SQL_ALL_SCHEMAS) (SQLCHAR *)"", 0, // TableName = "" (empty string) NULL, 0); // TableType = NULL (not filtered)
Nota

La specifica ODBC definisce TABLE_SCHEM come valido solo per l'enumerazione dello schema. Amazon Redshift si popola anche TABLE_CAT perché supporta il rilevamento di metadati tra database e ogni schema è limitato a un database specifico.