在 pyspark2 中读取文本文件



我正在尝试使用 python 读取 spark 2.3 中的文本文件,但出现此错误。 这是文本文件的格式:

name marks
amar 100
babul 70
ram 98
krish 45

法典:

df=spark.read.option("header","true")
.option("delimiter"," ")
.option("inferSchema","true")
.schema(
StructType(
[
StructField("Name",StringType()),
StructField("marks",IntegerType())
]
)
)
.text("file:/home/maria_dev/prac.txt") 

错误:

java.lang.AssertionError: assertion failed: Text data source only
produces a single data column named "value"

当我尝试将文本文件读入RDD时,它被收集为单个列。

应该更改数据文件还是应该更改代码?

而不是.text(仅产生单值列(,请使用.csv将文件加载到DF中。

>>> df=spark.read.option("header","true")
.option("delimiter"," ")
.option("inferSchema","true")
.schema(
StructType(
[
StructField("Name",StringType()),
StructField("marks",IntegerType())
]
)
)
.csv('file:///home/maria_dev/prac.txt') 
>>> from pyspark.sql.types import *
>>> df
DataFrame[Name: string, marks: int]
>>> df.show(10,False)
+-----+-----+
|Name |marks|
+-----+-----+
|amar |100  |
|babul|70   |
|ram  |98   |
|krish|45   |
+-----+-----+

最新更新