目录
1 Hive数据导入
1.1 直接向表中插入数据(强烈不推荐使用)
1.2 通过load加载数据(必须掌握)
1.3 通过查询加载数据(必须掌握)
1.4 查询语句中创建表并加载数据(as select)
1.5 创建表时指定location
1.6 export导出与import 导入 hive表数据(内部表操作)
2. Hive数据导出
2.1 insert 导出
2.2 Hive Shell 命令导出
2.3 export导出到HDFS上
hive (myhive)> create table score3 like score; hive (myhive)> insert into table score3 partition(month ='201807') values ('001','002','100');
语法:
hive> load data [local] inpath 'dataPath' [overwrite] into table student [partition (partcol1=val1,…)];
通过load方式加载数据
hive (myhive)> load data local inpath '/kkb/install/hivedatas/score.csv' overwrite into table score partition(month='201806');
通过查询方式加载数据
语法;官网地址
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...) [IF NOT EXISTS]] select_statement1 FROM from_statement; INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement;
例子
hive (myhive)> create table score5 like score; hive (myhive)> insert overwrite table score5 partition(month = '201806') select s_id,c_id,s_score from score;
将查询的结果保存到一张表当中去
hive (myhive)> create table score6 as select * from score;
创建表,并指定在hdfs上的位置
hive (myhive)> create external table score7 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore7';
上传数据到hdfs上,我们也可以直接在hive客户端下面通过dfs命令来进行操作hdfs的数据
hive (myhive)> dfs -mkdir -p /myscore7; hive (myhive)> dfs -put /kkb/install/hivedatas/score.csv /myscore7;
查询数据
hive (myhive)> select * from score7;
hive (myhive)> create table teacher2 like teacher; -- 导出到hdfs路径 hive (myhive)> export table teacher to '/kkb/teacher'; hive (myhive)> import table teacher2 from '/kkb/teacher';
表 -> 文件
官方文档
语法
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 [ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive 0.11.0) SELECT ... FROM ...
将查询的结果导出到本地
insert overwrite local directory '/kkb/install/hivedatas/stu' select * from stu;
将查询的结果格式化导出到本地
insert overwrite local directory '/kkb/install/hivedatas/stu2' row format delimited fields terminated by ',' select * from stu;
将查询的结果导出到HDFS上==(没有local)==
insert overwrite directory '/kkb/hivedatas/stu' row format delimited fields terminated by ',' select * from stu;
基本语法:
hive -e "sql语句" > file
hive -f sql文件 > file
在linux命令行中,运行如下命令;导出myhive.stu表的数据到本地磁盘文件/kkb/install/hivedatas/student1.txt
hive -e 'select * from myhive.stu;' > /kkb/install/hivedatas/student1.txt
export table myhive.stu to '/kkb/install/hivedatas/stuexport';