Thanks to visit codestin.com
Credit goes to jeewonb.github.io


Elasticsearch Query

1
2
## index 목록 조회
GET _cat/indices
1
2
3
4
5
6
7
## 모든 index 대상 전체 검색
GET _search
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
6
7
## 조회조건 없이
GET processIndex-*/_search
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## match, sort, range 쿼리
GET processIndex-2020.10.05/_search?track_total_hits=true&size=100
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"match": {
"SYSTEM_ID": "SYSTEM-1"
}
},
{
"match": {
"SERVER_ID": "SERVER-1"
}
},
{
"match": {
"PROCESS_ID": "PROCESS-1"
}
},
{
"range": {
"SERVER_TIME": {
"gte": "now-1h",
"lte": "now"
}
}
}
],
"should": [],
"must_not": []
}
}
}
},
"sort": [
{
"SERVER_TIME": {
"order": "asc"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## 특정 날짜 index대상 상위 1000개 검색
## TXN_KEY 빈 값 제외
GET servicerlogIndex-*-2020.10.06/_search?size=1000
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "TXN_KEY"
}
},
{
"match": {
"SYSTEM_ID": "SYSTEM-1"
}
},
{
"match": {
"PROCESS_TYPE": "PROCESS-1"
}
},
{
"range": {
"TXN_TIME": {
"gte": "2020-09-15T02:09:31.578Z",
"lte": "2020-09-15T02:24:31.578Z"
}
}
}
],
"should": [],
"must_not": [
{
"term": {
"TXN_KEY": ""
}
}
]
}
}
}
},
"sort": [
{
"TXN_TIME": {
"order": "asc"
}
},
{
"TXN_KEY": {
"order": "asc"
}
}
]
}
## track_total_hits=true 일 경우 전체 결과 건수 표시
## 최근 1시간
## 시간 역순

GET processIndex-system-1-server-1-process-1-2020.10.05/_search?track_total_hits=true&size=100
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "SYSTEM_ID": "SYSTEM-1"
              }
            },
            {
              "match": {
                "SERVER_ID": "SERVER-1"
              }
            },
            {
              "match": {
                "PROCESS_ID": "PROCESS-1"
              }
            },
            {
              "range": {
                "SERVER_TIME": {
                  "gte": "now-1h",
                  "lte": "now"
                }
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      }
    }
  },
  "sort": [
    {
      "PROCESS_INFO.PROCESS_STATUS_INFO.SERVER_TIME": {
        "order": "asc"
      }
    }
  ]
}


Curator로 Elasticsearch Index 삭제하기

1. curator 설치

2. curator.yml, action file(delete_indices.yml) 설정

  • curator.yml
  • delete_indices.yml
1
$curator —config curator.yml delete_indices.yml
curator.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Curator config file
# Remember, leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
client:
hosts:
- 127.0.0.1
port: 9200
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
ssl_no_validate: False
http_auth:
timeout: 30
master_only: False

logging:
loglevel: INFO
logfile:
logformat: default
blacklist: ['elasticsearch', 'urllib3']

delete_indices.yml

아래 예시는 액션 4개를 한 번에 정의함

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
actions:
1:
action: delete_indices
description: >-
Delete indices - servicerunlogreport - older than 8 day.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: servicerunlogreport
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 8
exclude:
2:
action: delete_indices
description: >-
Delete indices - processstatus - older than 3 day.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: processstatus-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 3
exclude:
3:
action: delete_indices
description: >-
Delete indices - servicerunstatus - older than 3 day.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: servicerunstatus-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 3
exclude:
4:
action: delete_indices
description: >-
Delete indices - middlewareeventreport - older than 5 day.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: middlewareeventreport-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 5
exclude:
3. 스케줄러로 등록하려면 crontab 이용
1
2

$crontab -e
1
2
3
4
5
01 10 * * * sh /home/wasadm/crontab/delete_servicerunlogreport.sh >> /var/log/elastic_cron/delete_srl.log 2>&1
01 10 * * * sh /home/wasadm/crontab/delete_servicerunstatus.sh >> /var/log/elastic_cron/delete_srs.log 2>&1
01 10 * * * sh /home/wasadm/crontab/delete_serveronlinestatus.sh >> /var/log/elastic_cron/delete_sos.log 2>&1
01 10 * * * sh /home/wasadm/crontab/delete_processstatus.sh >> /var/log/elastic_cron/delete_ps.log 2>&1
01 10 * * * sh /home/wasadm/crontab/delete_middlewareeventreport.sh >> /var/log/elastic_cron/delete_mer.log 2>&1


Beeline으로 하둡 접속 및 데이터 조회

1. 하둡 클러스터 생성

  • 하둡 클러스터 도커 이미지 pull

2. 하둡 클러스터 접속 및 hive 접속

1
2
$ beeline -u jdbc:hive2://localhost:10000 -n scott -p tiger
0: jdbc:hive2://localhost:10000>
여러 설정값
  • outputformat: csv2(‘,’로 구분), dsv(‘|’로 구분) 등
  • hiveconf: hive 관련 config 설정
  • hive.resultset.use.unique.column.names=false: 데이터 조회 시 컬럼명에 테이블명은 제외
1
2
$ beeline -u jdbc:hive2://localhost:10000 -n scott -p tiger
0: jdbc:hive2://localhost:10000> --outputformat=dsv --hiveconf hive.resultset.use.unique.column.names=false -f input/sample.hql > result/sample.txt

beeline으로 접속 후 jdbc -> hiveQl 쿼리 작성하면 됨

주의사항
  • ‘;’로 끝내야함
    1
    2
    $ beeline -u jdbc:hive2://localhost:10000 -n scott -p tiger
    0: jdbc:hive2://localhost:10000> select * from table1 where datetime = '2021-01-01';
  • 종료시에는 !quit

3. to-do (공부할 내용)

  1. hql 문법
  2. 병렬 처리 프로세스 (클러스터간에 영향은?)


프로그래머스 알고리즘

완전 탐색

  • #42840 모의고사
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def solution(answers):
answer = []

case1 = [1, 2, 3, 4, 5] * 2000
case2 = [2, 1, 2, 3, 2, 4, 2, 5] * 1250
case3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] * 1000

#1,2,3번 학생의 score list
score = [0, 0, 0]

for i in range(len(answers)):
#1번 학생
if answers[i] == case1[i]:
score[0] += 1
#2번 학생
if answers[i] == case2[i]:
score[1] += 1
#3번 학생
if answers[i] == case3[i]:
score[2] += 1

#score가 최대인 index list return (1~3번이므로 + 1)
answer = [i + 1 for i, x in enumerate(score) if x == max(score)]

return answer
  • #42839 소수 찾기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#소수 찾기
def solution(numbers): #numbers = "17"
answer = 0
#만들 수 있는 가장 큰 수(71)를 찾아서 거기까지만 소수를 일단 뽑음
temp = []
for x in numbers:
# print(x)
temp.append(x)
temp.sort(reverse=True)
high =('').join(temp)

#primes = [2, 3, 5, 7, ...., 71]
primes = prime_list(int(high) + 1)

answer = validate(primes, numbers)
return answer

#에라토스테네스의 체
def prime_list(n):
#에라토스테네스의 체 초기화: n개 요소에 True 설정(소수로 간주)
sieve = [True] * n

#n의 최대 약수가 sqrt(n) 이하이므로 i=sqrt(n)까지 검사
m = int(n ** 0.5)
for i in range(2, m + 1):
if sieve[i] == True: # i가 소수인 경우
for j in range(i+i, n, i): # i이후 i의 배수들을 False 판정
sieve[j] = False

#소수 목록 산출
return [i for i in range(2, n) if sieve[i] == True]

#numbers에 있는 숫자로 prime을 만들 수 있는지 확인
def validate(primes, numbers):
removed = 0 #primes 리스트에서 제외해야하는 count
for prime in primes: # primes = [2, 3, 5, 7, 11, 13, ... 71] -> prime = 11이라고 할 때
temp = numbers # temp = "17"
for p in str(prime): # prime = "11" -> p = ["1", "1"]
if p not in temp:
removed += 1 # 해당 prime을 primes 리스트에서 제외
break
else :
#numbers에서 prime의 글자를 하나씩 지워나감
temp = temp.replace(p, '', 1)
continue
answers = len(primes) - removed
return answers
  • #42842 카펫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def solution(brown, yellow):
answer = []

#가능한 최대값으로 x 초기값 설정
x = int(brown / 2) + 1 # (x + y) * 2 - 4 = brown (바깥쪽 둘레)
y = int(brown / 2) + 2 - x

while x ** 2 >= brown + yellow:

# 정답
if x * y == brown + yellow:
answer = [x, y]
break
# 닶 아님 -> x, y 값 수정
else:
# x 값 수정
x = x - 1
y = int(brown / 2) + 2 - x
continue
return answer