为了解决此问题,需要手动指定数据架构以确保加载器可以在模式中发现删除的列。示例代码如下:
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
# 定义数据架构
schema = StructType([
StructField("name", StringType(), True),
StructField("age", IntegerType(), True),
StructField("address", StringType(), True)
])
# 加载数据
data = spark.read.format("csv").schema(schema).option("header","true").load("path_to_data.csv")
# 模拟删除列
modified_schema = StructType([
StructField("name", StringType(), True),
StructField("address", StringType(), True)
])
# 在使用自动加载器之前,必须手动指定数据架构
data = spark.readStream.schema(modified_schema).csv("path_to_data.csv")
# 使用自动加载器来处理数据流
df = spark.readStream.format("cloudFiles") \
.option("cloudFiles.format", "csv") \
.option("cloudFiles.schemaDefinition", modified_schema.json()) \
.option("cloudFiles.validateOptions", "false") \
.option("cloudFiles.path", "path_to_data.csv").load()
上一篇:AzureDatabricks中Python代码的数据不匹配的问题
下一篇:AzureDatabricks:错误,指定的堆内存(4096MB)超出了节点类型Standard_F4允许的最大执行器内存(3157MB)。