Spark 不显示驻留在配置单元中的数据 table

Spark does not show data residing in a hive table

假设我使用 Spark 创建以下 table:

df = spark.createDataFrame([(1, 4), (2, 5), (3, 6)], ["A", "B"])
df.write.mode("overwrite").saveAsTable("hivedb.mwe")

现在,如果我尝试计算此 table 中的数据:

> spark.sql("SELECT count(*) FROM hivedb.mwe").show()

+--------+
|count(1)|
+--------+
|       0|
+--------+

但是,如果我使用 Hive(或 Impala 计算数据,结果相同)

jdbc:hive2:...> SELECT count(*) FROM hivedb.mwe

+------+--+
| _c0  |
+------+--+
| 3    |
+------+--+

这里可能发生了什么,spark 似乎没有看到 mwe 中的数据?

作为插件,Spark 非常了解 table:

> spark.sql("DESCRIBE hivedb.mwe").show()

+--------+---------+-------+
|col_name|data_type|comment|
+--------+---------+-------+
|       A|   bigint|   null|
|       B|   bigint|   null|
+--------+---------+-------+

为了完整起见:

这似乎是 cloudera 2.2 中的一个已知问题。

https://www.cloudera.com/documentation/spark2/latest/topics/spark2_known_issues.html#SPARK-21994

已提供最佳替代解决方案,您能否查看以上内容 link 并执行解决方案并查看可行的解决方案。

这些是解决方案

val options = Map("path" -> "/path/to/hdfs/directory/containing/table")
df.write.options(options).saveAsTable("db_name.table_name")

spark.sql("alter table db_name.table_name set SERDEPROPERTIES ('path'='hdfs://host.example.com:8020/warehouse/path/db_name.db/table_name')")
spark.catalog.refreshTable("db_name.table_name")