【发布时间】:2021-12-23 23:40:59
【问题描述】:
我正在使用 pyspark 从多行 json 对象中提取数据。我可以读取文件,但无法解析几何列的内容。
整体表格的示例如下所示。
+--------------------+--------------------+-------+
| geometry| properties| type|
+--------------------+--------------------+-------+
|{[13.583336, 37.2...|{AGRIGENTO, AGRIG...|Feature|
|{[13.584538, 37.3...|{AGRIGENTO, AGRIG...|Feature|
|{[13.657838, 37.3...|{FAVARA, AGRIGENT...|Feature|
|{[13.846247, 37.3...|{CANICATTÌ, AGRI...|Feature|
|{[13.616626, 37.4...|{ARAGONA, AGRIGEN...|Feature|
|{[13.108426, 37.6...|{SAMBUCA DI SICIL...|Feature|
|{[16.709313, 41.0...|{GRUMO APPULA, BA...|Feature|
|{[12.670994, 41.4...|{NETTUNO, ROMA, 6...|Feature|
|{[12.501805, 42.1...|{CASTELNUOVO DI P...|Feature|
|{[12.608105, 41.4...|{ANZIO, ROMA, b54...|Feature|
+--------------------+--------------------+-------+
这是json几何列的单行格式
"geometry":{"type":"Point","coordinates":[13.583336,37.270182]}
当我提取架构时,这就是它的样子
StructType(List(StructField("geometry",StructType(List(StructField("coordinates",ArrayType(DoubleType,true),true),StructField("type",StringType,true))),true)
但是,当我尝试在 PySpark 中设置架构以导入数据时,出现以下错误。
AttributeError: 'tuple' 对象没有属性 'name'
这是我正在使用的代码。
from pyspark.sql.types import StructField, StructType, StringType, FloatType, ArrayType, DoubleType
import pyspark.sql.functions as F
df = spark.read.option("multiLine", False).option("mode", "PERMISSIVE").json('Italy/it_countrywide-addresses-country.geojson')
schema = StructType([
(StructField("coordinates",
ArrayType(DoubleType())),
StructField("type",StringType()))
])
df.withColumn("geometry", F.from_json("geometry", schema)).select(col('geometry.*')).show()
我欢迎你的 cmets。
【问题讨论】:
-
我没有回答这个问题,但找到了解决方法。导入文件两次(必须是更好的方法)并在其中一个导入上获取模式并将其用作第二次导入的参数。
json_schema = spark.read.option("multiLine", False).option("mode", "PERMISSIVE").json('Italy/it_countrywide-addresses-country.geojson').schema df_with_schema = spark.read.option("multiLine", False).option("mode", "PERMISSIVE").schema(json_schema).json('Italy/it_countrywide-addresses-country.geojson') df_with_schema.printSchema() coordinates = df_with_schema.select(F.col('geometry.coordinates')) -
如果您有可行的解决方案,请随时回答您自己的问题。它可能会帮助其他人,并且不会阻止人们提供“更好”的答案。
-
错误是说你在 StructType 数组中有额外的
(。StructType应该是StructField的数组,而不是元组。 -
@Emmathanx !你救了我的 a$$ 我要疯了 ^^
标签: python json apache-spark pyspark apache-spark-sql