æ±äº¬ã¡ããã®ãªã¼ãã³ãã¼ã¿ãElasticsearch + Kibana (5.0.2)ã§å¯è¦åãã¦ã¿ãï¼Java Clientï¼
JJUG CCC 2016 fallãã¼ã¹ãã¼ã¯ã§å¤§è°·ãããElastic Advent Calendaræ ã¾ã ããã¾ãã£ã¦è¨ã£ã¦ã¦ãå¢ãã§ç»é²ãã¦ãã¾ã£ãã
å®ã¯æéããã¿ããªãã£ããã¨ã«å¾ããæ°ä»ãã¤ã¤éæ»ã§ãªãããã£ã¦ããã¾ãï¼
3æéãããã§ãªãã¨ããªã£ãããããªï¼
ä½ããããµã³ãã«ãã¼ã¿ãªãããªã¼
é»è»ã®é
延å¾åã¨ãããã¨ã°ã©ãã§è¦ããããããªã¼ã¨æãã¤ãã
以åã©ããã§èããæ°ããã¦èª¿ã¹ã¦è¦ã¤ããã®ãæ±äº¬ã¡ããã®ãªã¼ãã³ãã¼ã¿ã§ãã
æ±äº¬ã¡ããã®è·¯ç·æ
å ±ãé§
æ
å ±ãAPIã§åç
§ã§ãã¾ãã
APIãå©ç¨ããããã«ã¯éçºè
ç»é²ãããã¨æ大2å¶æ¥æ¥ã»ã©ã§ã¢ã¯ã»ã¹ãã¼ã¯ã³ãåå¾ã§ããã¨ã®ãã¨ã ã£ããã§ããèªåããã£ããåæ¥ã§ç»é²ãã¦ããã¾ããããããããã
APIã¯è·¯ç·IDã§è·¯ç·ãæ¤ç´¢ãããLat Lonã§é§
æ¤ç´¢ãããããããã§ãããã§ããã
ã¨ããããå
¨è·¯ç·ã®ç¾å¨ç¶æ³ãç¥ããããã°
https://api.tokyometroapp.jp/api/v2/datapoints?rdf:type=odpt:Train&acl:consumerKey=<ã¢ã¯ã»ã¹ãã¼ã¯ã³>
ãå©ãã¦ããã°å¤§ä¸å¤«ã§ããå
¬å¼ããã¥ã¡ã³ãã¯ãªãã ãã¡ãã£ã¨çµæ§é£ããæ¸ãã¦ããã¾ããHTTPãªã¯ã¨ã¹ãã§JSON arrayãè¿ã£ã¦ããã ãã§ãã
ã¬ã¹ãã³ã¹é
ç®ã¯éçºè
ç»é²å¾ã«èªããããã¥ã¡ã³ãã«ããã¾ãããããèªã¾ãªãã¦ãã¨ããããå¯è¦åããã°ãªã«ãããæãã§ãããã§ãããï¼ãããã¨ãããªï¼
Elasticsearchã®ã»ããã¢ãã
ä»åã®ç®æ¨ã¯å¯è¦åã§ãããµã¼ããã¤ã³ã¹ã¿ã³ã¹ãæºåããæã¯ãªãã®ã§èªåã®macã§æ§ç¯ãã¾ãã
èªåã¯ä»¥å2.3ç³»ã®elasticsearchã使ã£ã¦ãããã¨ããã£ãã®ã§ãããä»ã¯5ç³»ï¼ä½æ
ã3ç³»4ç³»ãã¨ãã ï¼ï¼ãåºã¦ããã®ãããã®ã§ãã£ããã ããæ°ãããã¤ã使ã£ã¦ã¿ã¾ãã
https://www.elastic.co/jp/downloads/elasticsearch
ãã¦ã³ãã¼ããã¼ã¸ãè¦ãã¨3ã¹ãããæ¸ãã¦ããã¾ãã
- ææ°ã®Elasticsearchããã¦ã³ãã¼ããã¦å±éãã
- ./bin/elasticsearchãå®è¡ãã
- curl -X GET http://localhost:9200/ ãå®è¡
è¶
ã«ã³ã¿ã³ã§ããï¼
ã§ã¯æ©éMacã®é©å½ãªãã£ã¬ã¯ããªã§ä½æ¥ãã¦ããã¾ãã
$ wget "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.2.tar.gz" $ tar xvzf elasticsearch-5.0.2.tar.gz $ cd elasticsearch-5.0.2 $ ./bin/elasticsearch
ãããããã°ãåºã¦èµ·åã§ããæãã§ãã
ãã©ã¦ã¶ã§ http://localhost:9200/ ã«ã¢ã¯ã»ã¹ããã¨ã¯ã©ã¹ã¿æ
å ±ãJSONã§è¡¨ç¤ºããã¾ãããæ¬å½ã«1åã§ã§ãã¡ããã¾ããã
// ã²ã¨ã㨠headã¨ãããelasticsearchã®ã¤ã³ããã¯ã¹ç¶æ³ãªã©ã詳細ã«Web UIã§è¡¨ç¤ºãã¦ãããã決ã¾ãã®ãã©ã°ã¤ã³ãããã¾ãã¦ã site pluginã¨ããæ©è½ã§5ç³»ã®åã¾ã§ã¯elasitcsearchã«çµã¿è¾¼ãã§èµ·åããã¨ãããã¨ããããã£ã¦ããã®ã§ãã 5ç³»ã§site pluginããªããªã£ãããã§ãããã headã使ããªãããã§ã¯ãªããä»ã¯elasticsearchã®å¤ããèµ·åã§ããããã«ãªã£ãæãã ã¨æãã¾ãã // ãµãã㨠ä»åã¯åç¬Macèµ·åãªã®ã§è§¦ãã¾ãããã¹ã±ã¼ã«ã¢ã¦ããç°¡åã«ã§ãã¾ãã åºæ¬åããããã¯ã¼ã¯å ã§ããã°åãã¯ã©ã¹ã¿åã§elasticsearchãèµ·åãã¦ã·ã£ã¼ãæ°ãå¢ããã°åæã«åæ£ãã¾ãã
Kibanaã®ã»ããã¢ãã
ãã¦ã次ã¯Kibanaã§ãã https://www.elastic.co.jp/downloads/kibana MACç¨ã®tar ballãããã¾ããã ã¹ããããè¦ãã¨elasticsearchã»ã©ã¯ã·ã³ãã«ã§ãªãã§ãããããã¨ã¯ãã5åã§çµãããã
- Kibana 4ããã¦ã³ãã¼ããã¦å±éãã
- ã¢ã¼ã«ã¤ãã®æ½åºãã¨ãã£ã¿ã§config/kibana.ymlãéããElasticsearchã¤ã³ã¹ã¿ã³ã¹ãåç §ããããã«elasticsearch.urlãè¨å®ããã./bin/kibanaãå®è¡ãã
- ãã©ã¦ã¶ã§ http://yourhost.com:5601 ãéã
ãããstableã®ãã¼ã¸ã§ã³ã¯5.0.2ã¨æ¸ãã¦ããã¾ããã¹ãããã§ã¯Kibana 4ã«ãªã£ã¦ã¾ãããã¾ãæ°ã«ããé²ãã¾ãããã
$ wget "https://artifacts.elastic.co/downloads/kibana/kibana-5.0.2-darwin-x86_64.tar.gz" $ tar xvzf kibana-5.0.2-darwin-x86_64.tar.gz $ cd kibana-5.0.2-darwin-X86_64 $ ./bin/kibana
èµ·åã§ããããã§ãã
ãã©ã¦ã¶ã§ http://localhost:5601 ã«ã¢ã¯ã»ã¹ããã¨kibanaã®ãªãããªUIã表示ããã¾ããã
ã¹ãããâ¡ã®kibana.ymlã®ç·¨éãé£ã°ãã¾ãããåé¡ããã¾ããã§ãããããã©ã«ãã§elasticsearchã¯localhost:9200ã«æ¥ç¶ãã«ããããã«ãªã£ã¦ãããã§ããã親åã
// ã²ã¨ã㨠æ°è¦æ§ç¯æã«ã¯ãã¾ãåé¡ã«ãªããªãã®ã§ããElasticsearchã¨Kibanaã®é£æºå¯è½ãã¼ã¸ã§ã³ã¯éããã¦ãã¾ãã Elasticsearchããã¼ã¸ã§ã³ã¢ãããããKibanaãå¤ãã¦è¦ããªããªã£ãã¨ãã éã«Kibanaããã¼ã¸ã§ã³ã¢ãããããElasticsearchãå¤ãã¦ãã¾ããããªãã¨ããããã¡ã ã¨æãã¾ãã ãã¼ã¸ã§ã³ã¢ããæã¯åºæ¬çã«ã¯ä¸¡æ¹ææ°ã«ãã¦ããã¾ãããã
Java Clientéçº
ã¡ããAPIãå¼ãã¦çµæJSONãelasticsearchã«æå
¥ããJavaã³ã¼ããæ¸ãã¦ããã¾ãã
以å使ã£ã¦ããæã¯HTTP POSTã§indexãã¦ããã®ã§elasticsearchã®Java Clientã使ãã®ã¯åã¨ãªãã¾ãã
âã®ãã¼ã¸ãè¦ãªããé²ãã¦ããã¾ãã
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
maven pomã§æå°éå¿
è¦ãªdependencyã¯elasticsearch transportã¨log4jã§ãã
èªåã§ã¯å®éã«ã¯ä»ã«ãããããã¦ã¼ãã£ãªãã£ã©ã¤ãã©ãªãã¤ã£ãã¿ã¾ããï¼Apache Commonã¨ãHTTP Clientã¨ãJSON simpleã¨ãï¼ã
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6.2</version> </dependency>
ã§éçºããã³ã¼ãã¹ãããããããã§ãã
JSONParser parser = new JSONParser(); // ã¡ããAPIãHTTP GETãã¦è¿ã£ã¦ããJSON arrayãJSONArrayãªãã¸ã§ã¯ãã¨ãã¦è¿ãæãã®é¢æ° public JSONArray getMetroAPI() throws IOException, ParseException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://api.tokyometroapp.jp/api/v2/datapoints?rdf:type=odpt:Train&acl:consumerKey=<ã¢ã¯ã»ã¹ãã¼ã¯ã³>"); CloseableHttpResponse apiResponse = httpclient.execute(httpGet); String jsonStr = IOUtils.toString(apiResponse.getEntity().getContent(), Charset.forName("UTF-8")); JSONArray arr = (JSONArray) parser.parse(jsonStr); apiResponse.close() return arr; } @Test public void test() throws IOException, ParseException { // ããã¨ããããelasticsearchã®ãã¹ãæ å ±ãæ¸ã. // TransportAddress add = new InetSocketTransportAddress(InetAddress.getLocalHost(), 9300); // ããã¯ãªããæ¥ç¶ã§ããªãã£ã. ãã¤ã³ãããã¦ããã®ãããã©ã«ãã ã¨ã«ã¼ãããã¯ã¢ãã¬ã¹ã ããªã®ãã TransportAddress add = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300); // ããã©ã«ãã ã¨Java Clientã«å¯¾ãããªãã¹ã³ãã¼ãã¯9200ã§ã¯ãªã9300ã TransportClient cli = new PreBuiltTransportClient(Settings.EMPTY) // ã¯ã©ã¹ã¿åãããã©ã«ãã§ãªããã°æå®ããªããã°ãã¡ .addTransportAddress(add); for (Object json : getMetroAPI()) { IndexResponse response = cli.prepareIndex("train", "info") // 第ä¸å¼æ°ãã¤ã³ããã¯ã¹å, 第äºå¼æ°ãã¿ã¤ãå. ãããããã¼ã¿ãã¼ã¹å, ãã¼ãã«åã¿ãããªãã®. å°æåãããªãã¨ãã¡. .setSource(json.toString()).get(); } }
ããã§ã¤ã³ããã¯ã¹ã§ãã¾ããã
ããã§getMetroAPIã¡ã½ããã¯éè¦ã§ã¯ãªãã§ãããã URLã«å¯¾ãã¦HTTP GETãã¦JSON Arrayãåå¾ãã¦ããã ãã§ãã
testã¡ã½ããå
ã§cli.prepareIndex(...).setSource(ãªã«ã).get();
ãã¦ããã¨ãããå®éã«elasticsearchã«ã¤ã³ããã¯ã¹ãã¦ããç®æã§ãã
setSourceã®å¼æ°ã§ã¤ã³ããã¯ã¹ããå
容ãå
¥ãã¾ãããJSONæååã ãã§ãªãMapãJacksonã·ãªã¢ã©ã¤ãºãããã¤ããªãé¸æã§ããããã§ãã
èªåã§ã³ã³ãã³ããããã£ã¦ã¤ã³ããã¯ã¹ãããããªå ´åã¯MapãJacksonã·ãªã¢ã©ã¤ãºã®æ¹ãè¯ãé¸æè¢ã§ããããä»åã¯å¤ããåå¾ããJSONããã®ã¾ã¾ã¤ã³ããã¯ã¹ããã®ã§JSONæååãé¸æãã¾ããã
ã¤ã³ããã¯ã¹åã¯é©å½ã«trainã¨ãã¾ãããäºåã«ã¤ã³ããã¯ã¹ãä½ã£ã¦ãããªãã¦ãåå¨ããªããã°elasticsearchãåæã«ä½ã£ã¦ããã¾ãã
ãããcronãªã©ã§å®æçã«å®è¡ããããã«ãã¦æ¾ç½®ãã¦ããã¾ãã
å¯è¦å
1æ¥ã ãããåããªãã£ãã®ã§å°ãªãæéã¨ãã¼ã¿ã§ãããelasticsearchã«ã¯ããããè·¯ç·æ
å ±ãã¤ã³ããã¯ã¹ãããã¯ãã§ãã
ãããå¯è¦åãã¦ããã¾ããKibanaã®Web UIã触ãã ãã§ãã
â»ãã¿ã¾ããããããã ãã ãé©å½ã«ãã
Kibanaã®æåã®ç»é¢ã¯Configure an index patternã§ãã
Index name or patternã¯ããã©ã«ãã§ã¯logstash-*ã¨æ¸ãã¦ããã¾ãããããã¯ãã°æå
¥æ©ã«logstashã¨ããelastic stackã使ããã¨ãæ³å®ãã¦ããããã§ãã
ä½ã£ãã¤ã³ããã¯ã¹ã¯"train"ãªã®ã§trainã¨æ¸ãã¾ãããã
æ®éã¤ã³ããã¯ã¹åã¯ãã¬ãã£ãã¯ã¹ã¨yyyyMMddçãçµã¿åããã¦ãã¤ã³ããã¯ã¹ãè¥å¤§åããããªãããã«ä¸å®æéæ¯ã«åºåã£ããããã®ã§ããä»åã¯ããã©ãããã®ã§"train"ã ãã§ãã
æå®ããã¨ã¤ã³ããã¯ã¹ãèªèãã¦Time-fieldã®æå®ãã§ããããã«ãªãã¾ãã
ä»åã®ãã°ã®å ´åã¿ã¤ã ã¹ã¿ã³ãåã®é
ç®ã¯2ã¤ããã¾ããããé
ç®ä»æ§ããdc:dateãé¸ãã§Createãã¿ã³ãæ¼ãã¾ããâã®ãããªæãã§ãã
ããã¨trainã¤ã³ããã¯ã¹ã®åãã£ã¼ã«ããèªèããã¦ä»¥ä¸ã®ãããªç»é¢ã«ãªãã¾ããã
次ã«åè·¯ç·ã®é
延ã°ã©ããä½ã£ã¦ã¿ã¾ãã
å·¦ã®ãã¼ã«ãã¼ã®Visualizeãã¯ãªãã¯ãã¦Line chartãé¸ã³ã¾ãã
Y軸ãX軸ãªã©ãæå®ã§ããç»é¢ã«ãªãã®ã§ãããªã§è¨å®ãã¦ããã¾ãã
Y軸ã¯é
延ç§ã表ãodpt:delayãCountã§ã¯ãªããSumã§ã
X軸ã¯Date Histogramã«ãã¦ã¤ã³ã¿ã¼ãã«ã¯éè¤ããªãããã«ããã¨ååºå®ã«ãã¾ãã
ããã«Y軸ã¯Sub AggregetionãTermsã«ããã£ã¼ã«ããè·¯ç·åã«æå®ãã¦åçãã¿ã³ã¿ãããªãã¤ãæ¼ãã¾ãã
APIã®å©ãæ¼ããªã©ã§ãã¾ãã¡ãªã°ã©ãã«ãªã£ã¦ãã¾ãã¾ãããä¸å¿è·¯ç·ãã¨ã®1æ¥ã®é
延æéãã°ã©ãåãããã¨ãã§ãã¾ããï¼
å°ä¸éã¯å¤ã®è·¯ç·ã¨éã£ã¦ãã¾ãé
延ããªãããªã¨æã£ã¦ããã®ã§ãããããã¦è¦ãã¨ãªãã ããã ã©ã®è·¯ç·ãã¡ãã£ã¨ã¯é
延ãã¦ã¾ãããç¹ã«ã©ãã·ã¥æéã
ãã®æ¥ã¯æãå代ç°ç·ãå¤æ¹ã¯é座ç·ã10-15åãããé
延ãã¦ãããã¨ããããã¾ããã
ããããã£ã¨é·æã§ã¨ã£ã¦ãé
延å¾åãããããªå´é¢ã§ã°ã©ãåãã¦ãããã«ãã£ãããããã·ã¥ãã¼ãåããã¨é¢ç½ããã§ããå¤ãæ´ãã¦ããã®ã§è¨äºã¯ãããããã«ãã¦ãããã¨æãã¾ãã
æå3æéã§ã§ããããããªã¨æ¸ãã¦ãã¾ãããJava Clientãããã§æéãæ¶è²»ãã3æéã¯å°ãå³ãããã®ãããã¾ããã
ã¨ã¯ããç°¡åã«ãã°å¯è¦åãã§ãããã¼ã«ã§ãããã¨ã¯ééããªãã¨æãã®ã§ã試ãããã