데이터 이전 후 설명 없는 NullPointerException
ERROR TaskSetManager: Task 0 in stage 5.0 failed 4 times; aborting job
org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 5.0 failed 4 times, most recent failure: Lost task 0.3 in stage 5.0 : java.lang.NullPointerException
at org.apache.spark.sql.execution.datasources.orc.OrcColumnVector.getUTF8String(OrcColumnVector.java:167)
...
데이터 이전을 잘 하고 나머지 집계도 잘 동작하는지 확인하기 위해 검증하는 도중 원인 설명이 제대로 되지 않은 문제가 발생했다.
원인
이전 이전의 Table에 필요한 columns을 추가하였고, 생성문은 관련된 columns끼리 묶어 관리하기 위해 순서를 변경함
그로 인하여 기존 테이블 스키마 순서와 이전할 테이블의 순서가 다르기 때문에 문제가 발생함.
간단 설명
아래와 같은 테이블이 존재했다 가정하고 시작해보자
id |
passwd |
name |
id1 |
passwd1 |
nane1 |
id2 |
passwd2 |
nane2 |
id3 |
passwd3 |
nane3 |
email칼럼을 추가하기위해 데이터를 추가하였다.
id |
passwd |
name |
email |
id1 |
passwd1 |
nane1 |
email1 |
id2 |
passwd2 |
nane2 |
email2 |
id3 |
passwd3 |
nane3 |
email3 |
이전한 테이블은 id,passwd,email,name
순으로 테이블 스키마를 정의했다.
create external table if not exists db명.tb명 ( id string , passwd string , email string , name string ) ;
복제할때 기존 테이블의 스키마 순서인 id,passwd,name,email
가아닌 id,passwd,email,name
로 스키마 순서가 바뀌어 데어터 순서가 뒤섞여 복제가 되었다.
id |
passwd |
email |
name |
id1 |
passwd1 |
nane1 |
email1 |
id2 |
passwd2 |
nane2 |
email2 |
id3 |
passwd3 |
nane3 |
email3 |
해결책
1번 해결책
생성문을 기존 칼럼 순서와 동일하게 만든다.
create external table if not exists db명.tb명 (
id string
, passwd string
, name string
, email string
)
;
- 위에 설명한 원인을 예시로 보면 위와 같이 기존 테이블 순서에 맞게 변경한 후 이관시킨다
- 하지만 이 방법은 columns끼리 묶으려고 순서를 변경하였기에 썩 좋지 않은 방법이라고 생각한다.
2번 해결책 ( 실패 )
ALTER TABLE test_change CHANGE [ col_name ] [col_new_name] [type] [location]
을 이용하여 순서변경
alter table db명.tb명 change id id string first ;
alter table db명.tb명 change passwd passwd string after id ;
alter table db명.tb명 change name name string after passwd ;
alter table db명.tb명 change email email string after name ;
3번 해결책 ( 성공 )
select a.CD_ID, a.COLUMN_NAME, a.INTEGER_IDX
from hive.COLUMNS_V2 a
inner join hive.SDS b
on b.CD_ID = a.CD_ID
inner join hive.TBLS c
on c.SD_ID = b.SD_ID
and c.TBL_NAME = 'table_name'
order by INTEGER_IDX
;
출력 예시
CD_ID |
COLUMN_NAME |
INTEGER_IDX |
504 |
member_id |
0 |
504 |
char_id |
1 |
504 |
first_time |
2 |
504 |
last_time |
3 |
504 |
reg_time |
4 |
위와 같이 metastore 정보를 확인해보면 COLUMN_NAME,integer_idx을 확인할 수 있다.
해당 COLUMN_NAME과 INTEGER_IDX의 값과 순서를 기존 테이블과 동일하게 만들어주면된다.
따로 외래키등이 걸려있지 않아서 해당 CD_ID의 값을 insert overwrite로 해결하였다.
추정되는 원인
기존에 hive.metastore.schema.verification=false로 처리가 되어 있어 schema 검증을 제대로 안 되서 그런 것으로 추정됨
hive --orcfiledump /File_Path
이 명령어를 이용하여 orc 파일의 schema를 확인해보니, 아래와 같다.
hive.metastore.schema.verification=true
Structure for /File_Path
File Version: 0.12 with HIVE_8732
Rows: 1151
Compression: SNAPPY
Compression size: 262144
Type: struct<member_id:string,char_id:string,first_time:timestamp,last_time:timestamp,reg_time:timestamp>
Stripe Statistics:
Stripe 1:
...
File Statistics:
...
Stripes:
...
hive.metastore.schema.verification=false
Structure for /File_Path
File Version: 0.12 with HIVE_13083
Rows: 1095
Compression: SNAPPY
Compression size: 262144
Type: struct<_col0:string,_col1:string,_col2:timestamp,_col3:timestamp,_col6:timestamp>
Stripe Statistics:
Stripe 1:
...
File Statistics:
...
Stripes:
...
위 결과를 보면 hive.metastore.schema.verification=true를 해야 orc의 schema값이 Hive의 스키마로 잘 생성된다.
true
Type: struct<member_id:string,char_id:string,first_time:timestamp,last_time:timestamp,reg_time:timestamp>
false
Type: struct<_col0:string,_col1:string,_col2:timestamp,_col3:timestamp,_col6:timestamp>
데이터 이전 전의 테이블이 false로 되어있던 것 이기 때문에 테스트는 하지 못 하였으나, 겪은 문제처럼 칼럼 순서가 뒤바뀐 이유는 이것으로 추정된다.