无法在 PySpark 中创建数据框

Cannot create Dataframe in PySpark

我想使用以下代码在 PySpark 中创建一个 Dataframe

from pyspark.sql import *
from pyspark.sql.types import *

temp = Row("DESC", "ID")
temp1 = temp('Description1323', 123)

print temp1

schema = StructType([StructField("DESC", StringType(), False),
                     StructField("ID", IntegerType(), False)])

df = spark.createDataFrame(temp1, schema)

但我收到以下错误:

TypeError: StructType can not accept object 'Description1323' in type type 'str'

我的代码有什么问题?

问题是您传递的是 Row,而您应该传递 Row 的列表。试试这个:

from pyspark.sql import *
from pyspark.sql.types import *

temp = Row("DESC", "ID")
temp1 = temp('Description1323', 123)

print temp1

schema = StructType([StructField("DESC", StringType(), False),
                     StructField("ID", IntegerType(), False)])

df = spark.createDataFrame([temp1], schema)

df.show()

结果:

+---------------+---+
|           DESC| ID|
+---------------+---+
|Description1323|123|
+---------------+---+