입력 데이터 변환하기
입력 데이터 변환하기 (Transform input data)
수집 중에 입력 데이터를 변환(transform)하는 방법을 보여 드릴게요. SELECT 절에서 컬럼 값을 변환하고 WHERE 절에서 필터링을 적용하는 방식으로 수집 시점에 데이터를 가공해 봐요.
출처: 문서
본문
사전 준비 (Prerequisite)
진행하기 전에 Quickstart (local)에 설명된 대로 Apache Druid®를 다운로드하고 로컬 머신에서 실행 중이어야 해요. Druid 클러스터에 데이터를 로드할 필요는 없어요.
Druid에서 데이터를 쿼리하는 방법에 익숙해야 해요. 아직 안 하셨다면 먼저 Query data 튜토리얼을 진행해 주세요.
샘플 데이터 (Sample data)
이 튜토리얼에서는 다음 샘플 데이터를 사용해요:
{"timestamp":"2018-01-01T07:01:35Z", "animal":"octopus", "location":1, "number":100}
{"timestamp":"2018-01-01T05:01:35Z", "animal":"mongoose", "location":2,"number":200}
{"timestamp":"2018-01-01T06:01:35Z", "animal":"snake", "location":3, "number":300}
{"timestamp":"2018-01-01T01:01:35Z", "animal":"lion", "location":4, "number":300}
수집 중 데이터 변환하기 (Transform data during ingestion)
INSERT INTO 문과 EXTERN 함수를 사용해 데이터를 인라인으로 수집하면서 샘플 데이터셋을 로드해 볼게요. Druid 웹 콘솔 (Druid web console)에서 Query 뷰로 이동해 다음 쿼리를 실행해 주세요:
INSERT INTO "transform_tutorial"
WITH "ext" AS (
SELECT *
FROM TABLE(EXTERN('{"type":"inline","data":"{\"timestamp\":\"2018-01-01T07:01:35Z\",\"animal\":\"octopus\", \"location\":1, \"number\":100}\n{\"timestamp\":\"2018-01-01T05:01:35Z\",\"animal\":\"mongoose\", \"location\":2,\"number\":200}\n{\"timestamp\":\"2018-01-01T06:01:35Z\",\"animal\":\"snake\", \"location\":3, \"number\":300}\n{\"timestamp\":\"2018-01-01T01:01:35Z\",\"animal\":\"lion\", \"location\":4, \"number\":300}"}', '{"type":"json"}'))
EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "location" BIGINT, "number" BIGINT)
)
SELECT
TIME_PARSE("timestamp") AS "__time",
TEXTCAT('super-', "animal") AS "animal",
"location",
"number",
"number" * 3 AS "triple-number"
FROM "ext"
WHERE (TEXTCAT('super-', "animal") = 'super-mongoose' OR "location" = 3 OR "number" = 100)
PARTITIONED BY DAY
SELECT 절에서 다음 변환을 지정해요:
animal: TEXTCAT 함수로animal컬럼의 값 앞에"super-"를 붙여요. 변환된 데이터만 수집한다는 점에 주목해 주세요.triple-number:number컬럼에 3을 곱하고 결과를triple-number라는 컬럼에 저장해요. 쿼리가 원본과 변환된 데이터를 모두 수집한다는 점에 주목해 주세요.
추가로 WHERE 절은 다음 세 개의 OR 연산자를 적용해서, 다음 조건 중 최소 하나가 참인 행만 쿼리가 수집하게 해요:
TEXTCAT('super-', "animal")이"super-mongoose"와 일치location이 3과 일치number가 100과 일치
행이 필터를 통과하면 수집 작업이 변환을 적용해요. 이 예시에서 필터는 처음 세 행을 선택해요. 각 행이 필요한 OR 조건 중 최소 하나를 충족하기 때문이에요. 선택된 행에 대해 수집 작업은 변환된 animal 컬럼, location 컬럼, 원본 number 와 변환된 triple-number 컬럼을 수집해요. "lion" 행은 어떤 조건도 충족하지 않으므로 수집되거나 변환되지 않아요.
변환된 데이터 쿼리하기 (Query the transformed data)
웹 콘솔의 Query 뷰에서 새 탭을 열어 주세요. 다음 쿼리를 실행해 수집된 데이터를 확인해 주세요:
SELECT * FROM "transform_tutorial"
다음과 같은 결과가 반환돼요:
__time |
animal |
location |
number |
triple-number |
|---|---|---|---|---|
| 2018-01-01T05:01:35.000Z | super-mongoose | 2 | 200 | 600 |
| 2018-01-01T06:01:35.000Z | super-snake | 3 | 300 | 900 |
| 2018-01-01T07:01:35.000Z | super-octopus | 1 | 100 | 300 |
"lion" 행이 빠져 있고, 수집된 다른 세 행에는 변환이 적용되어 있는 걸 확인할 수 있어요.
더 알아보기 (Learn more)
자세한 내용은 다음 주제를 참고해 주세요:
- All functions (모든 함수) — 데이터 변환에 사용할 수 있는 함수 목록.
- Transform spec 참조 (Transform spec reference) — JSON 기반 배치 수집에서 변환에 대해 더 알아보기.
- WHERE clause — Druid SQL에서 필터를 지정하는 방법.