在Hive表上创建视图:每个变量的注释都将丢失

时间:2018-03-07 13:11:53

标签: view hive pyspark comments

我创建了一个Hive表,我们在每个变量的“comment”字段中添加了一些描述,如下所示:

spark.sql("create table test_comment (col string comment 'col comment') comment 'hello world table comment ' ")
spark.sql("describe test_comment").show()
+--------+---------+-----------+
|col_name|data_type|    comment|
+--------+---------+-----------+
|     col|   string|col comment|
+--------+---------+-----------+

一切都很好,我们在变量“col”的commennt字段中看到评论“col comment”。

现在,当我在此表上创建视图时,“comment”字段不会传播到视图,“comment”列为空:

spark.sql("""create view test_comment_view as select * from test_comment""")
spark.sql("describe test_comment_view")

+--------+---------+-------+
|col_name|data_type|comment|
+--------+---------+-------+
|     col|   string|   null|
+--------+---------+-------+

有没有办法在创建视图时保留注释字段的值?这个“功能”的原因是什么?

我正在使用:

Hadoop 2.6.0-cdh5.8.0

Hive 1.1.0-cdh5.8.0

Spark 2.1.0.cloudera1

1 个答案:

答案 0 :(得分:1)

我观察到的是,即使从另一个表创建表,也不会继承注释。看起来这是默认行为。

db-service

但有一个解决方法。创建视图时,您可以使用注释指定单个列

create table t1 like another_table 
desc t1  //includes comments
+-----------+------------+------------------+--+
| col_name  | data_type  |     comment      |
+-----------+------------+------------------+--+
| id        | int        | new employee id  |
| name      | string     | employee name    |
+-----------+------------+------------------+--+

create table t1 as select * from another_table
desc t1 //excludes comments
+-----------+------------+----------+--+
| col_name  | data_type  | comment  |
+-----------+------------+----------+--+
| id        | int        |          |
| name      | string     |          |
+-----------+------------+----------+--+