scala에서 elatic4s에서 복합 쿼리(compound query)를 실행하는 예제이다. 

should 다음에 바로 뒤에 query를 사용하면 문제가 생긴다.



쿼리가 하나 손실되는 예제


shoud {

 matchQuery

 rangeQuery

}




appendShould 예제를 잘 사용하는 것을 추천한다. 


search(searchData.serviceTag / DEFAULT_TYPE).
query {
bool {
must {
matchQuery("message", searchData.grep)
} appendMust {
rangeQuery("@timestamp").gte(searchData.startDateTime).lte(searchData.endDateTime)
}
}
}.size(searchData.size).sortByFieldDesc("@timestamp")


요청 json은 다음과 같다. 



 
   "query": 
      "bool": 
         "should": 
             
               "match": 
                  "message": 
                     "query":"*HTTP*"
                  }
               }
            },
             
               "range": 
                  "@timestamp": 
                     "gte":"2017-11-14T14:00:00+09:00",
                     "lte":"2017-11-14T15:00:00+09:00"
                  }
               }
            }
         ]
      }
   },
   "size":10,
   "sort": 
       
         "@timestamp": 
            "order":"desc"
         }
      }
   ]
}


이 두개의 짝을 잘 맞춰 개발할 필요가 있다. 

should->appendShould

must->mustShould

Posted by '김용환'
,



elasticsearch 4s에서 json 쿼리 요청을 보려면 다음과 같이 사용한다.

(5.2.0이후부터 됨)



val json = search("music" / "bands") query "coldplay"

println("xxxxxxxx " + client.show(json))



결과는 다음과 같다.


xxxxxxxx {"query":{"query_string":{"query":"coldplay"}}}


Posted by '김용환'
,



elasticsearch의 connection pool을 만들고 slick이나 play2처럼 자원 객체를 열고 닫는 불편함을 줄이고 싶었다.


보통 java/spring을 사용한 경우라면 val connection = connectionPool.getObject() 한 후,

코드 작업 후에 connectionPool.returnObjejct()를 호출해서 자원을 반환하는 구조를 사용하는 것이 일반적인 패턴이다. (사실 이게 귀찮으면 interceptor(asepectJ)를 사용하기도 한다)




다음은 es pool 예제이다. apache pool과 elastic4s(https://github.com/sksamuel/elastic4s)를 사용했다. 

object ElasticsearchPoolManager {
val poolConfig: ElasticsearchPoolConfig = new ElasticsearchPoolConfig()
lazy val pool: ElasticsearchPool = new ElasticsearchPool(poolConfig, "googles")

def getObject(): ElasticsearchConnectionSource = {
pool.getResource()
}

def returnObject(esSource: ElasticsearchConnectionSource) = {
pool.returnResource(esSource)
}

def withES[A](block: ElasticsearchConnectionSource => A): A = {
val source = getObject()
try { block(source) }
finally { returnObject(source)}
}
}



withES를 사용함으로서 코드 블록을 감싸게 했고.. 자동으로 getObject와 returnObject 코드를 사용하지 않도록 했다.


@Singleton
class ElasticsearchDAO {
val SIZE = 10
val INDEX_DEFULT_TYPE = "fluentd"
import com.sksamuel.elastic4s.http.ElasticDsl._

def getData(searchData: SearchData): Unit = {
import elasticsearch.pool.KemiElasticsearchPoolManager._
withES { es =>
val client = es.client
val result = client.execute {
search(searchData.serviceTag / INDEX_DEFULT_TYPE).query {
rangeQuery("@timestamp") .gte(searchData.startDateTime).lte(searchData.endDateTime)
}.size(SIZE).sortByFieldDesc("@timestamp")
}.await

result.hits.hits.foreach { println }
}
}
}



아.. 이전보다는 깔끔하다..






Posted by '김용환'
,


apache phoenix에서 테이블 정보(mysql의 경우 show tables)와 테이블 속성(mysql의 경우 describe table)을 보려면 !tables와 !descrie를 사용하면 되낟.



> !tables

+------------+--------------+------------------+---------------+----------+------------+----------------------------+-----------------+--------------+-+

| TABLE_CAT  | TABLE_SCHEM  |    TABLE_NAME    |  TABLE_TYPE   | REMARKS  | TYPE_NAME  | SELF_REFERENCING_COL_NAME  | REF_GENERATION  | INDEX_STATE  | |

+------------+--------------+------------------+---------------+----------+------------+----------------------------+-----------------+--------------+-+

|            | SYSTEM       | CATALOG          | SYSTEM TABLE  |          |            |                            |                 |              | |

|            | SYSTEM       | FUNCTION         | SYSTEM TABLE  |          |            |                            |                 |              | |

|            | SYSTEM       | SEQUENCE         | SYSTEM TABLE  |          |            |                            |                 |              | |

|            | SYSTEM       | STATS            | SYSTEM TABLE  |          |            |                            |                 |              | |



> !describe google.log

+------------+--------------+------------------+--------------+------------+------------+--------------+----------------+-----------------+------------+

| TABLE_CAT  | TABLE_SCHEM  |    TABLE_NAME    | COLUMN_NAME  | DATA_TYPE  | TYPE_NAME  | COLUMN_SIZE  | BUFFER_LENGTH  | DECIMAL_DIGITS  | NUM_PREC_R |

...

Posted by '김용환'
,



double transformation을 무엇이라 번역할까 했는데..


http://www.showme.com/sh/?h=wVJLsSu


이 동영상을 보니.


역 변형이 올바른 번역인 듯 하다.



Posted by '김용환'
,