使用hive時,創建數據庫,建表,寫數據;數據庫
讀數據:select * from test_t2;spa
報錯SemanticException.net
緣由:建表時使用了其餘路徑,或者在另外一個路徑的數據庫(創建數據庫時指定了location參數:create database words_db location 'hdfs://tmaster:8020/user/root/words.db')中建表test_t2,也就是由於在建表時沒有在默認路徑下創建,默認路徑是:/user/hive/warehouse/databasename.db/tablename/partition_name (注意要創建分區)code
解決方法:orm
1. 方法一:從新在默認路徑下建表,也就是不顯式指定location參數,直接默認便可;blog
例如:創建外部分區表(指定location參數)--- (create table ...)get
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參數):string
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'。it
2. 方法二:修改設置參數location,參考:https://blog.csdn.net/ggwxk1990/article/details/78067803io
創建分區:當設置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參數
參考:
http://www.javashuo.com/article/p-ugrogdxr-dg.html
http://www.javashuo.com/article/p-yzwpriag-kz.html
## 歡迎交流