하이브 인스턴스 작업

Hive 운영 데모

usr에서 mkdir / wang

hadoop fs -mkdir /upload  
hadoop fs -chmod g+w /upload   

데이터 업로드

[root@master wang]# hadoop fs -put emp.csv /upload
[root@master wang]# hadoop fs -put dept.csv /upload

서버 : hiveserver2 &

고객:

익명으로 로그인

beeline -u jdbc : hive2 : // master : 10000 / default

**** 루트로 로그인하면 루트 사용자로 로그인합니다. 그렇지 않으면 쓰기 권한이 없습니다.

beeline -u jdbc : hive2 : // master : 10000 / default -n 루트

직원 테이블을 만들고 테이블을 만드는 문을 사용하여 입력 파일 구분 기호를 지정한 다음이 테이블에 데이터를로드합니다.

create table emp001(empno int,ename string,job string,mgr int,hiredate string,sal int,comm int,deptno int) row format delimited fields terminated by ',';   # 字段分割用逗号

부서 테이블 생성

create table dept001(deptno int,dname string,loc string) row format delimited fields terminated by ',';

데이터 가져 오기

load data inpath '/upload/emp.csv' into table emp001;
load data inpath '/upload/dept.csv' into table dept001;

직원의 부서 번호를 기반으로 파티션 생성

create table emp_part001(empno int,ename string,job string,mgr int,hiredate string,sal int,comm int) partitioned by (deptno int) row format delimited fields terminated by ',';

파티션 테이블에 데이터 삽입 : 가져온 데이터의 파티션 지정 (하위 쿼리를 통해 데이터 가져 오기)

insert into table emp_part001 partition(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm from emp001 where deptno=10;
insert into table emp_part001 partition(deptno=20) select empno,ename,job,mgr,hiredate,sal,comm from emp001 where deptno=20;
insert into table emp_part001 partition(deptno=30) select empno,ename,job,mgr,hiredate,sal,comm from emp001 where deptno=30;

버킷 테이블을 생성하고 직원의 직무 (직무)에 따라 버킷을 나눕니다.

create table emp_bucket001 (empno int,ename string,job string,mgr int,hiredate string,sal int,comm int,deptno int) clustered by (job) into 4 buckets row format delimited fields terminated by ',';

하위 쿼리를 통해 데이터 삽입

insert into emp_bucket001 select * from emp001;

사원 정보 조회 : 사원 번호, 이름, 급여

select empno,ename,sal from emp001;

다중 테이블 조인 쿼리

select dept001.dname,emp001.ename from emp001,dept001 where emp001.deptno=dept001.deptno;

직원 수

select count(empno) as emp_num from emp001;

중복 값 제거

select distinct deptno from emp001;

회사의 직위 수를 쿼리하십시오.

select count(distinct job) from emp001;

1981 년에 고용 된 총 직원 수를 세십시오.

select count(hiredate) as result from emp001 where hiredate like '%1981%';

학과별 총 급여 통계

select deptno,sum(sal) from emp001 group by deptno;

각 직책에 몇 명의 직원이 있는지 계산

select job, count(*) as emp_num from emp001 group by job order by emp_num asc;

가장 이른 직원 쿼리

select ename,hiredate from emp001
join
(select min(hiredate) as min_hiredate from emp001) t1
where hiredate=t1.min_hiredate;

급여 수준 판단

select ename,empno,sal,
case when sal<2000 then 'low' when sal >=2000 
and sal <3000 then 'middle' 
else 'high' 
end as level 
from emp001 
order by sal desc; 

직급에 따라 직원에게 급여를 인상하고 인상 전후 급여를 표시합니다.

select empno,ename,job,sal,
case job when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+800 
else sal+400
end
from emp001;

상반기 직원 수가 가장 많은 지역 통계

# cast用于转换数据类型
# substr用于截取字符串
select t1.loc,count(*)as emp_count
from 
(select dept001.loc,emp001.ename,
cast(substr(emp001.hiredate,6,2) as int) as hire_month
from dept001 join emp001 
on dept001.deptno=emp001.deptno) t1
where t1.hire_month<=6
group by t1.loc
order by emp_count desc
limit 1;

추천

출처blog.csdn.net/qq_46009608/article/details/112796151