hive数据操作

时间:2023-03-08 22:40:01
hive数据操作

mdl是数据操作类的语言,包括向数据表加载文件,写查询结果等操作

hive有四种导入数据的方式
》从本地加载数据 LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE [tableName];
》从hdfs上加载数据 load data inpath 'hdfs://192.168.177.124:9000/opt/hive/warehouse/student.txt'
》从其他表中查出数据并加载到其他表中 insert overwrite table student_1 partition(ds='',country ='B eiJing') select studentid,sex,address,email from student_3 where ad dress='BeiJing';
》创建表的时候从其他表中查出数据
向表中加载文件
当数据加载到表里的时候不会对数据进行任何转换,只是单纯的复制/移动 到hive表对应的位置
 从本地文件加载到表
hive> LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE [tableName];
local:表示从本地加载文件(想从hdfs中加载文件,则不适用locall)
inPath:要加载文件的位置(可以是绝对路径或者相对路径,相对路径会从默认当前用户下的相对路径)
overwrite:覆盖表中的数据,如果没有overwrite则会在表中数据的后面追加数据
tableName:要加载的表名
加载的目标可以是一个表或者是一个分区,如果表中含有分区的话则必须指定每个分区的分区名。
可以应用一个文件(hive会将文件移动到表所对应的目录中)或者一个目录
向分区表中添加数据(将查询的结果写入文件系统)
(1)通常情况下需要预先创建好该分区,才能使用
alter table student_1 add partition (ds='',country ='BeiJing');
hdfs上会创建给相应的文件目录
/opt/hive/warehouse/student_1/ds=20161219/country=BeiJing


(2)插入sql(可以向表/分区中写入多条数据写入多条数据)
hive> insert overwrite table student_1 partition(ds='20161219',country ='BeiJing') select studentid,sex,address,email from student_3 where address='BeiJing';
hive数据操作
插入成功
hive数据操作
从hdfs上导出数据到hive表中
load data inpath 'hdfs://192.168.177.124:9000/opt/hive/warehouse/student.txt'
into table tableName
partition (address='BeiJing');
可以使用正则表达式
select '(name|age)?+' form [tableName]
基于分区查询
select 是对全表进行扫描,如果创建表中有parttion by语句,可以对查询的语句进行剪枝,hive现在只对在where分区中的字段进行扫描
select*from tableName where address='BeiJing'
hive目前不支持having,可以子查询来完成
hive有很多语法都跟sql类似,比如group by ,join,having,多表insert,streaming
删除表数据
truncate table tableName;
删除hive中的表
drop table tableName;
删除表中的特定行
delete from tableName t where t....
hive执行文件中的sql(Hive可以运行保存在文件里面的一条或多条的语句,只要用-f参数)
bin/hive -f [文件名] //这种是没有进入hive命令行语法

一般文件的名称后缀是'.hsql 或者.q'

source [文件名]; 例:source /home/wyp/Documents/test;//进入hive命令行后的语法