使用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
## 欢迎交流