向表中装载数据(Load)
load data local inpath '/opt/module/hive/student.txt' into table student(表名);
加local代表将linux本地文件加载到表中,不加则是将hdfs中的文件加载到表中(速度快,因为是对namenode的指向路径进行操作,不会对数据进行操作)
加overwrite代表将原表中的数据进行覆盖,不加则不会覆盖。
通过查询语句向表中插入数据(Insert)
student表的数据:
student1表的数据:
两个表中存有的数据是一样的,用insert语句将student1的数据传入到student表中。
insert语句也能用student1的数据去覆盖student的数据
student表中的数据被覆盖:
多表插入模式:
需要提前创建一个表student2
create table student2(id string, name string) row format delimited fields terminated by '\t';
查询语句中创建表并加载数据(As Select)
根据查询结果创建表(查询的结果会添加到新创建的表中)
可在student3前加上if not exists
创建表时通过Location指定加载数据路径
HDFS中的文件没存在默认的元数据中,但是在创建表的时候想将文件的数据存入到创建的表中就可以使用Location方法创建一个外部表
红框为文件在HDFS中的路径。