python - 皮斯帕克。如何创建像这样的 df.shema ?

标签 python apache-spark pyspark

我正在尝试创建这样的架构:

root
 |-- _ehid: string (nullable = true)
 |-- duration: double (nullable = true)
 |-- list: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: string (containsNull = true)
 |-- request.id: string (nullable = true)

但我只能创建那个:

root
 |-- _ehid: string (nullable = true)
 |-- duration: double (nullable = true)
 |-- list: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- element: string (nullable = true)
 |-- request.id: string (nullable = true)

我看到元素的结构类型而不是数组。当我尝试通过 df.show(10) 查看我的 df 时,我只看到空值。

我的脚本:

schema = StructType([
    StructField("_ehid", StringType(), True),
    StructField("duration", DoubleType(), True),
    StructField("list", ArrayType(StructType([
            StructField("element", StringType())
        ])), True),
    StructField("request.id", StringType(), True)])

最佳答案

直接使用StringType即可:

schema = StructType([
    StructField("_ehid", StringType(), True),
    StructField("duration", DoubleType(), True),
    StructField("list", ArrayType(ArrayType(StringType())), True),
    StructField("request.id", StringType(), True)])

关于python - 皮斯帕克。如何创建像这样的 df.shema ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53959188/

相关文章:

scala - 从 Uber Jar 中的资源加载 CSV 文件作为数据框

python - 使用 pyspark 从没有 accountkey 的 blob 中读取数据

python - Spark - 创建嵌套数据框

apache-spark - pyspark:使用别名选择列

python - 错误 "' numpy.ndarray'对象没有属性 'values'"

python - 制作数据长度不足的numpy矩阵

python - 删除或加速 PySpark 中的显式 for 循环

apache-spark - Apache Spark SQL UDAF 在窗口上显示重复输入的奇怪行为

python - Pyramid/SQL Alchemy DetachedInstanceError

Python ctypes 和指针作为函数参数