使用hive時,建立數據庫,建表,寫數據;
讀數據:select * from test_t2;
報錯SemanticException
原因:建表時使用了其他路徑,或者在另一個路徑的數據庫(建立數據庫時指定了location參數:create database words_db location 'hdfs://tmaster:8020/user/root/words.db')中建表test_t2,也就是因為在建表時沒有在默認路徑下建立,默認路徑是:/user/hive/warehouse/databasename.db/tablename/partition_name (注意要建立分區)
解決方法:
1. 方法一:重新在默認路徑下建表,也就是不顯式指定location參數,直接默認即可;
例如:建立外部分區表(指定location參數)--- (create table ...)
CREATE EXTERNAL TABLE IF NOT EXISTS test_t1( column_1 string ,column_2 string ,column_3 string )PARTITIONED BY (day_time string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION '/user/root/test';
默認建表(不指定location參數):
create table test_t1(column_1 string,column_2 string) partitioned by (day_time string) row format delimited fields terminated by '\1';
其中:row format delimited fields terminated by '\1' 表示行內的列分隔符是'\1'。
2. 方法二:修改設置參數location,參考:https://blog.csdn.net/ggwxk1990/article/details/78067803
建立分區:當設置partitioned by (day_time string)時,表示以day_time來進行分區 --- (alter table ...)
alter table test_t1 add IF NOT EXISTS partition (day_time='20191031'); #或者 (同樣可加也可不加:IF NOT EXISTS) alter table database_name.test_t1 add IF NOT EXISTS partition (day_time='20191031')
一般的流程:建表--->建立分區--->寫數據
注意:建數據庫和建表時,均有location參數
參考:
https://blog.csdn.net/Thomson617/article/details/86153924
https://blog.csdn.net/qq_36743482/article/details/78383964
## 歡迎交流
