【问题标题】:How to CREATE TABLE USING delta with Spark 2.4.4?如何使用带有 Spark 2.4.4 的 delta 创建表?
【发布时间】:2020-04-20 03:00:39
【问题描述】:

这是 Spark 2.4.4 和 Delta Lake 0.5.0。

我正在尝试使用 delta 数据源创建一个表,但我似乎遗漏了一些东西。尽管CREATE TABLE USING delta 命令运行良好,但既没有创建表目录,也没有insertInto 运行。

以下CREATE TABLE USING delta 工作正常,但insertInto 失败。

scala> sql("""
create table t5
USING delta
LOCATION '/tmp/delta'
""").show

scala> spark.catalog.listTables.where('name === "t5").show
+----+--------+-----------+---------+-----------+
|name|database|description|tableType|isTemporary|
+----+--------+-----------+---------+-----------+
|  t5| default|       null| EXTERNAL|      false|
+----+--------+-----------+---------+-----------+

scala> spark.range(5).write.option("mergeSchema", true).insertInto("t5")
org.apache.spark.sql.AnalysisException: `default`.`t5` requires that the data to be inserted have the same number of columns as the target table: target table has 0 column(s) but the inserted data has 1 column(s), including 0 partition column(s) having constant value(s).;
  at org.apache.spark.sql.execution.datasources.PreprocessTableInsertion.org$apache$spark$sql$execution$datasources$PreprocessTableInsertion$$preprocess(rules.scala:341)
  ...

我以为我会在创建时定义列,但这也没有用。

scala> sql("""
create table t6
(id LONG, name STRING)
USING delta
LOCATION '/tmp/delta'
""").show
org.apache.spark.sql.AnalysisException: delta does not allow user-specified schemas.;
  at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:325)
  at org.apache.spark.sql.execution.command.CreateDataSourceTableCommand.run(createDataSourceTables.scala:78)
  at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:70)
  at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:68)
  at org.apache.spark.sql.execution.command.ExecutedCommandExec.executeCollect(commands.scala:79)
  at org.apache.spark.sql.Dataset.$anonfun$logicalPlan$1(Dataset.scala:194)
  at org.apache.spark.sql.Dataset.$anonfun$withAction$2(Dataset.scala:3370)
  at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId$1(SQLExecution.scala:78)
  at org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:125)
  at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:73)
  at org.apache.spark.sql.Dataset.withAction(Dataset.scala:3370)
  at org.apache.spark.sql.Dataset.<init>(Dataset.scala:194)
  at org.apache.spark.sql.Dataset$.ofRows(Dataset.scala:79)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:642)
  ... 54 elided

【问题讨论】:

    标签: apache-spark apache-spark-sql delta-lake


    【解决方案1】:

    Delta 的 OSS 版本目前还没有 SQL Create Table 语法。这将在使用 Spark 3.0 的未来版本中实现。

    要创建 Delta 表,您必须写出 Delta 格式的 DataFrame。 Python中的一个例子是

    df.write.format("delta").save("/some/data/path")
    

    这是用于 Python、Scala 和 Java 的创建表文档的link

    【讨论】:

      【解决方案2】:

      pyspark 3.0.0 和 delta 0.7.0 的示例

      print(f"LOCATION '{location}")
      spark.sql(f"""
      CREATE OR REPLACE TABLE  {TABLE_NAME} (
        CD_DEVICE INT, 
        FC_LOCAL_TIME TIMESTAMP,  
        CD_TYPE_DEVICE STRING,
        CONSUMTION DOUBLE,
        YEAR INT,
        MONTH INT, 
        DAY INT )
      USING DELTA
      PARTITIONED BY (YEAR , MONTH , DAY, FC_LOCAL_TIME)
      LOCATION '{location}'
      """)
      

      其中“位置”是用于 spark 集群模式保存 de delta 表的 dir HDFS。

      【讨论】:

        【解决方案3】:

        tl;dr 3.0.0 之前的 Spark 和 0.7.0 之前的 Delta Lake 不支持 CREATE TABLE USING delta


        Delta Lake 0.7.0 和 Spark 3.0.0(都刚刚发布)支持CREATE TABLE SQL 命令。

        确保使用spark.sql.catalog.spark_catalog 配置属性和org.apache.spark.sql.delta.catalog.DeltaCatalog 来“安装”Delta SQL。

        $ ./bin/spark-submit \
          --packages io.delta:delta-core_2.12:0.7.0 \
          --conf spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension \
          --conf spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog
        
        scala> spark.version
        res0: String = 3.0.0
        
        scala> sql("CREATE TABLE delta_101 (id LONG) USING delta").show
        ++
        ||
        ++
        ++
        
        scala> spark.table("delta_101").show
        +---+
        | id|
        +---+
        +---+
        
        scala> sql("DESCRIBE EXTENDED delta_101").show(truncate = false)
        +----------------------------+---------------------------------------------------------+-------+
        |col_name                    |data_type                                                |comment|
        +----------------------------+---------------------------------------------------------+-------+
        |id                          |bigint                                                   |       |
        |                            |                                                         |       |
        |# Partitioning              |                                                         |       |
        |Not partitioned             |                                                         |       |
        |                            |                                                         |       |
        |# Detailed Table Information|                                                         |       |
        |Name                        |default.delta_101                                        |       |
        |Location                    |file:/Users/jacek/dev/oss/spark/spark-warehouse/delta_101|       |
        |Provider                    |delta                                                    |       |
        |Table Properties            |[]                                                       |       |
        +----------------------------+---------------------------------------------------------+-------+
        

        【讨论】:

        • 应用提到的 spark 配置为我解决了这个问题。
        猜你喜欢
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多