我是新来的pyspark,面临一个奇怪的问题。我试图在加载CSV数据集时将某列设置为非空值。我可以重现我的情况有一个非常小的数据集(test.csv
):pyspark:StructField(...,...,False)总是返回`nullable = true`而不是`nullable = false`
col1,col2,col3
11,12,13
21,22,23
31,32,33
41,42,43
51,,53
有一个在第5行,第2列空值,我不想让我的DF内该行。我将所有字段设置为非空(nullable=false
),但我得到一个架构,其中所有三列都有nullable=true
。即使我将所有三列设置为非空值,也会发生这种情况!我正在运行Spark的最新版本2.0.1。
下面的代码:
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import *
spark = SparkSession \
.builder \
.appName("Python Spark SQL basic example") \
.config("spark.some.config.option", "some-value") \
.getOrCreate()
struct = StructType([ StructField("col1", StringType(), False), \
StructField("col2", StringType(), False), \
StructField("col3", StringType(), False) \
])
df = spark.read.load("test.csv", schema=struct, format="csv", header="true")
df.printSchema()
回报:
root
|-- col1: string (nullable = true)
|-- col2: string (nullable = true)
|-- col3: string (nullable = true)
和df.show()
回报:
+----+----+----+
|col1|col2|col3|
+----+----+----+
| 11| 12| 13|
| 21| 22| 23|
| 31| 32| 33|
| 41| 42| 43|
| 51|null| 53|
+----+----+----+
,同时我希望这样的:
root
|-- col1: string (nullable = false)
|-- col2: string (nullable = false)
|-- col3: string (nullable = false)
+----+----+----+
|col1|col2|col3|
+----+----+----+
| 11| 12| 13|
| 21| 22| 23|
| 31| 32| 33|
| 41| 42| 43|
+----+----+----+