【问题标题】:How to avoid AssertionError while executing a query over Hive table in ORC format from pyspark?从pyspark对ORC格式的Hive表执行查询时如何避免AssertionError?
【发布时间】:2019-12-31 19:25:46
【问题描述】:

我正在从 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)

【问题讨论】:

    标签: apache-spark hive pyspark pyspark-sql orc


    【解决方案1】:

    这是 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 }) 失败了。这意味着它正在检查列数和数据类型。一个原因可能是在 alter table Metastore 之后没有更新,但这不太可能。

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

    1. SPARK-28098
    2. SPARK-28099

    如果我们将spark.sql.hive.convertMetastoreOrc 保留为默认true,那么它将使用矢量化阅读器official doc。由于这个错误,列数不匹配并且断言失败。我怀疑这个属性会导致在使用矢量化阅读器时添加了一些虚拟列。

    【讨论】:

      【解决方案2】:

      您可以尝试以下步骤一次吗,因为我认为我们无法直接使用 HiveContext 查询配置单元表

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

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

      result.registerTempTable("table_a")
      

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

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

      【讨论】:

      • 我无法每次都注册我的表,因为我有 100 多个表动态出现。我使用的是 spark 会话,而不是 sparkcontext。这是我和你的答案的唯一区别
      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多