ApacheSpark—从配置单元查询时,分区表中的数据不会显示

cld4siwp  于 2021-06-26  发布在  Hive
关注(0)|答案(4)|浏览(342)

注意这个问题不是这个问题的重复!我不使用sparksql进行分区!我正在保存个人Parquet文件!
我还使用不支持Hive风格的sql的databricks。
我在hive中有一个表(我正在使用databricks),它包含两个分区。看起来是这样的:

CREATE TABLE foo_test (`col0` STRING, `col1` STRING, `col2` STRING, `datestamp` STRING)
USING parquet
OPTIONS (
  `serialization.format` '1'
)
PARTITIONED BY (datestamp)

编辑:*这也是调用 show create table foo_test; 我已经手动在这个表中添加了两个带spark的分区:

df = spark.read.csv(file_path.format(datestamp), header=True, inferSchema=True)

partitionPath = "/mnt/foo_test/datestamp={}/".format(datestamp)

df.coalesce(1).write.parquet(partitionPath, mode="overwrite")

/mnt/foo_test/datestamp=20180101/
/mnt/foo_test/datestamp=20180102/

如果我用spark加载数据,我可以看到它在那里:

spark.read.option("mergeSchema", True).parquet("/mnt/foo_test").show()

+----+----+----+----+---------+
|col0|col1|col2|col3|datestamp|
+----+----+----+----+---------+
| foo| bar| baz|   1| 20180102|
| xul| qux| wom|   2| 20180102|
| bar| foo| baz|   1| 20180102|
| qux| xul| wom|   2| 20180102|
| foo| bar| baz|null| 20180101|
| xul| qux| wom|null| 20180101|
| bar| foo| baz|null| 20180101|
| qux| xul| wom|null| 20180101|
+----+----+----+----+---------+

我的问题是,如果对这个配置单元表运行sql查询,它不会返回任何结果:

SELECT * FROM foo_test;

OK

即使手动添加分区:

spark.sql("ALTER TABLE foo_test ADD IF NOT EXISTS PARTITION (datestamp=20180102)")

修理table:

MSCK REPAIR TABLE foo_test;

我可以看到分区是根据配置单元显示的:

SHOW PARTITIONS foo_test;

partition
datestamp=20180102
datestamp=20180101

但是 SELECT 不返回任何内容。
这是我的表的描述:

col0    string  null
col1    string  null
col2    string  null
datestamp   string  null

# Partition Information

# col_name  data_type   comment

datestamp   string  null

# Detailed Table Information

Database    default 
Table   foo_test    
Owner   root    
Created Thu Apr 26 12:25:06 UTC 2018    
Last Access Thu Jan 01 00:00:00 UTC 1970    
Type    MANAGED 
Provider    parquet 
Table Properties    [transient_lastDdlTime=1524745506]  
Location    dbfs:/user/hive/warehouse/foo_test  
Serde Library   org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe 
InputFormat org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat   
OutputFormat    org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat  
Storage Properties  [serialization.format=1]    
Partition Provider  Catalog

有什么问题吗?

sauutmhj

sauutmhj1#

您没有在中设置位置 CREATE TABLE 定义,并且您没有设置新添加分区的位置。
在表定义中,应该将其定义为外部表,并为其提供指向数据的路径,或者作为 LOCATION 或者 PATH 参数。那么 MSCK REPAIR TABLE 应该正确添加分区。
对于 ALTER TABLE 命令,你需要设置 LOCATION 参数。在这个片段中,您只是告诉表“thereapartitiondate=20180102”,而不告诉它数据在哪里。
每https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#languagemanualddl-添加分区 spark.sql("ALTER TABLE foo_test ADD IF NOT EXISTS PARTITION (datestamp=20180102) location '/mnt/foo_test/datestamp=20180102/' ")

ua4mk5z4

ua4mk5z42#

在配置单元中创建表时,请使用:

CREATE TABLE foo_test (`col0` STRING, `col1` STRING, `col2` STRING)
PARTITIONED BY (`datestamp` STRING)
stored as PARQUET
location '/mnt/foo_test';

您可以使用spark write with partition:

df.repartition($"datestamp").partitionBy("datestamp").write.parquet(partitionPath, mode="overwrite")

这将以分区路径/mnt/foo\u test/datestamp=***/写入配置单元表。
我希望这会有帮助

k7fdbhmy

k7fdbhmy3#

表定义指向不同的位置

Location    dbfs:/user/hive/warehouse/foo_test

它应该指向以下位置 /mnt/foo_test 查找具有位置的配置单元创建表

cgfeq70w

cgfeq70w4#

我不使用sparksql进行分区!
我不同意。这不是配置单元表定义:

CREATE TABLE foo_test (`col0` STRING, `col1` STRING, `col2` STRING, `datestamp` STRING)
USING parquet
OPTIONS (
  `serialization.format` '1'
)
PARTITIONED BY (datestamp)

这是一个Spark表定义。
配置单元表定义为:

CREATE TABLE foo_test (`col0` STRING, `col1` STRING, `col2` STRING)
PARTITIONED BY (`datestamp` STRING)
STORED AS PARQUET

所以您确实使用了spark分区,正如您在链接的问题中已经解释的,在链接的jira票证中,spark和hive分区方案是不兼容的。。
请注意,中同时支持hive和spark SparkSession.sql 只要已启用配置单元支持,当 SparkSession 已初始化(databricks平台上的默认设置)。
也不清楚你为什么写信给我 /mnt/foo_test/datestamp={} 在这里,这可能是另一个问题的根源。如果您想使用本地文件api(为什么要这么做?),databricks会在默认情况下在 /dbfs .
既然你打电话来了 ADD PARTITIONS 如果没有位置,它将使用表的根路径( dbfs:/user/hive/warehouse/foo_test 基于 DESCRIBE 输出),所以如果您决定使用本地api,并使用默认配置,您应该写入

/dbfs/user/hive/warehouse/foo_test/datestamp=20180102

如果您使用非标准配置,如果在您的问题中包含它,那将是非常好的。

相关问题