{"id":2583,"date":"2015-05-17T14:39:05","date_gmt":"2015-05-17T12:39:05","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=2583"},"modified":"2015-05-17T15:39:08","modified_gmt":"2015-05-17T13:39:08","slug":"import-mysql-database-with-apache-solr-delta-import","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=2583","title":{"rendered":"Loading data from Mysql to Solr with a Data Import Handler"},"content":{"rendered":"<p>Solr is used not as a primary data store because it is a Search Platform whose primary purpose is giving the ability to do complex searches with blazing performance. This means that you usually have your data in a primary data store, like mysql Server and you need to move data to a Solr server to power up your searches.<\/p>\n<p>I am using SolR 4.9.1 under windows vista with wampserver for this tutorial with a single core collection1 &#8230;.<\/p>\n<p>In our case we used MySQL so we added in the <strong>contrib \/ dataimporthandler \/ lib<\/strong> :<br \/>\nmysql-connector-java-5.1.32-bin.jar<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/mysql-jar.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2584\" alt=\"mysql-jar\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/mysql-jar.jpg\" width=\"589\" height=\"315\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/mysql-jar.jpg 876w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/mysql-jar-300x160.jpg 300w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Nota Bene : If you were using <strong>SQL Serve<\/strong>r then you will need to\u00a0 add <strong>sqljdbc4.jar<\/strong> that contains classes needed to connect to a SqlServer database from java jdbc.<\/p>\n<p>First step is to use the sample mysql database used by Apache SolR tutorials (let us start wampserver and create a database called test with root user an without password :<\/p>\n<pre>CREATE TABLE `item` (\r\n`ID` int(11) NOT NULL AUTO_INCREMENT,\r\n`NAME` varchar(450) DEFAULT NULL,\r\n`MANU` varchar(450) DEFAULT NULL,\r\n`WEIGHT` float DEFAULT NULL,\r\n`PRICE` float DEFAULT NULL,\r\n`POPULARITY` int(11) DEFAULT NULL,\r\n`INSTOCK` tinyint(4) DEFAULT NULL,\r\n`INCLUDES` varchar(450) DEFAULT NULL,\r\n`last_modified` TIMESTAMP,\r\nPRIMARY KEY (`ID`)\r\n);\r\n\r\nCREATE TABLE `category` (\r\n`id` int(11) NOT NULL AUTO_INCREMENT,\r\n`description` varchar(450) DEFAULT NULL,\r\n`last_modified` TIMESTAMP,\r\nPRIMARY KEY (`id`)\r\n);\r\n\r\nCREATE TABLE `feature` (\r\n`id` int(11) NOT NULL AUTO_INCREMENT,\r\n`description` varchar(450) DEFAULT NULL,\r\n`item_id` int(11) DEFAULT NULL,\r\n`last_modified` TIMESTAMP,\r\nPRIMARY KEY (`id`),\r\nKEY `fk_feature_1` (`item_id`),\r\nCONSTRAINT `fk_feature_1` FOREIGN KEY (`item_id`) REFERENCES `item` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION\r\n);\r\n\r\nCREATE TABLE `item_category` (\r\n`id` int(11) NOT NULL AUTO_INCREMENT,\r\n`CATEGORY_ID` int(11) DEFAULT NULL,\r\n`item_id` int(11) DEFAULT NULL,\r\n`last_modified` TIMESTAMP,\r\nPRIMARY KEY (`id`),\r\nKEY `fk_item_category_1` (`CATEGORY_ID`),\r\nKEY `fk_item_category_2` (`item_id`),\r\nCONSTRAINT `fk_item_category_1` FOREIGN KEY (`CATEGORY_ID`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,\r\nCONSTRAINT `fk_item_category_2` FOREIGN KEY (`item_id`) REFERENCES `item` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION\r\n);<\/pre>\n<p>Adding some Sample Data in MySQL:<\/p>\n<pre>insert into item values(id,'item 1','item 1 manu','1.2','100.23','1',1,'includes item 1',CURRENT_TIMESTAMP);\r\n\r\ninsert into feature values(id,'feature item 1',1,CURRENT_TIMESTAMP);\r\ninsert into category values(id,'music',CURRENT_TIMESTAMP);\r\ninsert into item_category values(id,1,1,CURRENT_TIMESTAMP);<\/pre>\n<p>After that we have to prepare collection xml config file :<br \/>\n<b>in solrconfig.xml add (windows)<\/b><\/p>\n<pre>..\r\n\r\n&lt;lib dir=\"C:\\solr\\contrib\\dataimporthandler\\lib\\\" regex=\".*\\.jar\" \/&gt;\r\n&lt;lib dir=\"C:\\solr\\dist\\\" regex=\"solr-dataimporthandler-\\d.*\\.jar\" \/&gt;\r\n\r\n&lt;requestHandler name=\"\/dataimport\" class=\"org.apache.solr.handler.dataimport.DataImportHandler\"&gt;\r\n    &lt;lst name=\"defaults\"&gt;\r\n          &lt;str name=\"config\"&gt;db-data-config.xml&lt;\/str&gt;\r\n      &lt;\/lst&gt;\r\n&lt;\/requestHandler&gt;    \r\n...<\/pre>\n<p>Now create in config collection1 folder : <strong>db-data-config.xml<\/strong> (From Solr version 4.4 and +++). This Data Import configuration file will specify how you want to import data inside your Solr.<\/p>\n<pre>&lt;dataConfig&gt;\r\n    &lt;dataSource driver=\"com.mysql.jdbc.Driver\" type=\"JdbcDataSource\"\r\n       url=\"jdbc:mysql:\/\/localhost:3306\/test\" user=\"root\" password=\"\" \/&gt;\r\n&lt;document&gt;\r\n&lt;!-- this entity is the 'root' entity. --&gt;\r\n    &lt;entity name=\"item\" query=\"select * from item\" deltaQuery=\"select id from item where last_modified &gt; '${dataimporter.last_index_time}'\"&gt;\r\n\t&lt;field column=\"NAME\" name=\"name\" \/&gt;\r\n\t&lt;!-- This entity is nested and reflects the one-to-many relationship between an item and its multiple features. Note the use of variables; ${item.ID} is the value of the column 'ID' for the\r\n\tcurrent item ('item' referring to the entity name) --&gt;\r\n\t&lt;entity name=\"feature\"\r\n\t\tquery=\"select DESCRIPTION from FEATURE where ITEM_ID='${item.ID}'\" deltaQuery=\"select ITEM_ID from FEATURE where last_modified &gt; '${dataimporter.last_index_time}'\"\r\n\t\tparentDeltaQuery=\"select ID from item where ID=${feature.ITEM_ID}\"&gt;\r\n\t&lt;field name=\"features\" column=\"DESCRIPTION\" \/&gt;\r\n\t&lt;\/entity&gt;\r\n\r\n\t&lt;entity name=\"item_category\"\r\n\t\tquery=\"select CATEGORY_ID from item_category where ITEM_ID='${item.ID}'\"\r\n\t\tdeltaQuery=\"select ITEM_ID, CATEGORY_ID from item_category where last_modified &gt; '${dataimporter.last_index_time}'\"\r\n\t\tparentDeltaQuery=\"select ID from item where ID=${item_category.ITEM_ID}\"&gt;\r\n\t\t&lt;entity name=\"category\" query=\"select DESCRIPTION from category where ID = '${item_category.CATEGORY_ID}'\"\r\n\t\t\tdeltaQuery=\"select ID from category where last_modified &gt; '${dataimporter.last_index_time}'\"\r\n\t\t\tparentDeltaQuery=\"select ITEM_ID, CATEGORY_ID from item_category where CATEGORY_ID=${category.ID}\"&gt;\r\n\t\t\t&lt;field column=\"description\" name=\"cat\" \/&gt;\r\n\t\t&lt;\/entity&gt;\r\n\t&lt;\/entity&gt;\r\n    &lt;\/entity&gt;\r\n&lt;\/document&gt;\r\n&lt;\/dataConfig&gt;<\/pre>\n<p>In case it was an sql server, I like to show a more simple sample DIH handler :<\/p>\n<pre>\r\n&lt;dataConfig&gt;  \r\n    &lt;dataSource type=\"JdbcDataSource\"\r\n            driver=\"com.microsoft.sqlserver.jdbc.SQLServerDriver\"\r\n            url=\"jdbc:sqlserver:\/\/10.0.10.104;databaseName=thesaurus;\"\r\n            user=\"sa\"\r\n            password=\"zzzzzz\"\r\n            batchSize=\"5\" \/&gt; \r\n             \r\n    &lt;document name=\"TestDocument\"&gt;  \r\n        &lt;entity name=\"TestEntity\" query=\"SELECT * FROM tag\"&gt;  \r\n            &lt;field column=\"Id\" name=\"id\" \/&gt;\r\n            &lt;field column=\"Term\" name=\"term\" \/&gt;  \r\n            &lt;field column=\"Name\" name=\"name\" \/&gt;  \r\n        &lt;\/entity&gt;  \r\n    &lt;\/document&gt;  \r\n&lt;\/dataConfig&gt; \r\n<\/pre>\n<p>All the tests after insert or update will be queried under Admin SolR UI \/ collection1 &#8230;<\/p>\n<p>Before we continue, Here are some useful URLs for Import \/ indexing MySQL database with Apache Solr &#8211; Delta Import to make the tests more quicker :<\/p>\n<pre>Clear or delete Solr index: http:\/\/localhost:8983\/solr\/update?stream.body=&lt;delete&gt;&lt;query&gt;*:*&lt;\/query&gt;&lt;\/delete&gt;&amp;commit=true\r\nRetrieve all: http:\/\/localhost:8983\/solr\/select?q=*:*&amp;omitHeader=true\r\nIndex db: http:\/\/localhost:8983\/solr\/collection1\/dataimport?command=full-import\r\n\r\nhttp:\/\/localhost:8983\/solr\/collection1\/dataimport?command=full-import&clean=false\r\nReload core: http:\/\/localhost:8983\/solr\/admin\/cores?action=RELOAD&amp;core=collection1\r\nQuery for word Sample: http:\/\/localhost:8983\/solr\/select?q=Sample&amp;wt=json&amp;qf=first_name%20last_name&amp;defType=edismax<\/pre>\n<p>For indexing the whole database use :<\/p>\n<pre>http:\/\/localhost:8983\/solr\/collection1\/dataimport?command=full-import<\/pre>\n<p>Now let us test SolR delta import by updating a row in items mysql table :<\/p>\n<pre>update item set `last_modified` = CURRENT_TIMESTAMP , price=91 where `ID` =1;\r\nupdate users set `last_modified` = CURRENT_TIMESTAMP , user_name='Haddad' where `user_id` =1;\r\n<\/pre>\n<p>and let us do solr delta import with our dih :<\/p>\n<pre>http:\/\/localhost:8983\/solr\/collection1\/dataimport?command=delta-import&amp;optimize=false<\/pre>\n<p>It is important to note that some problems may occur with delta import if the server time is not set properly or on ID in DIH if the proper version of Solr is not the good one<br \/>\nDIH file for version lucene 1.4 is slightly different from 4.4 +++ versions<\/p>\n<p><span style=\"text-decoration: underline;\"><strong><em>Import data with standard Admin UI solr web interface<\/em><\/strong><\/span><br \/>\n<a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/solr_admin_ui.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2605\" alt=\"solr_admin_ui\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/solr_admin_ui.png\" width=\"793\" height=\"509\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/solr_admin_ui.png 1000w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2015\/05\/solr_admin_ui-300x192.png 300w\" sizes=\"(max-width: 793px) 100vw, 793px\" \/><\/a><\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solr is used not as a primary data store because it is a Search Platform whose primary purpose is giving the ability to do complex searches with blazing performance. This means that you usually&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2847,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[340],"tags":[341],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2583"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2583"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2847"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}