如何避免在从 pyspark 以 ORC 格式对 Hive 表执行查询时断言错误?



我正在从PySpark运行一个简单的Hive查询,但它抛出了一个错误。该表采用 ORC 格式。需要一些帮助。下面是代码

spark = SparkSession.builder.appName("Termination_Calls Snapshot").config("hive.exec.dynamic.partition", "true").config("hive.exec.dynamic.partition.mode", "nonstrict").enableHiveSupport().getOrCreate()
x_df = spark.sql("SELECT count(*) as RC from bi_schema.table_a")

这将引发如下所示的错误

Hive Session ID = a00fe842-7099-4130-ada2-ee4ae75764be 
Traceback (mostrecent call last):   
File "<stdin>", line 1, in <module>   
File "/usr/hdp/current/spark2-client/python/pyspark/sql/session.py", line 716, in sql
return DataFrame(self._jsparkSession.sql(sqlQuery), self._wrapped)   
File "/usr/hdp/current/spark2-client/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py",line 1257, in __call__   
File "/usr/hdp/current/spark2-client/python/pyspark/sql/utils.py", line 63,
in deco return f(*a, **kw)   
File "/usr/hdp/current/spark2-client/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value py4j.protocol.Py4JJavaError: An error occurred while calling o70.sql. : java.lang.AssertionError: assertion
failed at scala.Predef$.assert(Predef.scala:156) at org.apache.spark.sql.hive.HiveMetastoreCatalog.convertToLogicalRelation(HiveMetastoreCatalog.scala:214)

当我在 hive 中运行相同的查询时,我得到的结果符合预期,如下所示。

+-------------+
|     rc      |
+-------------+
| 3037579538  |
+-------------+
1 row selected (25.469 seconds)

这是Spark中的错误,特定于ORC格式。

在 sparkContext 配置中设置以下属性将解决问题:

spark.conf.set("spark.sql.hive.convertMetastoreOrc", "false")

如果我们仔细研究HiveMetastoreCatalog的火花代码,那么似乎

assert(result.output.length == relation.output.length && result.output.zip(relation.output).forall { case (a1, a2) => a1.dataType == a2.dataType })正在失败。这意味着它正在检查列数和数据类型。一个原因可能是在更改表元存储未更新后,但这不太可能。

然后我想为相同的 JIRA 票证创建,但事实证明 ORC 格式总是有一些问题。关于这个问题,已经有两个 JIRA 票证:

  1. 火花-28098
  2. 火花-28099

如果我们spark.sql.hive.convertMetastoreOrc保持默认true,那么它将使用矢量化的阅读器官方文档。由于此错误,列数不匹配,断言失败。我怀疑此属性导致在使用矢量化阅读器时添加了一些虚拟列。

你能尝试一次以下步骤吗,因为我认为我们不能直接使用 HiveContext 查询 Hive 表

from pyspark.sql import HiveContext
hive_context = HiveContext(sc)
result= hive_context.table("bi_schema.table_a")

在以上述方式获取表后,我们需要将该结果数据帧注册为可临时的,如下所示

result.registerTempTable("table_a")

现在我们可以查询该表上的 select 语句,如下所示

x_df = hive_context.sql("SELECT count(*) as RC fromtable_a")

最新更新