首页 > 解决方案 > 将数组的数据类型从 double 更改为 int 的问题

问题描述

我有一组数据,我正在尝试编写一个 python 程序,该程序在将文件加载到数据块中时从模式级别更改数据类型。在将数组的数据类型从 DOUBLE 更改为 INT 时,我不断收到错误

架构

root
 |-- _id: string (nullable = true)
 |-- city: string (nullable = true)
 |-- loc: array (nullable = true)
 |    |-- element: double (containsNull = true)
 |-- pop: long (nullable = true)
 |-- state: string (nullable = true)

我的代码

s= StructType([
StructField("_id",IntegerType(), True),
StructField("city",StringType(), True),
StructField("loc",ArrayType(), True),
StructField("element",DoubleType(), True),
StructField("pop",LongType(), True),
StructField("state",StringType(), True)
])

filepath= "/FileStore/tables/zips.json"
df2= spark.read.format("json").load(filepath, schema=s)
df2.show()

错误

TypeError: __init__() missing 1 required positional argument: 'elementType'

样本数据

在此处输入图像描述

标签: pythonpysparkdatabricks

解决方案


您错过了在ArrayType(elementType)中传递一个参数

错误:elementType 应该是 DataType

from pyspark.sql.types import *

ArrayType(IntegerType())

在此处查看更多信息:文档


推荐阅读