【问题标题】:How to create DDL schema (Hive) out of JSON Schema如何从 JSON Schema 创建 DDL 模式(Hive)
【发布时间】:2020-05-07 18:38:48
【问题描述】:

我使用 pyspark 的 printSchema() 函数创建了 JSON 模式,我想从这个 JSON 中创建一个配置单元 DDL。

我进行了谷歌搜索,但没有找到解决方案。有人有想法吗?

谢谢,

百合

【问题讨论】:

    标签: json pyspark hive schema ddl


    【解决方案1】:

    这是一个 spark python 函数示例,您可以使用该函数从 dataframe 架构创建 DDL 以创建 hive 表。请相应调整。

    def sparkDataFrameCreateTable(df, T = ''):
        cols = df.dtypes
        ddl = []
        ddl.append("CREATE TABLE IF NOT EXISTS {} (".format(T))
        kv =  df.dtypes
        num = len(df.dtypes)
        count = 1
        for i in kv:
            print(count, num, i)
            if count == num:
                total = str(i[0]) + str(" ") + str(i[1])
            else:
                total = str(i[0]) + str(" ") + str(i[1]) + str(", ")
            ddl.append(total)
            count = count + 1
        ddl.append(") STORED AS PARQUET")
        schema_map = ''.join(ddl)
        print(schema_map)
        exec_sql = spark.sql(schema_map)
        return exec_sql
    
    df = spark.range(10)
    spark.sql("create database if not exists junk")
    spark.sql("show databases").show()
    sparkDataFrameCreateTable(df, "junk.test")
    spark.sql("use junk")
    spark.sql("show tables").show()
    
    +------------+
    |databaseName|
    +------------+
    |     default|
    |        junk|
    +------------+
    
    1 1 ('id', 'bigint')
    CREATE TABLE IF NOT EXISTS junk.test (id bigint) STORED AS PARQUET
    +--------+---------+-----------+
    |database|tableName|isTemporary|
    +--------+---------+-----------+
    |    junk|     test|      false|
    +--------+---------+-----------+
    

    【讨论】:

    • 非常感谢,但是我如何使用嵌套的子(带有结构和数组列)j 文件来做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2012-12-26
    • 2019-10-13
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多