[Elasticsearch] LowLevelRestClient 를 이용한 개발 시 Json 결과 처리

Elastic/Elasticsearch 2021. 11. 25. 14:08

보통 Elasticsearch LLRC 를 이용해서 RESTful API 요청 하게 되면
...중략...
Request request = new Request(...중략...);
Response response = esClient.getRestClient().performRequest(request);
String body = EntityUtils.toString(response.getEntity());
...중략...

String 으로 결과를 받아서 리턴 하게 되는데 이 과정에서 JSON String 변환 시 slash 가 추가 되는 불편함이 있을 수 있습니다.
이걸 해소 하려면
...중략...
String body = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = ObjectMapper.readTree(body);
...중략...
readTree 해서 JsonNode Object 로 변환 한 후 처리 하시면 {}, [] 등 모두 깔끔하게 처리가 가능 합니다.

 

ASIS)

{
	"response": "{ \"alias\": \"henry\" }"
}

{
	"response": "[
    	{ \"alias1\": \"henry\" },
        { \"alias2\": \"jeong\" }
      ]"
}

 

TOBE)

{
	"response": { "alias": "henry" }
}

{
	"response": [
    	{ "alias1": "henry" },
        { "alias2": "jeong" }
      ]
}

 

:

[cURL] cURL in Bash Script

ITWeb/개발일반 2021. 11. 23. 15:35

Case 1)
response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'`

Case 2)
curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'

Case 3)
size=100
message="test"

response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'`

Case 4)
size=100
message="test"

curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'

proxy 를 지정 하고자 한다면 아래 내용 추가 합니다.
curl -x 'http://PROXY-IP:PROXY-PORT' -s -X GET 'http://www.kakaopay.com'

 

:

[Shell] Bash Random Number & Substring

ITWeb/개발일반 2021. 11. 23. 15:33

이런것도 기억력을 돕기 위해 기록해 봅니다.

 

- Bash Random Number
$ echo $(( $RANDOM % 4 ))

 

nodes=( '1' '2' '3' '4' )

i=$(( $RANDOM % 4 ))

node=${nodes[${i}]}

- Bash Substring
${String:Position:length} 문자:인덱스:길이

 

:

[Logstash] filter split 예제

Elastic/Logstash 2021. 11. 18. 12:12

참고문서)

https://www.elastic.co/guide/en/logstash/current/plugins-filters-split.html

https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-split

 

예제코드)

라인단위로 정보가 작성 되어 있을 때

데이터 예시)
1|A|가
2|B|나

filter {
	split { }

	mutate {
		split => { "message" => "|" }
		add_field => {
			"first" => "%{[message][0]}"
			"second" => "%{[message][1]}"
			"third" => "%{[message][2]}"
		}
	}
}

 

:

[Shell] for loop example

ITWeb/개발일반 2021. 11. 18. 11:25

간혹 서버간 ssh tunneling 을 위해 pub key 를 등록 해야 할 일이 있습니다.

노가다 하기 귀찮으니까 instance 목록만 가지고 쭈욱 돌리면 되겠죠.

 

#!/bin/bash

PUB="abcdefg"
HOSTS=("ip1" "ip2" "ip3" ...)

for host in "${HOSTS[@]}"
do
  echo "ssh -o StrictHostKeychecking=no USER@$host \"echo '$PUB' >> .ssh/authorized_keys\""
  ssh -o StrictHostKeychecking=no USER@$host "echo '$PUB' >> .ssh/authorized_keys"
done

이런것도 맨날 기억을 못해서 기록을 합니다.

: