지구정복

[Hive] HiveQL에서 auto increment 사용하기 본문

데이터 엔지니어링 정복/Hive

[Hive] HiveQL에서 auto increment 사용하기

eeaarrtthh 2021. 6. 6. 15:26
728x90
반응형
SMALL

내 환경

하둡 3.3.0

하이브 3.1.2

 

먼저 자신의 하이브 디렉터리 lib에서 hive-contrib-3.1.2.jar를 찾는다. 버전은 다를 수 있다.

cd hive/lib/
ll hive-contrip*

 

그리고 hive를 실행해서 아래 명령어로 위 라이브러리를 하이브에 추가시키고 함수를 만들어준다.

#라이브러리 추가
hive>add jar /home/hadoop01/hive/lib/hive-contrib-3.1.2.jar;

#auto increment 함수 생성
hive>create temporary function row_sequence as 'org.apache.hadoop.hive.contrib.udf.UDFRowSequence';

 

그리고 auto increment를 진행할 테이블을 만든다.

나는 아래와 같다. 아래 컬럼 중에서 no가 auto increment가 들어갈 컬럼명이다.

drop table wordCnt;

create table if not exists wordCnt(
no int,
content string,
mor string,
cnt int
)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;

 

그리고 위 테이블에 overwrite할 테이블을 준비한다.

나의 경우 overwrite 테이블은 temp2이고 해당 컬럼은 content, mor, cnt로 구성되어 있다.

hive> desc temp2;
content             	string              	                    
mor                 	string              	                    
cnt                 	int  

 

그리고 아래처럼 temp2 테이블과 row_sequence() 함수를 이용해서 auto increment를 적용시켜서 insert해준다.

insert overwrite table wordCnt
select row_sequence(), content, mor, cnt
from temp2;

 

그러면 아래처럼 결과가 출력된다!

hive> select  * from wordCnt limit 30;
OK
1	브라운	Noun	277
2	야드	Noun	275
3	사이즈	Noun	163
4	구매	Noun	146
5	입니다	Adjective	107
6	에센셜	Noun	88
7	제품	Noun	75
8	것	Noun	74
9	유스	Noun	63
10	잘	Verb	59
11	분	Noun	57
12	거	Noun	52
13	요	Noun	50
14	재킷	Noun	49
15	데님	Noun	48
16	숏	Noun	48
17	너무	Adverb	43
18	했습니다	Verb	42
19	이번	Noun	41
20	입고	Verb	40
21	더	Noun	39
22	많이	Adverb	38
23	중	Noun	38
24	생각	Noun	37
25	합니다	Verb	37
26	시즌	Noun	36
27	다	Adverb	35
28	셔츠	Noun	34
29	후기	Noun	33
30	저	Noun	32

 

 

 

728x90
반응형
LIST
Comments