【问题标题】:How to obtain SQL column comments如何获取 SQL 列注释
【发布时间】:2019-09-09 14:18:51
【问题描述】:

表中有重要的元数据由

create table t1 (
  column1 type COMMENT '... the comments1...',
  column2 type COMMENT '... the comments2...'
  ...
);

在我们的数据库中有数百个表,每个表都有数百个列COMMENT

我们需要 Spark 或 HQL 语句来获取 SQL 列 cmets。

如何对它进行一些严肃的信息检索?我需要它作为first class table,...或者,如果不可能,一个JSON或@ 987654322@ 包含所有元数据。


注意事项:

  • 我们同时使用 Hive SQL 和 Spark,因此任何答案(HQL 或 Scala/Spark 脚本)都是有效的。

  • 不需要to scrapeSHOW/DESCRIBE TABLES命令,需要认真解决。

  • This question 类似,但仅限于 MySQL...唯一的线索是直接访问 Metastore(在我们的配置中似乎是 Derby SQL 模式 - 没有标准)。

  • This other question还有关于元数据的“严肃信息检索”。

【问题讨论】:

    标签: apache-spark


    【解决方案1】:

    您可以使用 StructField.getComment() 通过 DataFrame 架构检索 cmets

    例如,如果您想将 cmets 作为数据框进行操作

    // create a demo table
    spark.sql("create table t1 (id long comment 'this is an example comment')")
    
    // load table as dataframe
    val t1 = spark.table("t1")
    
    // load column comments as dataframe
    val commentsDf1 = t1.schema.map(f => (f.name,f.getComment)).toDF("name","comment")
    
    // check that everything is loaded correctly
    commentsDf1.show(false)
    +----+--------------------------+
    |name|comment                   |
    +----+--------------------------+
    |id  |this is an example comment|
    +----+--------------------------+
    

    如果要使用SQL访问这些cmets,可以使用DESCRIBE <tablename>达到同样的效果:

    val commentsDf2 = spark.sql("describe t1")
    
    commentsDf2.show(false)
    +--------+---------+--------------------------+
    |col_name|data_type|comment                   |
    +--------+---------+--------------------------+
    |id      |bigint   |this is an example comment|
    +--------+---------+--------------------------+
    

    commentsDf1commentsDf2 都是 first-class citizenscommentsDf2 是表格,commentsDf1 更丰富和复杂。

    【讨论】:

    • 谢谢!似乎是一个未记录的 Spark 功能,getComment...您是否建议提供有关 .schema 和架构实用程序的高级使用的链接或教程?
    • 我不知道是否真的存在。
    猜你喜欢
    • 2020-12-31
    • 2016-10-03
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多