有没有办法通过红移来描述外部/频谱表?

New*_*ria 6 ddl amazon-redshift amazon-redshift-spectrum

在 AWS Athena 中,您可以编写

SHOW CREATE TABLE my_table_name;

并查看描述如何构建表架构的类似 SQL 的查询。 它适用于架构在 AWS Glue 中定义的表。 这对于在常规 RDBMS 中创建表、加载和探索数据视图非常有用。

以这种方式与 Athena 交互是手动的,我想自动化创建常规 RDBMS 表的过程,这些表与 Redshift Spectrum 中的表具有相同的架构。

我如何通过可以运行的查询来做到这一点psql?或者有另一种方法可以通过aws-cli?

bot*_*que 10

Redshift Spectrum支持SHOW CREATE TABLE语法,但有一些系统表可以提供相同的信息。我不得不说,虽然它不如Athena 返回的随时可用的sql有用。

表是

使用该数据,您可以重建表的 DDL。

例如,要以CREATE TABLE一种可以执行的格式获取列及其类型的列表:

select distinct
       listagg(columnname || ' ' || external_type, ',\n') 
             within group ( order by columnnum ) over ()
from svv_external_columns
where tablename = '<YOUR_TABLE_NAME>'
and schemaname = '<YOUR_SCHEM_NAME>'
Run Code Online (Sandbox Code Playgroud)

该查询为您提供类似于以下内容的输出:

col1 int, 
col2 string,
...
Run Code Online (Sandbox Code Playgroud)

*) 我使用的是listagg窗口函数而不是聚合函数,因为显然listagg聚合函数只能与用户定义的表一起使用。无赖。


Joh*_*ark 5

I had been doing something similar to @botchniaque's answer in the past, but recently stumbled across a solution in the AWS-Labs' amazon-redshift-utils code package that seems to be more reliable than my hand-spun queries:

amazon-redshift-utils: v_generate_external_tbl_ddl

If you don't have the ability to create a view backed with the ddl listed in that package, you can run it manually by removing the CREATE statement from the start of the query. Assuming you can create it as a view, usage would be:

SELECT ddl
FROM admin.v_generate_external_tbl_ddl
WHERE schemaname = '<external_schema_name>'
    -- Optionally include specific table references:
    --     AND tablename IN ('<table_name_1>', '<table_name_2>', ..., '<table_name_n>')
ORDER BY tablename, seq
;
Run Code Online (Sandbox Code Playgroud)