筛选spark分区表在pyspark中不起作用

yhived7q  于 2021-07-13  发布在  Spark
关注(0)|答案(1)|浏览(396)

我使用的是spark2.3,并且已经编写了一个dataframe来使用pyspark中的dataframe writer类方法创建hive分区表。

newdf.coalesce(1).write.format('orc').partitionBy('veh_country').mode("overwrite").saveAsTable('emp.partition_Load_table')

这是我的表结构和分区信息。

hive> desc emp.partition_Load_table;
OK
veh_code                varchar(17)
veh_flag                varchar(1)
veh_model               smallint
veh_country             varchar(3)

# Partition Information

# col_name              data_type               comment

veh_country              varchar(3)

hive> show partitions partition_Load_table;
OK
veh_country=CHN
veh_country=USA
veh_country=RUS

现在我在pyspark的Dataframe中读取这个表。

df2_data = spark.sql("""
    SELECT * 
    from udb.partition_Load_table
    """);

df2_data.show() --> is working

但是我不能用分区键列过滤它

from pyspark.sql.functions import col
newdf = df2_data.where(col("veh_country")=='CHN')

我收到以下错误消息:

: java.lang.RuntimeException: Caught Hive MetaException attempting to get partition metadata by filter from Hive. 
You can set the Spark configuration setting spark.sql.hive.manageFilesourcePartitions to false to work around this problem, 
however this will result in degraded performance. Please report a bug: https://issues.apache.org/jira/browse/SPARK
Caused by: MetaException(message:Filtering is supported only on partition keys of type string)

而当我通过指定表的hdfs绝对路径来创建dataframe时。筛选器和where子句按预期工作。

newdataframe = spark.read.format("orc").option("header","false").load("hdfs/path/emp.db/partition_load_table")

下面是工作

newdataframe.where(col("veh_country")=='CHN').show()

我的问题是,为什么它不能过滤Dataframe放在首位。还有为什么它抛出一个错误消息“过滤只支持string类型的分区键”,即使我的vehèu国家定义为string或varchar数据类型。

eeq64g8w

eeq64g8w1#

我也偶然发现了这个问题。对我有帮助的是这样做:

spark.sql("SET spark.sql.hive.manageFilesourcePartitions=False")

然后使用 spark.sql(query) 而不是使用Dataframe。
我不知道引擎盖下发生了什么,但这解决了我的问题。
虽然对你来说可能太迟了(因为这个问题是8个月前提出来的),但这对其他人可能有帮助。

相关问题