[英]DataFrame - ValueError: Unexpected tuple with StructType
我正在嘗試為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對用於編碼數據的序列化格式沒有特別的了解。 此外,它預計RDD
或Row
或某些產品,顯然不是這樣。
在Scala你可以使用
sqlContext.read.schema(schema).json(rdd)
使用RDD[String]
但有兩個問題:
即使它是你創建的模式也是無效的:
pandas
是一個struct
not和array
pandas.happy
不是一個boolean
string
pandas.attributes
是string
而不是array
模式僅用於避免類型推斷,而不用於類型轉換或任何其他轉換。 如果要轉換數據,則必須先解析它:
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)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.