Loading data from MySQL

Solr's contrib provides the datahandlerimport module, and one of the examples in Solr is also focused on DataImportHandler, also known as DIH. Let's run the DIH example given by default. Hit solr -e dih to start Solr with the example of DIH. It will pick the configuration set in $solr_home/example/example-DIH. Create sample data in MySQL as follows:

Now it's time to integrate it into Solr. Follow these steps to find out the necessary configurations:

  1. As stated earlier, any Solr installation would require the solrconfig.xml file, which has the necessary config information. This file usually resides in $solr_home/<config_set_selected>/conf. As we have selected -e dih and we are specifically looking for the database utility, we will find our solrconfig.xml at $solr_home/example/example-DIH/solr/db/conf/solrconfig.xml. Add the following lines of code in this file.

Since we are going to use MySql, we need to download the MySQL connector JAR (http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.8-dmr/mysql-connector-java-8.0.8-dmr.jar), place it in the dist folder $solr_home/dist, and let Solr know where to find the connector JAR by adding the following lines of code. Make sure that the dataimporthandler module is available in dist:

<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*.jar" />
<lib dir="${solr.install.dir:../../../..}/contrib/extraction/lib" regex=".*.jar" />
<lib dir="${solr.install.dir:../../../..}/dist" regex="mysql-connector-java-d.*.jar" />
  1. Next, we need to tell Solr where our database configuration file is. Add the following lines of code and create a file db-data-config.xml parallel to solrconfig.xml:
<requestHandler name="/dataimport" class="solr.DataImportHandler">
<lst name="defaults">
<str name="config">db-data-config.xml</str>
</lst>
</requestHandler>
  1. db-data-config.xml is the file where we will define our database and Solr mapping. Create one file and define that file with the following schema:
<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/chintan" user="root" password="root" />
<document>
<entity name="archives" query="select * from archives"
deltaImportQuery="SELECT * from archives WHERE category_id='${dih.delta.id}'">
<field column="category_id" name="category_id" />
<field column="category_name" name="category_name" />
<field column="remarks" name="remarks" />
</entity>
</document>
</dataConfig>
  1. The last part is to let Solr know about the new entity we have added. Go to ${solr_home}/example/example-DIH/solr/db/conf/managed-schema and make the following changes. Change the primary key from id to category_id:
<field name="category_id" type="string" indexed="true" stored="true" required="true" multiValued="false" />

<uniqueKey>category_id</uniqueKey>
This is just a method to show how to import data from MySQL, and hence we are changing the primary key. In the real world, doing this is strongly not recommended.
  1. Now let's add the new fields you introduced in db-data-config.xml:
<field name="category_name" type="string" indexed="true" stored="true"/>
<field name="remarks" type="string" indexed="true" stored="true"/>

That's done. Now restart the server by hitting solr/bin stop -all && solr/bin start -e dih.

Go to the Solr admin panel. On the core selector, select the database and follow the steps shown in this screenshot:

You can hit on the browser to see MySQL data in Solr.

Let's look at ways to browse data in Solr through the browse interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.98.111