In a Linux environment, deploy Solr in tomcat7, import Mysql database data, regularly update indexes, and solrtomcat7

Source: Internet
Author: User

In a Linux environment, deploy Solr in tomcat7, import Mysql database data, regularly update indexes, and solrtomcat7
What is solr?

Solr is a full-text search Server Based on Lucene. It expands and optimizes Lucene.

Preparations

First, download the following software package:

JDK8: jdk-8u60-linux-x64.tar.gz

TOMCAT8: apache-tomcat-8.5.4.zip

SOLR5.5.3: solr-5.5.3.zip.

And upload it to a path on the linux server. For example, if I put it in the/usr/local/solr directory:

Install solr:Decompress solr-5.5.3.zip;

Install tomcat and jdk:A little, there are a lot of online materials, basically unzip, set an environment variable;

Then, install mysql:I installed Mysql 5.5;

Create a database and table structure:I created a database solrdemo and a user table, such,

Updatetime: The Last Update time;

Valid: valid or not. 0 indicates the deleted data and no index is required.

Create a solr core

As for what is core, I think it is something to manage data. There are some configuration files and indexes in it. The creation procedure is as follows:

Enter the bin directory of solr and enter the following command to start solr:

./solr start

The following logs are displayed:

[root@localhost bin]# ./solr start
Waiting up to 30 seconds to see Solr running on port 8983 [-]  
Started Solr server on port 8983 (pid=9951). Happy searching!

Enter the./solr create-c demo command to create a core called demo;

./solr create -c demo

The following logs are displayed:

Copying configuration to new core instance directory:
/usr/local/solr/solr-5.5.3/server/solr/demo

Creating new core 'demo' using command:
http://localhost:8983/solr/admin/cores?action=CREATE&name=demo&instanceDir=demo

{
  "responseHeader":{
    "status":0,
    "QTime":7258},
  "core":"demo"}

Creation complete;

Close the solr service:

Enter the command./solr stop-all

./solr stop -all

The following logs are displayed:

Sending stop command to Solr running on port 8983 ... waiting 5 seconds to allow Jetty process 9951 to stop gracefully.

You can see the demo core we created in the/usr/local/solr/solr-5.5.3/server/solr directory

Deploy to tomcat

Create a folder named solr in the webapp directory of tomcat;

Copy/usr/local/solr/solr-5.5.3/server/solr-webapp/webapp to/usr/local/solr/apache-tomcat-8.5.4/webapps/solr directory;

Create a solrhome folder in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr directory, copy/usr/local/solr/solr-5.5.3/server/solr (there is a core we just created, "demo ") contents in the directory are stored in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome directory.

Copy the jar package under the/usr/local/solr/solr-5.5.3/server/lib/ext directory to/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/lib;

Copy the solr-5.5.3 and solr-dataimporthandler-5.5.3.jar under the/usr/local/solr/solr-dataimporthandler-extras-5.5.3.jar/dist directory to/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/lib;

Copy log4j under the/usr/local/solr/solr-5.5.3/server/resources directory. properties file to the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/classes directory, create one by yourself without the classes folder.

Modify the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/web. xml file content and add the solr/home configuration information:

    <env-entry>       <env-entry-name>solr/home</env-entry-name>       <env-entry-value>/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome</env-entry-value>       <env-entry-type>java.lang.String</env-entry-type>    </env-entry>

Modify/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome/demo/conf/solrconfig. in the xml file, change lines-85 to the following content. Because we copied the content and the path changed, I will use the absolute path here.

  <lib dir="/usr/local/solr/solr-5.5.3/contrib/extraction/lib" regex=".*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/dist/" regex="solr-cell-\d.*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/contrib/clustering/lib/" regex=".*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/dist/" regex="solr-clustering-\d.*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/contrib/langid/lib/" regex=".*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/dist/" regex="solr-langid-\d.*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/contrib/velocity/lib" regex=".*\.jar" />  <lib dir="/usr/local/solr/solr-5.5.3/dist/" regex="solr-velocity-\d.*\.jar" />

Finally, start tomcat and remember to open port 8080. You can modify/etc/sysconfig/iptables, add-A RH-Firewall-1-INPUT-m state -- state NEW-m tcp-p tcp -- dport 8080-j ACCEPT, and then enter service iptables restart to restart the iptables service.

Then, enter http: // 192.168.229.100: 8080/solr/index.html in the browser to view the core demo we created, for example:

Import mysql database data and create indexes

1. Download the mysql connection driver, such as mysql-connector-java-5.1.22-bin.jar, copy to/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/lib directory;

2. Create a file data_config.xml in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome/demo/conf directory. The content is as follows:

<dataConfig><dataSource name="solrdemo" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.229.1:3306/solrdemo" user="root" password="root"/>  <document>    <entity dataSource="solrdemo"              name="user"             query="select id, name, address from user where valid=1 "             deltaImportQuery="select  id,name,address  from user where ID='${dataimporter.delta.id}'"              deltaQuery="select id  from user where updatetime > '${dataimporter.last_index_time}'"              deletedPkQuery="select id  from user where valid=0">                    <field  column="id"  name="id"/>        <field  column="name"  name="name"/>        <field  column="address"  name="address"/>     </entity>  </document></dataConfig>

Modify the managed-schema file in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome/demo/conf directory and add the following two lines, id already exists. Do not add:

    <field name="address" type="string" indexed="true" stored="true" required="true"/>    <field name="name" type="string" indexed="true" stored="true" required="true" />

Modify the managed-schema file in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome/demo/conf directory and add the following content:

    <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">          <lst name="defaults">              <str name="config">data-config.xml</str>          </lst>      </requestHandler>  

Restart tomcat, access http: // 192.168.229.100: 8080/solr/index.html, select demo core, open the dataimport tab, and click execute to update the index, as shown in:

 

Open the query tab and you can see the data in our database. Here, our parameter q is *. *, matching all, as shown below:

Regularly update Indexes

Download the apache-solr-dataimportscheduler.jar library and copy it to the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/lib directory.

Modify the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/WEB-INF/web. xml file and add the listener:

    <listener>
       <listener-class>
          org.apache.solr.handler.dataimport.scheduler.ApplicationListener
       </listener-class>
    </listener>

Create a conf folder in the/usr/local/solr/apache-tomcat-8.5.4/webapps/solr/solrhome/directory and create dataimport under the folder. properties file. The file content is as follows:

######################################## ########### Dataimport schedort properties ######################### ########################### to sync or not to sync #1-active; anything else-inactivesyncEnabled = 1 # which cores to schedule # in a multi-core environment you can decide which cores you want syncronized # leave empty or comment it out if using single-core priority = demo # solr s Erver name or IP address # [defaults to localhost if empty] server = 192.168.229.100 # solr server port # [defaults to 80 if empty] port = 8080 # application name/context # [defaults current ServletContextListener's context (app) name] webapp = solr # URL params [mandatory] # remainder of URLparams =/dataimport? Command = delta-import & clean = false & commit = true # schedule interval # number of minutes between two runs # [defaults to 30 if empty] interval = 1 # interval of redoing Indexes, the Unit is minute. The default value is 7200, that is, 1 day. # It is null, 0, or comment out: indicates that the index reBuildIndexInterval is never redone. 2 # reBuildIndexParams =/dataimport? Command = full-import & clean = true & commit = true # timing start time of the redo index interval. The time for the first real execution = reBuildIndexBeginTime + reBuildIndexInterval * 60*1000; # Two formats: 03:10:00 or 03:10:00. The latter will automatically complete the date part of the reBuildIndexBeginTime = 03:00:00

Here, we configure two minutes to update the index.

Restart tomcat and observe the log. The index is automatically updated every two minutes;

You can also perform a small test on your own, insert data into the database, and then search for the data. You can find it after two minutes.

Resource file

Https://github.com/peterchenhdu/Demos/tree/master/solr-in-tomcat

 

Reference: https://cwiki.apache.org/confluence/display/solr/Apache+Solr+Reference+Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.