【问题标题】:Create Hive Table on partionned by year month day parquet在按年月日镶木地板分区上创建 Hive 表
【发布时间】:2020-12-24 12:58:29
【问题描述】:

我创建了一个这样的数据框:

val df = Seq(
  (1,27,"bob",2020,9,3),
  (1,27,"jack",2020,9,3),
  (3,31,"tom",2020,9,4)
).toDF("id","age","nom","year","month","day")

我得到以下数据框

+---+---+----+----+-----+---+
|id |age|nom |year|month|day|
+---+---+----+----+-----+---+
|1  |27 |bob |2020|9    |3  |
|1  |27 |jack|2020|9    |3  |
|3  |31 |tom |2020|9    |4  |
+---+---+----+----+-----+---+

然后我用 partitionBy 用年月日在 hdfs 上写 df;

df.write
  .mode(SaveMode.Append)
  .partitionBy("year", "month", "day")
  .parquet(s"$outputPath/test_hive")

我在以下 hdfs 路径上获取数据:

  • /outputPath/test_hive/year=2020/month=9/day=3
  • /outputPath/test_hive/year=2020/month=9/day=4

我想知道如何在位置创建外部配置单元表 outputPath/test_hive 可以考虑子目录年、月和日。

我尝试了以下创建表,但它不起作用:

CREATE EXTERNAL TABLE test1(id int, age int, nom string, year int, month int, day int) STORED AS PARQUET LOCATION 'outputPath/test_hive'

+-----------+------------+------------+--+
| test1.id  | test1.age  | test1.nom  |
+-----------+------------+------------+--+
| 1         | 27         | bob        |
| 1         | 27         | jack       |
| 3         | 31         | tom        |
+-----------+------------+------------+--+

和

CREATE EXTERNAL TABLE test2(id int, age int, nom string) PARTITIONED BY(year INT, month int , day INT) STORED AS PARQUET LOCATION 'outputPath/test_hive'

+-----------+------------+------------+-------------+--------------+------------+--+
| test2.id  | test2.age  | test2.nom  | test2.year  | test2.month  | test2.day  |
+-----------+------------+------------+-------------+--------------+------------+--+
+-----------+------------+------------+-------------+--------------+------------+--+

和

CREATE EXTERNAL TABLE test3(id int, age int, nom string) STORED AS PARQUET LOCATION 'outputPath/test_hive' PARTITIONED BY(year INT, month int , day INT);

Error while compiling statement: FAILED: ParseException line 1:138 missing EOF at 'PARTITIONED' near ''/outputPath/test_hive'' (state=42000,code=40000)

【问题讨论】:

  • 如何更改stored as 和partitioned by 第二个的顺序?
  • @Lamanus 我试着改变你提到的第三个我刚刚编辑的顺序,但我得到一个错误:编译语句时出错:失败:ParseException line 1:138 missing EOF at 'PARTITIONED' near ''/outputPath/test_hive'' (state=42000,code=40000)
  • 前两个发生了什么错误?
  • @Lamanus 我没有收到任何错误,但我没有预期的数据。我在上面的问题中添加了来自蜂巢的结果

标签: scala apache-spark hive hdfs


【解决方案1】:

msck repair table or add partitions 到桌边。

Example:

From Hive:

hive> msck repair table test3

--or

hive> ALTER TABLE test3 ADD PARTITION (year='2020', month='9',day='3') location '/outputPath/test_hive/year=2020/month=9/day=3';

From spark:

spark.sql("ALTER TABLE test3 ADD PARTITION (year='2020', month='9',day='3') location '/outputPath/test_hive/year=2020/month=9/day=3'")

//or

spark.sql("msck repair table test3")

【讨论】:

  • msck 修复表 test2 在每次更新 hdfs 数据后完成这项工作,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 2018-04-20
  • 2019-03-17
  • 2018-12-23
相关资源
最近更新 更多