默认路径 /opt/soft/hive312/warehouse
可以使用hdfs dfs -put 上传
Load data [local] inpath 'filepath' [overwrite] into table tablename;
将在本地文件系统中查找文件路径
若指定相对路径,将相对于用户的当前工作目录进行解释
用户也可以为本地文件指定完整的URI-----例如:file://opt/file.txt
如果filepath指向的是一个完整的URI,会直接使用这个URI;
如果没有指定数据库,Hive会使用在hadoop配置文件中参数fs.default.name指定的
create table if not exists employee(
name string,
workplace array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,string>
)
row format delimited fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
# 本地加载(本质是hadoop dfs -put 上传操作)复制
load data local inpath '/opt/stufile/emp.txt' into table employee;
# 从HDFS加载 (本质是hadoop fs -mv 操作)移动
load data inpath "hdfspath" into table employee;
Hive官方推荐加载数据的方式
也可以使用insert语法把数据插入到指定的表中(应为insert操作底层走MapReduce操作,效率很低)
最常用的配合是把查询返回的结果插入到另一张表中(insert+select)。
insert into table table_name select statement from table2_name
注意:查询返回的字段必须和插入表字段一致
SELECT[ALL丨DISTINCT] select—expr,select—expr,....
FROM table_reference
[WHERE where—condition]
[GROUP BY col_list]
[ORDER BY col_list]
[LIMT[offset,]rows];
SELECT currernt_database();----查询当前数据库
create table employee2(
name string,
work_place array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,string>
)
partitioned by (age int)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
partitioned by (age int) 含义是:创建分区 以age分区
0: jdbc:hive2://192.168.95.150:10000> load data local inpath '/opt/employee.txt' into table employee2 partition(age=20);
0: jdbc:hive2://192.168.95.150:10000> load data local inpath '/opt/employee.txt' into table employee2 partition(age=30);
show partitions employee2;
create table employee3(
name string,
work_place array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,string>
)
partitioned by (age int , gender string)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
0: jdbc:hive2://192.168.95.150:10000> load data local inpath '/opt/employee.txt' into table employee3 partition(age=20,gender='0');
0: jdbc:hive2://192.168.95.150:10000> load data local inpath '/opt/employee.txt' into table employee3 partition(age=20,gender='1');
数据表分为内部表和外部表
HDFS中为所属数据库目录下的子文件夹
数据完全由Hive管理,删除表(元数据)会删除数据
数据保存在指定位置的HDFS路径中
Hive不完全管理数据,删除表(元数据)不会删除数据
create external table if not exists employee(
name string,
work_place array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,string>
)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n
location '/tmp/hivedata/employee';
注意:创建外部表要在create后面加上一个 external