{"id":935,"date":"2020-06-20T17:16:08","date_gmt":"2020-06-20T08:16:08","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=935"},"modified":"2020-06-30T16:12:22","modified_gmt":"2020-06-30T07:12:22","slug":"jdbc-%eb%a5%bc-%ec%9d%b4%ec%9a%a9%ed%95%9c-elasticsearch-rdbms-%ec%97%b0%eb%8f%99","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=935","title":{"rendered":"JDBC \ub97c \uc774\uc6a9\ud55c Elasticsearch RDBMS \uc5f0\ub3d9"},"content":{"rendered":"<h1>JDBC \ub97c \uc774\uc6a9\ud55c Elasticsearch RDBMS \uc5f0\ub3d9<\/h1>\n<p>Logstash \uc640 jdbc \ub97c \uc774\uc6a9\ud574 Elasticsearch RDBMS \uc0ac\uc774\uc758 \ub370\uc774\ud0c0\ub97c \ub3d9\uae30\ud654 \ud569\ub2c8\ub2e4.<\/p>\n<p>\uc6b4\uc601\ud658\uacbd\uc5d0\uc11c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c\ub294 <code>Bulk Insert<\/code> \ub97c \ubcd1\ud589\ud574\uc11c \uc0ac\uc6a9\ud574\uc57c \ud558\uc9c0\ub9cc \ubb38\uc11c\uc758 \uc591\uc744 \uc904\uc774\uae30 \uc704\ud574 \uc0dd\ub7b5\ud569\ub2c8\ub2e4.<\/p>\n<p>\ub610, JDK, ES, Logstash, MySQL \uc758 \uc124\uce58\ub294 \uc0dd\ub7b5\ud569\ub2c8\ub2e4.<\/p>\n<h2>\ucc38\uc870 \uc0ac\uc774\ud2b8<\/h2>\n<ul>\n<li><a href=\"https:\/\/peung.tistory.com\/m\/13\">https:\/\/peung.tistory.com\/m\/13<\/a><\/li>\n<li><a href=\"https:\/\/www.elastic.co\/kr\/blog\/how-to-keep-elasticsearch-synchronized-with-a-relational-database-using-logstash\">https:\/\/www.elastic.co\/kr\/blog\/how-to-keep-elasticsearch-synchronized-with-a-relational-database-using-logstash<\/a><\/li>\n<\/ul>\n<h2>MySQL \uc124\uc815<\/h2>\n<pre><code class=\"language-sql\">CREATE DATABASE db_test DEFAULT CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI;\n\nUSE db_test;\n\nDROP TABLE IF EXISTS items;\n\nCREATE TABLE items (\n    itemid BIGINT(20) UNSIGNED NOT NULL,\n    PRIMARY KEY (itemid),\n    UNIQUE KEY unique_id (itemid),\n    itemname VARCHAR(512) NOT NULL,\n    category VARCHAR(128) NULL,\n    price DECIMAL(13, 4) NULL,\n    lastupdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n    regdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n\nINSERT INTO items (itemid, itemname) VALUES (1, &#039;\ube14\ub799 \uc544\uc774\ud3f0 \ucf00\uc774\uc2a4&#039;);<\/code><\/pre>\n<p><code>itemid<\/code> \uac00 PK \ub85c ES \uc758 <code>document id<\/code> \uc640 \ub9e4\uce6d\ub429\ub2c8\ub2e4. lastupdate \uac00 \ubcc0\uacbd\ub41c \ub0b4\uc5ed\ub9cc \ub3d9\uae30\ud654\ub429\ub2c8\ub2e4.<\/p>\n<h2>Elasticsearch \uc124\uc815<\/h2>\n<p>\uc778\ub371\uc2a4\ub294 \uc0dd\uc131\ud574 \uc8fc\uc9c0 \uc54a\uc544\ub3c4, Logstash \uac00 \uc790\ub3d9\uc0dd\uc131\ud558\uc9c0\ub9cc \uc218\uae30\ub85c \uc0dd\uc131\ud574\uc8fc\ub294 \uac83\uc774 \uc548\uc804\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-bash\">curl -XDELETE http:\/\/localhost:9200\/items?pretty\n\ncurl -XPUT http:\/\/localhost:9200\/items?pretty -H &#039;Content-Type: application\/json&#039; -d &#039;{\n  &quot;settings&quot; : {\n    &quot;number_of_shards&quot;: &quot;5&quot;,\n    &quot;number_of_replicas&quot;: &quot;1&quot;,\n    &quot;index&quot;:{\n      &quot;analysis&quot;:{\n        &quot;analyzer&quot;:{\n          &quot;korean&quot;:{\n            &quot;type&quot;:&quot;custom&quot;,\n            &quot;tokenizer&quot;:&quot;seunjeon_default_tokenizer&quot;\n          }\n        },\n        &quot;tokenizer&quot;: {\n          &quot;seunjeon_default_tokenizer&quot;: {\n            &quot;index_eojeol&quot;: &quot;true&quot;,\n            &quot;user_words&quot;: [\n              &quot;\ud150\ubc14\uc774\ud150&quot;, &quot;\uc5e0\ub514&quot;, &quot;\uc5d0\uc5b4\ud31f&quot;, &quot;\ub7ec\ube0c\ud50c\ub77c\ubcf4&quot;, &quot;\uc530\uc2a4\ud2f0\ucee4&quot;,\n              &quot;1+1&quot;\n            ],\n            &quot;index_poses&quot;: [\n                &quot;UNK&quot;, &quot;EP&quot;, &quot;I&quot;, &quot;J&quot;, &quot;M&quot;,\n                &quot;N&quot;, &quot;SL&quot;, &quot;SH&quot;, &quot;SN&quot;, &quot;VCP&quot;,\n                &quot;XP&quot;, &quot;XS&quot;, &quot;XR&quot;\n            ],\n            &quot;decompound&quot;: &quot;true&quot;,\n            &quot;type&quot;: &quot;seunjeon_tokenizer&quot;\n          }\n        }\n      }\n    }\n  },\n  &quot;mappings&quot; : {\n    &quot;_doc&quot; : {\n      &quot;properties&quot; : {\n        &quot;@timestamp&quot; : {\n          &quot;type&quot; : &quot;date&quot;\n        },\n        &quot;itemid&quot; : {\n          &quot;type&quot; : &quot;integer&quot;\n        },\n        &quot;itemname&quot; : {\n          &quot;type&quot; : &quot;text&quot;,\n          &quot;analyzer&quot;: &quot;korean&quot;\n        },\n        &quot;category&quot; : {\n          &quot;type&quot; : &quot;text&quot;,\n          &quot;analyzer&quot;: &quot;korean&quot;\n        },\n        &quot;price&quot; : {\n          &quot;type&quot;: &quot;scaled_float&quot;,\n          &quot;scaling_factor&quot;: 10000\n        },\n        &quot;lastupdate&quot; : {\n          &quot;type&quot; : &quot;date&quot;\n        },\n        &quot;regdate&quot; : {\n          &quot;type&quot; : &quot;date&quot;\n        }\n      }\n    }\n  }\n}&#039;<\/code><\/pre>\n<h2>Logstash \uc124\uc815<\/h2>\n<p>\ub85c\uadf8\uc2a4\ud0dc\uc2dc \ub370\ubaac\uc774 \uc2a4\ucf00\uc904\uc5d0 \ub9de\ucdb0 jdbc \ub97c \ud638\ucd9c\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-bash\">sudo mkdir \/last_run\nsudo chown logstash:logstash \/last_run\nsudo vi \/etc\/logstash\/conf.d\/logstash-jdbc-es.conf<\/code><\/pre>\n<p>\ub3d9\uae30\ud654 \ud574\uc57c\ud560 \uc778\ub371\uc2a4\uac00 \uc5ec\ub7ec \uac1c\uc77c \uacbd\uc6b0 <code>last_run_metadata_path<\/code> \ub97c \uc9c0\uc815\ud574 \uc2a4\ucf00\uc904\ub9c8\ub2e4 \uac01\uac01 <code>sql_last_value<\/code> \ub97c \uc800\uc7a5\ud560 \uc218 \uc788\uac8c \ud574\uc8fc\uc5b4\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-configuration\">input {\n    jdbc {\n        jdbc_driver_library =&gt; &quot;\/home\/ubuntu\/mysql-connector-java-8.0.20.jar&quot;\n        jdbc_driver_class =&gt; &quot;com.mysql.jdbc.Driver&quot;\n        jdbc_connection_string =&gt; &quot;jdbc:mysql:\/\/localhost:3306\/db_test?zeroDateTimeBehavior=convertToNull&amp;serverTimezone=Asia\/Seoul&quot;\n        jdbc_user =&gt; &quot;testuser&quot;\n        jdbc_password =&gt; &quot;test1234&quot;\n        jdbc_paging_enabled =&gt; true\n        jdbc_page_size =&gt; 100000\n        tracking_column =&gt; &quot;unix_ts_in_secs&quot;\n        use_column_value =&gt; true\n        tracking_column_type =&gt; &quot;numeric&quot;\n        schedule =&gt; &quot;*\/5 * * * * *&quot;\n        statement =&gt; &quot;SELECT *, UNIX_TIMESTAMP(lastupdate) AS unix_ts_in_secs FROM items WHERE (UNIX_TIMESTAMP(lastupdate) &gt; :sql_last_value AND lastupdate &lt; NOW()) ORDER BY lastupdate ASC&quot;\n        last_run_metadata_path =&gt; &quot;\/last_run\/logstash-jdbc-es&quot;\n    }\n}\n\nfilter {\n    mutate {\n        copy =&gt; { &quot;itemid&quot; =&gt; &quot;[@metadata][_id]&quot;}\n        remove_field =&gt; [&quot;@version&quot;, &quot;unix_ts_in_secs&quot;]\n    }\n}\n\noutput {\n    # stdout { codec =&gt;  &quot;rubydebug&quot;}\n    elasticsearch {\n        hosts =&gt; [&quot;localhost:9200&quot;]\n        index =&gt; &quot;items&quot;\n        document_id =&gt; &quot;%{[@metadata][_id]}&quot;\n        document_type =&gt; &quot;_doc&quot;\n    }\n}<\/code><\/pre>\n<h2>\ud14c\uc2a4\ud2b8\ud558\uae30<\/h2>\n<pre><code class=\"language-sql\">use db_test;\nupdate items set itemname = &#039;\ube14\ub799 \uc544\uc774\ud3f0 \ucf00\uc774\uc2a41&#039; where itemid = 1;<\/code><\/pre>\n<pre><code class=\"language-bash\">curl -XGET http:\/\/localhost:9200\/items\/_search?pretty\ncurl -XGET http:\/\/localhost:9200\/items\/doc\/1?pretty\n\ncurl -XGET http:\/\/localhost:9200\/items\/_search?pretty -H &#039;Content-Type: application\/json&#039; -d &#039;{\n  &quot;query&quot;: {\n    &quot;match&quot;: {\n      &quot;itemname&quot;: &quot;\uc544\uc774\ud3f0&quot;\n    }\n  }\n}&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JDBC \ub97c \uc774\uc6a9\ud55c Elasticsearch RDBMS \uc5f0\ub3d9 Logstash \uc640 jdbc \ub97c \uc774\uc6a9\ud574 Elasticsearch RDBMS \uc0ac\uc774\uc758 \ub370\uc774\ud0c0\ub97c \ub3d9\uae30\ud654 \ud569\ub2c8\ub2e4. \uc6b4\uc601\ud658\uacbd\uc5d0\uc11c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c\ub294 Bulk Insert \ub97c \ubcd1\ud589\ud574\uc11c \uc0ac\uc6a9\ud574\uc57c \ud558\uc9c0\ub9cc \ubb38\uc11c\uc758 \uc591\uc744 \uc904\uc774\uae30 \uc704\ud574 \uc0dd\ub7b5\ud569\ub2c8\ub2e4. \ub610, JDK, ES, Logstash, MySQL \uc758 \uc124\uce58\ub294 \uc0dd\ub7b5\ud569\ub2c8\ub2e4. \ucc38\uc870 \uc0ac\uc774\ud2b8 https:\/\/peung.tistory.com\/m\/13 https:\/\/www.elastic.co\/kr\/blog\/how-to-keep-elasticsearch-synchronized-with-a-relational-database-using-logstash MySQL \uc124\uc815 CREATE DATABASE db_test DEFAULT CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI; USE\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=935\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-elasticsearch"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=935"}],"version-history":[{"count":22,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions"}],"predecessor-version":[{"id":1062,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions\/1062"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}