gpt4 book ai didi

apache-spark - 如何在配置单元表中插入具有map列的数据框

转载 作者:行者123 更新时间:2023-12-02 19:54:14 24 4
gpt4 key购买 nike

我有一个具有多列的数据框,其中一列是map(string,string)类型。我能够打印具有列为 map 的数据框,从而将数据提供为Map(“PUN”->“Pune”)。我想将此数据帧写入具有与map类型相同列的配置单元表(存储为avro)。

Df.withcolumn("cname", lit("Pune"))
withcolumn("city_code_name", map(lit("PUN"), col("cname"))
Df.show(false)

//table - created external hive table..stored as avro..with avro schema

删除此 map 类型列后,我可以将数据框保存到配置单元avro表。

保存到配置单元表的方式:
  • spark.save-保存Avro文件
  • spark.sql-使用avro文件位置
  • 在hive表上创建分区

    最佳答案

    see this test case as an example from spark tests

      test("Insert MapType.valueContainsNull == false") {
    val schema = StructType(Seq(
    StructField("m", MapType(StringType, StringType, valueContainsNull = false))))
    val rowRDD = spark.sparkContext.parallelize(
    (1 to 100).map(i => Row(Map(s"key$i" -> s"value$i"))))
    val df = spark.createDataFrame(rowRDD, schema)
    df.createOrReplaceTempView("tableWithMapValue")
    sql("CREATE TABLE hiveTableWithMapValue(m Map <STRING, STRING>)")
    sql("INSERT OVERWRITE TABLE hiveTableWithMapValue SELECT m FROM tableWithMapValue")

    checkAnswer(
    sql("SELECT * FROM hiveTableWithMapValue"),
    rowRDD.collect().toSeq)

    sql("DROP TABLE hiveTableWithMapValue")
    }

    另外,如果要保存选项,则可以尝试使用saveAsTable,如 here所示
    Seq(9 -> "x").toDF("i", "j")
    .write.format("hive").mode(SaveMode.Overwrite).option("fileFormat", "avro").saveAsTable("t")

    yourdataframewithmapcolumn.write.partitionBy是创建分区的方法。

    关于apache-spark - 如何在配置单元表中插入具有map列的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60438988/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com