Hive SQL 练习

点击有惊喜


首先将练习用的两张表 上传到hdfs上
命令如下:
hadoop fs -put /opt/dep.txt /dep.txt
hadoop fs -put /opt/employee.txt /employee.txt

然后开始写练习

创建数据库
create database bs17;
use bs17;
创建员工信息表
create table employee(
emp_id string
,emp_name string
,status string
,salary string
,status_salary string
,in_work_date string
,leader_id string
,dep_id string
)row format delimited
fields terminated by '\t'

row format delimited fileds terminated by '\t' 表示将传进来的文件按照空格分割 写入到表格中

闯入文件 employee.txt
load data inpath '/employee.txt' overwrite into table employee;

查询表 
select * from employee;

同理创建第二个表
create table department(
dep_id string
,dep_name string
,address string
)row format delimited 
fields terminated by '\t'
传入文件 以空格分割写入到表格中
load data inpath '/dep.txt'overwrite into table department;
查询表
select * from department
之前的创建表 都是采用string字段 这样做的原因是防止传入的数据损失精度失真 以原文件的格式类型为准 txt 通用 string
接下来通过 as 来更改表结构 将字段改为所需要的
create table dw_employee as select 
cast(emp_id as int) emp_id
,emp_name
,status
,cast(salary as double)salary
,cast(status_salary as double)status_salary
,cast(in_work_date as date)in_work_date
,cast(leader_id as int)leader_id
,cast(dep_id as int)dep_id
from employee

dw_employee 为创建好的更改好字段的表
select * from dw_employee
查询表结构语句
describe formatted dw_employee
同理更改第二张表的表结构
create table dw_department as
select cast(dep_id as int)dep_id
,dep_name
,address
from department

select * from dw_department
除了 通过 as 复制表 之外 还可以通过 like 来克隆 不过 like 克隆的只有表结构 没有数据
create table employee_clone like employee;
select * from employee_clone

查找employee 表中的 id name 月薪 季薪 年薪
select emp_id
,emp_name
,salary mon_salary
,salary*3 season_salary
,salary*12 year_salary
from dw_employee
查找员工的月工资 月薪加奖金
select emp_id
,emp_name
,salary+status_salary getMoney
from dw_employee
插入数据 id name 其他为null
insert into dw_employee(emp_id,emp_name) values(1111,'aaaa')
nvl 表示条件 如果有字段信息 则表示字段信息 如果为null 则用后的表示
如有职位 则表示 没有则用普通员工代替 
如果有入职日期 则表示 没有 则用2015-5-1表示
select emp_id
,emp_name
,nvl(status,'普通员工') job
,nvl(in_work_date,'2015-5-1') in_work_date
from dw_employee

复制员工表,表名为emp_copy
create table emp_copy as select * from dw_employee
机构中有多少种职位(就是将所有的status 聚合展示出来 展示出来几种就有几种)
select distinct status from dw_employee

薪水高于6000的员工

select * from dw_employee where salary>6000

职位是analyst 的员工

select * from dw_employee where status ='analyst'

以小写格式展示职位信息(lower())
select emp_id
,emp_name
,lower(status)
from dw_employee
忽略大小写匹配职位等于‘ANALYST’的记录
select * from dw_employee where upper(status)='ANALYST'

查询出薪水在5000到8000之间的员工(between and)
select * from dw_employee where salary between 5000 and 8000;
查询出2016年入职的员工
select * from dw_employee where year(in_work_date)=2016

薪水不在5000到8000的员工
select * from dw_employee where salary not between 5000 and 8000;

查询出职位是Manager或者analyst的员工
select * from dw_employee where status in(Manager,analyst);

模糊查询like % 
查询出没有岗位工资的员工
select * from dw_employee where status_salary is null;

查询有岗位工资的员工
select * from dw_employee where status_salary is not null;

查询出不在10部门和不再30部门的员工
select * from where dw_employee where dep_id not in(10,30)


点击有惊喜


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下 如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值