从 pyspark 以 ORC 格式对 Hive table 执行查询时如何避免 AssertionError?
How to avoid AssertionError while executing a query over Hive table in ORC format from pyspark?
我运行正在从 PySpark 执行一个简单的 Hive 查询,但它会引发错误。 table 是 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)
当我 运行 在配置单元中执行相同的查询时,我得到了预期的结果,如下所示。
+-------------+
| rc |
+-------------+
| 3037579538 |
+-------------+
1 row selected (25.469 seconds)
您能否尝试以下步骤一次,因为我认为我们无法使用 HiveContext
直接查询配置单元 table
from pyspark.sql import HiveContext
hive_context = HiveContext(sc)
result= hive_context.table("bi_schema.table_a")
以上述方式获取 table 后,我们需要将该结果数据帧注册为 temptable,如下所示
result.registerTempTable("table_a")
现在我们可以在 table 上查询 select 语句,如下所示
x_df = hive_context.sql("SELECT count(*) as RC fromtable_a")
这是 Spark 中的 Bug,特定于 ORC 格式。
在 sparkContext 配置中 属性 下的设置将解决问题:
spark.conf.set("spark.sql.hive.convertMetastoreOrc", "false")
如果我们仔细研究 HiveMetastoreCatalog 的 spark 代码,那么看起来
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 票证:
如果我们将 spark.sql.hive.convertMetastoreOrc
保持为默认值 true
,那么它将使用矢量化 reader official doc。由于此错误,列数不匹配并且断言失败。我怀疑这个 属性 导致在使用矢量化 reader 时添加了一些虚拟列。
我运行正在从 PySpark 执行一个简单的 Hive 查询,但它会引发错误。 table 是 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)
当我 运行 在配置单元中执行相同的查询时,我得到了预期的结果,如下所示。
+-------------+
| rc |
+-------------+
| 3037579538 |
+-------------+
1 row selected (25.469 seconds)
您能否尝试以下步骤一次,因为我认为我们无法使用 HiveContext
直接查询配置单元 tablefrom pyspark.sql import HiveContext
hive_context = HiveContext(sc)
result= hive_context.table("bi_schema.table_a")
以上述方式获取 table 后,我们需要将该结果数据帧注册为 temptable,如下所示
result.registerTempTable("table_a")
现在我们可以在 table 上查询 select 语句,如下所示
x_df = hive_context.sql("SELECT count(*) as RC fromtable_a")
这是 Spark 中的 Bug,特定于 ORC 格式。
在 sparkContext 配置中 属性 下的设置将解决问题:
spark.conf.set("spark.sql.hive.convertMetastoreOrc", "false")
如果我们仔细研究 HiveMetastoreCatalog 的 spark 代码,那么看起来
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 票证:
如果我们将 spark.sql.hive.convertMetastoreOrc
保持为默认值 true
,那么它将使用矢量化 reader official doc。由于此错误,列数不匹配并且断言失败。我怀疑这个 属性 导致在使用矢量化 reader 时添加了一些虚拟列。