本文介绍了嵌套列上的 DataFrame partitionBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在如下嵌套字段上调用 ​​partitionBy:

I am trying to call partitionBy on a nested field like below:

val rawJson = sqlContext.read.json(filename)
rawJson.write.partitionBy("data.dataDetails.name").parquet(filenameParquet)

运行时出现以下错误.我确实看到名称"列为以下架构中的字段.是否有不同的格式来指定嵌套的列名?

I get the below error when I run it. I do see the 'name' listed as the field in the below schema. Is there a different format to specify the column name which is nested?

java.lang.RuntimeException: 在架构 StructType(StructField(name,StringType,true), StructField(time,StringType,true), StructField(data,StructType(StructField(dataDetails,StructType(StructField(name,StringType,true), StructField(id,StringType,true),true)),true))

这是我的json文件:

This is my json file:

{
  "name": "AssetName",
  "time": "2016-06-20T11:57:19.4941368-04:00",
  "data": {
    "type": "EventData",
    "dataDetails": {
      "name": "EventName"
      "id": "1234"

    }
  }
}

推荐答案

这似乎是此处列出的已知问题:https://issues.apache.org/jira/browse/SPARK-18084

This appears to be a known issue listed here: https://issues.apache.org/jira/browse/SPARK-18084

我也遇到了这个问题,为了解决这个问题,我能够在我的数据集上取消嵌套列.我的数据集与你的数据集略有不同,但这里是策略...

I had this issue as well and to work around it I was able to un-nest the columns on my dataset. My dataset was a little different than your dataset, but here is the strategy...

原始 Json:

{
  "name": "AssetName",
  "time": "2016-06-20T11:57:19.4941368-04:00",
  "data": {
    "type": "EventData",
    "dataDetails": {
      "name": "EventName"
      "id": "1234"

    }
  }
}

修改后的 Json:

{
  "name": "AssetName",
  "time": "2016-06-20T11:57:19.4941368-04:00",
  "data_type": "EventData",
  "data_dataDetails_name" : "EventName",
  "data_dataDetails_id": "1234"
  }
}

获取修改后的 Json 的代码:

Code to get to Modified Json:

def main(args: Array[String]) {
  ...

  val data = df.select(children("data", df) ++ $"name" ++ $"time"): _*)

  data.printSchema

  data.write.partitionBy("data_dataDetails_name").format("csv").save(...)
}

def children(colname: String, df: DataFrame) = {
  val parent = df.schema.fields.filter(_.name == colname).head
  val fields = parent.dataType match {
    case x: StructType => x.fields
    case _ => Array.empty[StructField]
  }
  fields.map(x => col(s"$colname.${x.name}").alias(s"$colname" + s"_" + s"${x.name}"))
}

这篇关于嵌套列上的 DataFrame partitionBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:38