python - DataFrame - 值错误 : Unexpected tuple with StructType

标签 python apache-spark dataframe pyspark apache-spark-sql

我正在尝试为 dataframe 创建一个手动模式。我传入的数据是从 json 创建的 RDD。这是我的初始数据:

json2 = sc.parallelize(['{"name": "mission", "pandas": {"attributes": "[0.4, 0.5]", "pt": "giant", "id": "1", "zip": "94110", "happy": "True"}}'])

然后这里是如何指定模式的:

schema = StructType(fields=[
    StructField(
        name='name',
        dataType=StringType(),
        nullable=True
    ),
    StructField(
        name='pandas',
        dataType=ArrayType(
            StructType(
                fields=[
                    StructField(
                        name='id',
                        dataType=StringType(),
                        nullable=False
                    ),
                    StructField(
                        name='zip',
                        dataType=StringType(),
                        nullable=True
                    ),
                    StructField(
                        name='pt',
                        dataType=StringType(),
                        nullable=True
                    ),
                    StructField(
                        name='happy',
                        dataType=BooleanType(),
                        nullable=False
                    ),
                    StructField(
                        name='attributes',
                        dataType=ArrayType(
                            elementType=DoubleType(),
                            containsNull=False
                        ),
                        nullable=True

                    )
                ]
            ),
            containsNull=True
        ),
        nullable=True
    )
])

当我使用 sqlContext.createDataFrame(json2, schema) 然后尝试对生成的 dataframe 执行 show() 时,我收到以下错误:

ValueError: Unexpected tuple '{"name": "mission", "pandas": {"attributes": "[0.4, 0.5]", "pt": "giant", "id": "1", "zip": "94110", "happy": "True"}}' with StructType

最佳答案

首先 json2 只是一个 RDD[String]。 Spark 没有关于用于编码数据的序列化格式的特殊知识。此外,它需要一个 RDDRow 或某些产品,但显然不是这种情况。

在 Scala 中你可以使用

sqlContext.read.schema(schema).json(rdd) 

使用 RDD[String] 但有两个问题:

  • 在 PySpark 中无法直接访问此方法
  • 即使它是您创建的模式也是无效的:

    • pandas 是一个 struct 而不是 array
    • pandas.happy 不是 string 而是 boolean
    • pandas.attributesstring 不是array

Schema 仅用于避免类型推断而不用于类型转换或任何其他转换。如果你想转换数据,你必须先解析它:

def parse(s: str) -> Row:
    return ...

rdd.map(parse).toDF(schema)

假设您有这样的 JSON(固定类型):

{"name": "mission", "pandas": {"attributes": [0.4, 0.5], "pt": "giant", "id": "1", "zip": "94110", "happy": true}} 

正确的架构如下所示

StructType([
    StructField("name", StringType(), True),
    StructField("pandas", StructType([
        StructField("attributes", ArrayType(DoubleType(), True), True),
        StructField("happy", BooleanType(), True),
        StructField("id", StringType(), True),
        StructField("pt", StringType(), True),
        StructField("zip", StringType(), True))],
    True)])

关于python - DataFrame - 值错误 : Unexpected tuple with StructType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399209/

相关文章:

python - 检查字符串但跳过括号内的数字?

python - 带有空列表的切片分配 - 为什么?

python - 根据 DataFrame 中的值计数保留具有前 n 个项目的所有行

python - 比较数据框中的两列python

python - Unicode解码错误: 'utf8' codec can't decode byte 0xc3 in position 34: unexpected end of data

python - IDLE shell 无法找到 cx_Oracle 的镜像(原因 : image not found)

apache-spark - Pyspark 按顺序将多个 csv 文件读取到数据框中

apache-spark - Spark在yarn模式下增加执行器数量

scala - 从自定义数据格式创建 spark 数据框

R:给定 0 和 1 的矩阵,创建显示行中列之间重复的矩阵