


--建表
--table1: 员工表
CREATE TABLE employee(
id int,
name string,
deg string,
salary int,
dept string
) row format delimited
fields terminated by ',';
--table2:员工家庭住址信息表
CREATE TABLE employee_address (
id int,
hno string,
street string,
city string
) row format delimited
fields terminated by ',';
--table3:员工联系方式信息表
CREATE TABLE employee_connection (
id int,
phno string,
email string
) row format delimited
fields terminated by ',';
--加载数据到表中(先把数据放到/root/hivedata/中)
load data local inpath '/root/hivedata/employee.txt' into table employee;
load data local inpath '/root/hivedata/employee_address.txt' into table employee_address;
load data local inpath '/root/hivedata/employee_connection.txt' into table employee_connection;
--查看表
select *
from employee;
select *
from employee_address;
select *
from employee_connection;



select e.id,e.name,e_a.city,e_a.street
from employee e inner join employee_address e_a
on e.id =e_a.id;
--等价于 inner join=join
select e.id,e.name,e_a.city,e_a.street
from employee e join employee_address e_a
on e.id =e_a.id;
--等价于 隐式连接表示法
select e.id,e.name,e_a.city,e_a.street
from employee e , employee_address e_a
where e.id =e_a.id;`

select e.id,e.name,e_conn.phno,e_conn.email
from employee e left join employee_connection e_conn
on e.id =e_conn.id;
--等价于 left outer join
select e.id,e.name,e_conn.phno,e_conn.email
from employee e left outer join employee_connection e_conn
on e.id =e_conn.id;

show functions查看当下可用的所有函数;也可以通过describe function extended funcname来查看函数的使用方式。

Hive的函数分为两大类:内置函数(Built-in Functions)、用户定义函数UDF(User-Defined Functions)
UDF分类标准
UDF分类标准本来针对的是用户自己编写开发实现的函数。UDF分类标准可以扩大到Hive的所有函数中:包括内置函数和用户自定义函数。
select length("hello");

select reverse("hello");

select concat("hello","world");

select concat_ws('.', 'www', array('youtube', 'com'));

select substr("helloworld",-2); --pos是从1开始的索引,如果为负数则是倒过来数
select substr("helloworld",2,2);


--split针对字符串数据进行切割 返回是数组array 可以通过数组的下标取内部的元素 注意下标从0开始的
select split('apache hive', ' ');
select split('apache hive', ' ')[0];
select split('apache hive', ' ')[1];



select current_date();

select unix_timestamp();

select unix_timestamp("2022-02-22 22:00:22");
-- 指定格式转换
select unix_timestamp('20220222 22:00:02','yyyyMMdd HH:mm:ss');

select from_unixtime(1234567890);
select from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');


select datediff('2022-08-08','2012-12-21');

select date_add('2022-01-30',10);

select date_sub('2022-03-06',10);

select round(3.1415926);
#输出
3
select round(3.1415926,4);
#输出
3.1416
select rand();
#输出
0.7184552121506158
select rand(3);
#输出
0.731057369148862
select if(1=2,100,200);
#输出
200
select * from student limit 3;
select if(sex ='男','M','W') from student limit 3;


select nvl("cauchy","win");
#输出cauchy
select nvl(null,"win");
#输出win
select case sex when '男' then 'male' else 'female' end from student limit 3;
