PySpark을 못쓰는 상황에서 대규모 데이터는 Impala를 통해서 하둡에서 가져와야 하고, 파이썬에서는 가져온 데이터의 기본적인 확인 및 모델링을 주로 하게 된다. 따라서 임팔라에서 데이터를 랜덤하게 가져와야 하는 상황이라면 해당 쿼리를 짜야 하게 되어서 생각하게 된게, 쿼리를 동적으로 돌리되 왠만한 데이터 생성은 모두 임팔라로 한다였다. 

 

 1. 모델 생성할 대상군의 샘플링을 모두 임팔라로 진행한다. 여기서는 기본 고객키만을 이용해서 생성하므로 오래 걸리지는 않을 것이다. : 쿼리는 임팔라로 이를 반복 실행하는 것은 파이썬으로 짠다.

 

 2. 생성된 고객키를 가지고 실제 고객모델링할 데이터를 생성한다. : 1번에서 만들어 놓은 고객키를 기반으로 실제 데이터를 주제별로 생성해서 갖고 있는다.

 

select * from 
(
  select  
  row_number() over (partition by country order by country , random()) rn,
  count() over (partition by country order by country) cntpartition,
  tab.*
  from   dat.mytable tab
)rs
where rs.rn between 1 and cntpartition* 1/100  -- This is for 1% data

 

https://stackoverflow.com/questions/68259133/randomly-sampling-n-rows-in-impala-using-random-or-tablesample-system

 

 

Randomly sampling n rows in impala using random() or tablesample system()

I would like to randomly sample n rows from a table using Impala. I can think of two ways to do this, namely: SELECT * FROM TABLE ORDER BY RANDOM() LIMIT <n> or SELECT * FROM TABLE TABLESAMPLE

stackoverflow.com

 

 

반응형

'Hive, Impala, Spark' 카테고리의 다른 글

경우의 수를 생성하는 Oracle 쿼리  (0) 2022.11.03
Oracle connect by  (0) 2022.06.17
Oracle connect by  (0) 2022.05.25
Convert 'YYYYMM' to timestamp in Impala  (0) 2022.04.21
Calendar week# in the month(Hive Query)  (0) 2020.09.25

+ Recent posts