原文:

https://www.toutiao.com/i6769166601871688196/?group_id=6769166601871688196

数据的导入

  1. load data [local]

创建数据表

加载数据

load data local inpath '/data/hivetest/stu_info_two' into table stu_info;

加载HDFS数据,移动数据文件到表对应的目录

我们先清空数据truncate table stu_info;

将数据加载到HDFS上

hdfs dfs -put /data/hivetest/stu_info_local_format /hivetestdata/

Hive中查看下

然后加载数据

  1. load data + overwrite 覆盖数据

为了区分我们新创建数据库

可以覆盖数据

3、子查询 as select

create table tb_stu_as_test_stu as select * from stu_info;

适合数据查询结果的保存

4、insert 方式

插入数据的表必须要存在

我们创建新表

我们如果再执行一遍,数据就会增多(追加)

执行覆盖(原先数据变了)

在关系型数据库插入一条数据

insert into table table_name(id,name) values(1,'test');

Hive也支持插入

insert into table hive_table_name(id,name) values(1,'test');

注意:这种方式适合数据非常小的情况下去使用,如果大数据量,避免这种操作,如果执行show tables会发现一个临时表values__tmp__table__1,说明SQL这种方式用在Hive中会用临时表过渡。

5、建表的时候用location指定数据文件的方式

数据的导出

官网

  1. INSERT OVERWRITE [LOCAL] DIRECTORY directory1 方式(local的方式和插入一样一个是本地一个是HDFS)

Insert overwrite local directory '/data/hivetest/export_local_stu_info' select * from stu_info;

我们查看数据

添加语句可以指定分隔符 ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '

语句:

Insert overwrite local directory '/data/hivetest/export_local_stu_info' ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' select * from stu_info;

数据有空格了

如果不使用local的话就是将输入导入到HDFS中,就可以用hdfs命令下载文件了

  1. bin/hive -e 或者 -f + >> 或者 >

hive -e 'select * from db_import_export.stu_info' >> /data/hivetest/export_local_stu_info_1.txt;

  1. 可以使用sqoop等工具

我们使用之前的数据库

select * from emp where sal > 3000;

select * from emp limit 5;

select distinct deptno from emp;

select * from emp where sal between 1300 and 3000;

select * from emp where sal >=1000 and sal <= 3000;

select empno,ename from emp where comm is null;

select empno,ename from emp where comm is not null;

聚合函数

count()、max()、min()、sum()、avg()、group by

如果我们加一个字段

select deptno,max(sal) from emp ;

有错误

select中出现的字段,需要用聚合函数包裹或者放入group by当中

select deptno ,max(sal) from emp group by deptno;

select max(deptno) ,max(sal) from emp ;

join

left join 、right join、inner join(等值)、full join(全)

新创建一个库

创建两张表

我们准备两个数据

等值join

select a_test.id,a_test.name,b_test.id,b_test.adress from a a_test join b b_test on a_test.id=b_test.id;

左join,以左表为基准,没有匹配到的字段为NULL

select a_test.id,a_test.name,b_test.id,b_test.adress from a a_test left join b b_test on a_test.id=b_test.id;

右join,以右表为基准,没有匹配到的字段为NULL

select a_test.id,a_test.name,b_test.id,b_test.adress from a a_test right join b b_test on a_test.id=b_test.id;

全join,所有的字段都会出现,没有匹配到的字段为NULL

select a_test.id,a_test.name,b_test.id,b_test.adress from a a_test full join b b_test on a_test.id=b_test.id;

不写连接条件,两张表做笛卡尔积

select a_test.id,a_test.name,b_test.id,b_test.adress from a a_test join b b_test ;

最新文章

  1. 百度地图api的覆盖物样式与bootstrap样式冲突解决办法
  2. linux用户管理(二)
  3. Kylin Java RESTful API
  4. 傅盛:如何快慢&ldquo;炼&rdquo;金山?(转)
  5. Theano2.1.10-基础知识之循环
  6. echo 和 cat 的 区别
  7. hud 1785 畅通工程
  8. Undefined index:
  9. susy-Toolkit 之翻译
  10. C#异步编程 z
  11. http怎样保持有状态?
  12. (转)DEDECMS模板原理、模板标签学习 - .Little Hann
  13. 信息安全实验四:information-security
  14. centos 添加用户
  15. Oracle中中文、数字,英文混杂形式的字段进行排序的方法
  16. AsyncTask 不能与Thread.sleep()同时使用解决方案
  17. 基于Jquery UI的autocompelet改写,自动补全控件,增加下拉选项,动态设置样式,点击显示所有选项,并兼容ie6+
  18. ArcGIS API for JavaScript 4.x 本地部署之Apache(含Apache官方下载方法)
  19. webpack-dev-server live reloading 技术实现
  20. 【JVM】-NO.111.JVM.1 -【JDK11 HashMap详解-1-hash()剖析】

热门文章

  1. C/C++ Qt 数据库与Chart历史数据展示
  2. MicroK8S 安装 修改IP 设置镜像加速 升级 卸载等
  3. MySQL查询数据库表空间大小
  4. Android: Client-Server communication
  5. SROP例题
  6. 十年后回到百年前?(Excel技巧集团)
  7. .NET 云原生架构师训练营(建立系统观)--学习笔记
  8. Kubernetes-API Server
  9. 音视频中的PTS和DTS及同步
  10. [react]react创建app,路由,mobx 全教程