개요
nifi에서 사용하는 groovy processor를 만드는데 필요한 코드 스니펫이다. 지저분하지만 json parsing이나 flow file 처리 등을 나중에 찾아보기 위한 용도로 포스팅 해둔다.
샘플
package com.creamsugardonut
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
class TestMatrixToConverter {
static void main(String[] args) {
def jsonSlurper = new JsonSlurper()
def body = StringBuilder.newInstance()
def number = 1
def line = "{\"t_id\":\"436219846\",\"t_timestamp\":1.511426387E12,\"t_props\":{\"user_key\":\"123\",\"admin_recm_yn\":\"N\",\"del_yn\":\"N\",\"plylst_type_code\":\"TYPE1\",\"dsply_order\":\"3\",\"admin_open_yn\":\"Y\",\"song_cnt\":\"159\",\"djmag_reg_yn\":\"N\",\"cate_type_code\":null,\"cate_code\":null,\"open_yn\":\"N\",\"del_posbl_yn\":\"Y\",\"reg_date\":\"2017-11-13 00:35:04.000\",\"plylst_title\":\"title\",\"fame_reg_yn\":\"N\",\"updt_date\":\"2017-11-23 17:39:47.000\"},\"t_songs\":[\"123\",\"456\"]}"
def obj = jsonSlurper.parseText(line)
obj.t_props.each { key, val ->
if (key.contains("BASKET")) {
convertBasketToArray(obj, key)
}
}
if (obj.get("t_id") != null) {
obj.put("id", obj.get("t_id").toString())
obj.remove("t_id")
} else {
obj.put("from", obj.get("t_from").toString())
obj.remove("t_from")
obj.put("to", obj.get("t_to").toString())
obj.remove("t_to")
obj.put("label", obj.get("t_label"))
obj.remove("t_label")
}
obj.put("timestamp", obj.get("t_timestamp"))
obj.remove("t_timestamp")
obj.put("props", obj.get("t_props"))
obj.remove("t_props")
if (obj.get("props") != null) {
convertToInteger(obj, "play_time")
convertToInteger(obj, "svc_avail_flg")
removeIfNull(obj, "song_name")
removeIfNull(obj, "rep_song_id")
removeIfNull(obj, "album_id")
removeIfNull(obj, "album_name")
removeIfNull(obj, "album_img_path")
removeIfNull(obj, "issue_date")
removeIfNull(obj, "order_issue_date")
removeIfNull(obj, "reg_date")
removeIfNull(obj, "act_type_name")
removeIfNull(obj, "artist_img_path")
removeIfNull(obj, "artist_name")
removeIfNull(obj, "album_img_path")
removeIfNull(obj, "rep_song_name")
removeIfNull(obj, "birthday")
removeIfNull(obj, "gender")
removeIfNull(obj, "prefe_artist")
removeIfNull(obj, "prefe_gnr")
removeIfNull(obj, "prefe_style")
convertToInteger(obj, "main_use_dow")
convertToInteger(obj, "main_use_tmz")
convertToInteger(obj, "ad_age_band")
if (obj.get("t_songs") == null) {
if ("true".equals(obj.props.get("top100_yn"))) {
obj.props.put("top100_yn", true)
} else {
obj.props.put("top100_yn", false)
}
} else {
if ("true".equals(obj.props.get("admin_open_yn"))) {
obj.props.put("admin_open_yn", true)
} else {
obj.props.put("admin_open_yn", false)
}
if ("true".equals(obj.props.get("admin_recm_yn"))) {
obj.props.put("admin_recm_yn", true)
} else {
obj.props.put("admin_recm_yn", false)
}
if ("true".equals(obj.props.get("del_posbl_yn"))) {
obj.props.put("del_posbl_yn", true)
} else {
obj.props.put("del_posbl_yn", false)
}
if ("true".equals(obj.props.get("del_yn"))) {
obj.props.put("del_yn", true)
} else {
obj.props.put("del_yn", false)
}
if ("true".equals(obj.props.get("djmag_reg_yn"))) {
obj.props.put("djmag_reg_yn", true)
} else {
obj.props.put("djmag_reg_yn", false)
}
if ("true".equals(obj.props.get("djmag_reg_yn"))) {
obj.props.put("djmag_reg_yn", true)
} else {
obj.props.put("djmag_reg_yn", false)
}
if ("true".equals(obj.props.get("open_yn"))) {
obj.props.put("open_yn", true)
} else {
obj.props.put("open_yn", false)
}
obj.props.put("songs", obj.get("t_songs"))
obj.remove("t_songs")
}
}
if (number != 1) {
body << ","
}
body << JsonOutput.toJson(obj)
println "body = " + body
}
def static convertBasketToArray(obj, key) {
if (obj.t_props.get(key) != null && obj.t_props.get(key).length() > 0)
obj.t_props.put(key, obj.t_props.get(key).tokenize("@#%@#%"))
}
def static convertToInteger(obj, key) {
if (obj.props.get(key) == null) {
obj.props.remove(key)
} else {
obj.props.put(key, obj.props.get(key).toInteger())
}
}
def static removeIfNull(obj, key) {
if (obj.props.get(key) == null) {
obj.props.remove(key)
}
}
}