문자열 함수
문자열 함수 (String functions)
PPL에서 지원되는 문자열 함수는 다음과 같아요.
출처: 문서
본문
CONCAT
Usage: CONCAT(str1, str2, ...., str_9)
최대 9개의 문자열을 연결해요.
Parameters:
str1, str2, ..., str_9(Required): 연결할 최대 9개의 문자열이에요.
Return type: STRING
Example
source=people
| eval `CONCAT('hello', 'world')` = CONCAT('hello', 'world'), `CONCAT('hello ', 'whole ', 'world', '!')` = CONCAT('hello ', 'whole ', 'world', '!')
| fields `CONCAT('hello', 'world')`, `CONCAT('hello ', 'whole ', 'world', '!')`
질의는 다음과 같은 결과를 반환해요.
| CONCAT(‘hello’, ‘world’) | CONCAT(‘hello ‘, ‘whole ‘, ‘world’, ‘!’) |
|---|---|
| helloworld | hello whole world! |
CONCAT_WS
Usage: CONCAT_WS(sep, str1, str2)
sep을 두 문자열 사이의 구분자로 사용해 str1과 str2를 연결한 결과를 반환해요.
Parameters:
sep(Required): 연결된 문자열 사이에 넣을 구분자 문자열이에요.str1(Required): 연결할 첫 번째 문자열이에요.str2(Required): 연결할 두 번째 문자열이에요.
Return type: STRING
Example
source=people
| eval `CONCAT_WS(',', 'hello', 'world')` = CONCAT_WS(',', 'hello', 'world')
| fields `CONCAT_WS(',', 'hello', 'world')`
질의는 다음과 같은 결과를 반환해요.
| CONCAT_WS(‘,’, ‘hello’, ‘world’) |
|---|
| hello,world |
LENGTH
Usage: length(str)
바이트 단위로 측정한 문자열의 길이를 반환해요.
Parameters:
str(Required): 길이를 계산할 문자열이에요.
Return type: INTEGER
Example
source=people
| eval `LENGTH('helloworld')` = LENGTH('helloworld')
| fields `LENGTH('helloworld')`
질의는 다음과 같은 결과를 반환해요.
| LENGTH(‘helloworld’) |
|---|
| 10 |
LIKE
Usage: like(string, PATTERN[, case_sensitive])
문자열이 패턴과 일치하면 TRUE를, 그렇지 않으면 FALSE를 반환해요.
Parameters:
string(Required): 패턴과 대조할 문자열이에요.PATTERN(Required): 일치시킬 패턴으로, 와일드카드를 지원해요.case_sensitive(Optional): 패턴 일치가 대소문자를 구분하는지 여부예요. 기본값은plugins.ppl.syntax.legacy.preferred에 따라 결정돼요.
Wildcards:
%- 0개, 1개 또는 여러 문자를 나타내요._- 단일 문자를 나타내요.
Configuration:
plugins.ppl.syntax.legacy.preferred=true일 때case_sensitive의 기본값은false예요.plugins.ppl.syntax.legacy.preferred=false일 때case_sensitive의 기본값은true예요.
Return type: BOOLEAN
Example
source=people
| eval `LIKE('hello world', '_ello%')` = LIKE('hello world', '_ello%'), `LIKE('hello world', '_ELLo%', true)` = LIKE('hello world', '_ELLo%', true), `LIKE('hello world', '_ELLo%', false)` = LIKE('hello world', '_ELLo%', false)
| fields `LIKE('hello world', '_ello%')`, `LIKE('hello world', '_ELLo%', true)`, `LIKE('hello world', '_ELLo%', false)`
질의는 다음과 같은 결과를 반환해요.
| LIKE(‘hello world’, ‘_ello%’) | LIKE(‘hello world’, ‘_ELLo%’, true) | LIKE(‘hello world’, ‘_ELLo%’, false) |
|---|---|---|
| True | False | True |
제한 사항: LIKE 함수의 DSL 와일드카드 질의로의 푸시다운은 keyword 필드에서만 지원돼요.
ILIKE
Usage: ilike(string, PATTERN)
문자열이 패턴과 일치하면(대소문자 무시) TRUE를, 그렇지 않으면 FALSE를 반환해요.
Parameters:
string(Required): 패턴과 대조할 문자열이에요.PATTERN(Required): 대소문자를 무시하는 일치 패턴으로, 와일드카드를 지원해요.
Wildcards:
%- 0개, 1개 또는 여러 문자를 나타내요._- 단일 문자를 나타내요.
Return type: BOOLEAN
Example
source=people
| eval `ILIKE('hello world', '_ELLo%')` = ILIKE('hello world', '_ELLo%')
| fields `ILIKE('hello world', '_ELLo%')`
질의는 다음과 같은 결과를 반환해요.
| ILIKE(‘hello world’, ‘_ELLo%’) |
|---|
| True |
제한 사항: ILIKE 함수의 DSL 와일드카드 질의로의 푸시다운은 keyword 필드에서만 지원돼요.
LOCATE
Usage: locate(substr, str[, start])
str에서 start 위치부터 시작해 substr이 처음 등장하는 위치를 반환해요. start를 지정하지 않으면 검색이 위치 1에서 시작해요. substr을 찾지 못하면 0을 반환해요. 인자 중 하나라도 NULL이면 함수는 NULL을 반환해요.
Parameters:
substr(Required): 검색할 부분 문자열이에요.str(Required): 검색 대상 문자열이에요.start(Optional): 검색을 시작할 위치예요. 기본값은 1이에요.
Return type: INTEGER
Example
source=people
| eval `LOCATE('world', 'helloworld')` = LOCATE('world', 'helloworld'), `LOCATE('invalid', 'helloworld')` = LOCATE('invalid', 'helloworld'), `LOCATE('world', 'helloworld', 6)` = LOCATE('world', 'helloworld', 6)
| fields `LOCATE('world', 'helloworld')`, `LOCATE('invalid', 'helloworld')`, `LOCATE('world', 'helloworld', 6)`
질의는 다음과 같은 결과를 반환해요.
| LOCATE(‘world’, ‘helloworld’) | LOCATE(‘invalid’, ‘helloworld’) | LOCATE(‘world’, ‘helloworld’, 6) |
|---|---|---|
| 6 | 0 | 6 |
LOWER
Usage: lower(string)
문자열을 소문자로 변환해요.
Parameters:
string(Required): 소문자로 변환할 문자열이에요.
Return type: STRING
Example
source=people
| eval `LOWER('helloworld')` = LOWER('helloworld'), `LOWER('HELLOWORLD')` = LOWER('HELLOWORLD')
| fields `LOWER('helloworld')`, `LOWER('HELLOWORLD')`
질의는 다음과 같은 결과를 반환해요.
| LOWER(‘helloworld’) | LOWER(‘HELLOWORLD’) |
|---|---|
| helloworld | helloworld |
LTRIM
Usage: ltrim(str)
문자열에서 앞쪽 공백 문자를 제거해요.
Parameters:
str(Required): 앞쪽 공백을 제거할 문자열이에요.
Return type: STRING
Example
source=people
| eval `LTRIM(' hello')` = LTRIM(' hello'), `LTRIM('hello ')` = LTRIM('hello ')
| fields `LTRIM(' hello')`, `LTRIM('hello ')`
질의는 다음과 같은 결과를 반환해요.
| LTRIM(‘ hello’) | LTRIM(‘hello ‘) |
|---|---|
| hello | hello |
POSITION
Usage: POSITION(substr IN str)
str에서 substr이 처음 등장하는 위치를 반환해요. substr을 찾지 못하면 0을 반환해요. 인자 중 하나라도 NULL이면 NULL을 반환해요.
Parameters:
substr(Required): 검색할 부분 문자열이에요.str(Required): 검색 대상 문자열이에요.
Return type: INTEGER
Example
source=people
| eval `POSITION('world' IN 'helloworld')` = POSITION('world' IN 'helloworld'), `POSITION('invalid' IN 'helloworld')` = POSITION('invalid' IN 'helloworld')
| fields `POSITION('world' IN 'helloworld')`, `POSITION('invalid' IN 'helloworld')`
질의는 다음과 같은 결과를 반환해요.
| POSITION(‘world’ IN ‘helloworld’) | POSITION(‘invalid’ IN ‘helloworld’) |
|---|---|
| 6 | 0 |
REPLACE
Usage: replace(str, pattern, replacement)
str에서 패턴과 일치하는 모든 항목이 대체 문자열로 교체된 문자열을 반환해요. 인자 중 하나라도 NULL이면 NULL을 반환해요.
Parameters:
str(Required): 대체를 수행할 입력 문자열이에요.pattern(Required): 대조할 정규 표현식 패턴이에요(Java regex 문법 지원).replacement(Required): 대체 문자열이에요.
Return type: STRING
정규 표현식 지원: pattern 인자는 Java regex 문법을 지원해요.
정규 표현식 특수 문자: 패턴은 정규 표현식(regex)으로 해석돼요. 다음 문자는 regex에서 특별한 의미를 가져요: ., *, +, [, ], (, ), {, }, ^, $, |, ?, \. 이 문자들을 리터럴로 일치시키려면 백슬래시로 이스케이프하세요:
example.com은'example\\.com'이 돼요(이스케이프된 점).value*는'value\\*'가 돼요(이스케이프된 별표).price+tax는'price\\+tax'가 돼요(이스케이프된 플러스).
여러 특수 문자를 포함하는 문자열은 \\Q...\\E로 감싸 전체 문자열을 리터럴로 취급할 수 있어요. 예를 들어 '\\Qhttps://example.com/path?id=123\\E'는 전체 URL을 리터럴 문자열로 취급해요.
예시: 리터럴 문자열 대체
source=people
| eval `REPLACE('helloworld', 'world', 'universe')` = REPLACE('helloworld', 'world', 'universe'), `REPLACE('helloworld', 'invalid', 'universe')` = REPLACE('helloworld', 'invalid', 'universe')
| fields `REPLACE('helloworld', 'world', 'universe')`, `REPLACE('helloworld', 'invalid', 'universe')`
질의는 다음과 같은 결과를 반환해요.
| REPLACE(‘helloworld’, ‘world’, ‘universe’) | REPLACE(‘helloworld’, ‘invalid’, ‘universe’) |
|---|---|
| hellouniverse | helloworld |
예시: 특수 문자 이스케이프
source=people
| eval `Replace domain` = REPLACE('api.example.com', 'example\\.com', 'newsite.org'), `Replace with quote` = REPLACE('https://api.example.com/v1', '\\Qhttps://api.example.com\\E', 'http://localhost:8080')
| fields `Replace domain`, `Replace with quote`
질의는 다음과 같은 결과를 반환해요.
| Replace domain | Replace with quote |
|---|---|
| api.newsite.org | http://localhost:8080/v1 |
예시: 정규 표현식 패턴
source=people
| eval `Remove digits` = REPLACE('test123', '\\d+', ''), `Collapse spaces` = REPLACE('hello world', ' +', ' '), `Remove special` = REPLACE('hello@world!', '[^a-zA-Z]', '')
| fields `Remove digits`, `Collapse spaces`, `Remove special`
질의는 다음과 같은 결과를 반환해요.
| Remove digits | Collapse spaces | Remove special |
|---|---|---|
| test | hello world | helloworld |
예시: 캡처 그룹과 역참조
source=people
| eval `Swap date` = REPLACE('1/14/2023', '^(\\d{1,2})/(\\d{1,2})/', '$2/$1/'), `Reverse words` = REPLACE('Hello World', '(\\w+) (\\w+)', '$2 $1'), `Extract domain` = REPLACE('[email protected]', '.*@(.+)', '$1')
| fields `Swap date`, `Reverse words`, `Extract domain`
질의는 다음과 같은 결과를 반환해요.
| Swap date | Reverse words | Extract domain |
|---|---|---|
| 14/1/2023 | World Hello | example.com |
예시: 고급 정규 표현식
source=people
| eval `Clean phone` = REPLACE('(555) 123-4567', '[^0-9]', ''), `Remove vowels` = REPLACE('hello world', '[aeiou]', ''), `Add prefix` = REPLACE('test', '^', 'pre_')
| fields `Clean phone`, `Remove vowels`, `Add prefix`
질의는 다음과 같은 결과를 반환해요.
| Clean phone | Remove vowels | Add prefix |
|---|---|---|
| 5551234567 | hll wrld | pre_test |
PPL 질의에서 regex 패턴 사용 시 참고 사항:
- 백슬래시는 두 번 반복해 이스케이프해야 해요:
\대신\\. 예: 숫자 패턴은\\d, 단어 문자는\\w+. - 역참조는 PCRE 스타일(
\1,\2)과 Java 스타일($1,$2) 문법을 모두 지원해요. PCRE 스타일 역참조는 내부적으로 Java 스타일로 자동 변환돼요.
REVERSE
Usage: REVERSE(str)
주어진 문자열을 뒤집은 결과를 반환해요.
Parameters:
str(Required): 뒤집을 문자열이에요.
Return type: STRING
Example
source=people
| eval `REVERSE('abcde')` = REVERSE('abcde')
| fields `REVERSE('abcde')`
질의는 다음과 같은 결과를 반환해요.
| REVERSE(‘abcde’) |
|---|
| edcba |
RIGHT
Usage: right(str, len)
str의 마지막 len 개수만큼의 문자를 반환해요. 인자 중 하나라도 NULL이면 NULL을 반환해요.
Parameters:
str(Required): 입력 문자열이에요.len(Required): 오른쪽에서 반환할 문자 수예요.
Return type: STRING
Example
source=people
| eval `RIGHT('helloworld', 5)` = RIGHT('helloworld', 5), `RIGHT('HELLOWORLD', 0)` = RIGHT('HELLOWORLD', 0)
| fields `RIGHT('helloworld', 5)`, `RIGHT('HELLOWORLD', 0)`
질의는 다음과 같은 결과를 반환해요.
| RIGHT(‘helloworld’, 5) | RIGHT(‘HELLOWORLD’, 0) |
|---|---|
| world |
RTRIM
Usage: rtrim(str)
문자열에서 뒤쪽 공백 문자를 제거해요.
Parameters:
str(Required): 뒤쪽 공백을 제거할 문자열이에요.
Return type: STRING
Example
source=people
| eval `RTRIM(' hello')` = RTRIM(' hello'), `RTRIM('hello ')` = RTRIM('hello ')
| fields `RTRIM(' hello')`, `RTRIM('hello ')`
질의는 다음과 같은 결과를 반환해요.
| RTRIM(‘ hello’) | RTRIM(‘hello ‘) |
|---|---|
| hello | hello |
SUBSTRING
Usage: substring(str, start[, length])
str의 start 위치에서 시작해 length 문자만큼의 부분 문자열을 반환해요. length를 지정하지 않으면 start부터 문자열 끝까지의 부분 문자열을 반환해요.
Parameters:
str(Required): 입력 문자열이에요.start(Required): 부분 문자열의 시작 위치예요.length(Optional): 부분 문자열의 길이예요. 지정하지 않으면start부터 끝까지 반환해요.
Return type: STRING
Synonyms: SUBSTR
Example
source=people
| eval `SUBSTRING('helloworld', 5)` = SUBSTRING('helloworld', 5), `SUBSTRING('helloworld', 5, 3)` = SUBSTRING('helloworld', 5, 3)
| fields `SUBSTRING('helloworld', 5)`, `SUBSTRING('helloworld', 5, 3)`
질의는 다음과 같은 결과를 반환해요.
| SUBSTRING(‘helloworld’, 5) | SUBSTRING(‘helloworld’, 5, 3) |
|---|---|
| oworld | owo |
TRIM
Usage: trim(str)
문자열에서 앞쪽과 뒤쪽 공백 문자를 제거해요.
Parameters:
str(Required): 앞뒤 공백을 제거할 문자열이에요.
Return type: STRING
Example
source=people
| eval `TRIM(' hello')` = TRIM(' hello'), `TRIM('hello ')` = TRIM('hello ')
| fields `TRIM(' hello')`, `TRIM('hello ')`
질의는 다음과 같은 결과를 반환해요.
| TRIM(‘ hello’) | TRIM(‘hello ‘) |
|---|---|
| hello | hello |
UPPER
Usage: upper(string)
문자열을 대문자로 변환해요.
Parameters:
string(Required): 대문자로 변환할 문자열이에요.
Return type: STRING
Example
source=people
| eval `UPPER('helloworld')` = UPPER('helloworld'), `UPPER('HELLOWORLD')` = UPPER('HELLOWORLD')
| fields `UPPER('helloworld')`, `UPPER('HELLOWORLD')`
질의는 다음과 같은 결과를 반환해요.
| UPPER(‘helloworld’) | UPPER(‘HELLOWORLD’) |
|---|---|
| HELLOWORLD | HELLOWORLD |
REGEXP_REPLACE
Usage: regexp_replace(str, pattern, replacement)
str에서 pattern과 일치하는 모든 부분 문자열을 replacement로 대체한 결과 문자열을 반환해요.
Parameters:
str(Required): 대체를 수행할 입력 문자열이에요.pattern(Required): 대조할 정규 표현식 패턴이에요.replacement(Required): 대체 문자열이에요.
Return type: STRING
Synonyms: REPLACE
Example
source=people
| eval `DOMAIN` = REGEXP_REPLACE('https://opensearch.org/downloads/', '^https?://(?:www\\.)?([^/]+)/.*$', '\\1')
| fields `DOMAIN`
질의는 다음과 같은 결과를 반환해요.
| DOMAIN |
|---|
| opensearch.org |