(SOLR series: IV) Import data from the MySQL database into SOLR

Source: Internet
Author: User
Tags solr

In the previous blog post, the deployment of SOLR in Tomcat was completed, a custom core was added to SOLR, and an IK word breaker was introduced.

So how do you import the local MySQL data into SOLR?

Preparatory work:

1. mysql Data source: User table in MyUser Library (8 data)

/*navicat mysql data transfersource server          : localhostSource Server Version : 50521Source Host            : localhost:3306Source Database        : usertarget server type    : mysqltarget  Server Version : 50521File Encoding          : 65001Date: 2016-10-21 10:14:01*/SET FOREIGN_KEY_CHECKS=0;-- ----------------- ------------- Table structure for user-- ----------------------------drop table  IF EXISTS  ' user '; create table  ' user '   (   ' id '  int (one)  not null auto_increment,    ' name '  varchar (255)  NOT NULL,   ' password '  varchar (255)  NOT&NBsp null,   ' UpdateTime '  datetime DEFAULT NULL,  PRIMARY KEY  (' id '))  ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;-- ---------------------------- -- records of user-- ----------------------------insert into  ' user '  values   (' 1 ',  ' Zhang San ',  ' abc ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 2 ',  ' John Doe ',  ' def ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 3 ',  ' Harry ',  ' Ghi ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 4 ',  ' Zhao Liu ',  ' jkl ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 5 ',  ' Tianqi ',  ' mno ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 6 ',  ' old eight ',  ' PQR ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  values   (' 7 ',  ' Gold Nine ',  ' stu ',  ' 2016-10-21 10:10:58 ');insert into  ' user '  VALUES  (' 8 ',  ' Silver Ten ',   ' vwx ',  ' 2016-10-21 10:10:58 ');

2. Data Source configuration file: New file: Data-config.xml, the file contents are as follows :

<dataconfig><datasource type= "Jdbcdatasource" driver= "Com.mysql.jdbc.Driver" url= "Jdbc:mysql://localhost /myuser "user=" root "password=" root "batchsize=" "/><document><entity name=" user "pk=" id "query=" Select Id,name,password,updatetime from the user "deltaimportquery=" select Id,name,password,updatetime from user where id= ' ${dataimporter.delta.id} ' deltaquery= ' select id from user where updatetime > ' ${dataimporter.last_index_time} ' " ><field column= "id" name= "id"/><field column= "name" name= "name"/><field column= "password" name= " Password "/><field column=" UpdateTime "name=" UpdateTime "/></entity></document></ Dataconfig>

3. The jar package required for MySQL driver jar package and SOLR import data: Mysql-connector-java-5.1.32.jar (this everyone has) and Solr-dataimporthandler-4.10.2.jar (This can be found in the Solr-4.10.2\dist directory, copy out a Can)

====================================================================================

After the preparation is complete, it can be configured and manipulated.

Step one: Copy the two jar packages you just prepared into the Apache-tomcat-7.0.72\webapps\solr\web-inf\lib directory.


Step two: Copy the prepared data-config.xml files to the solrhome\simple\conf directory, and schema.xml the same directory.


Step three: Locate the Solrconfig.xml file in the solrhome\simple\conf directory and add the following to the file:

<requesthandler name= "/dataimport" class= "Org.apache.solr.handler.dataimport.DataImportHandler" ><lst Name= "Defaults" > <str name= "config" >data-config.xml</str></lst> </requestHandler>

Step four: Locate the Schema.xml file in the solrhome\simple\conf directory, open and modify the following:

<?xml version= "1.0"  encoding= "UTF-8"  ?><schema name= "Example"  version= "1.5" ><!--Define Type-->  <types><fieldtype name= "string"  class= "SOLR. Strfield "sortmissinglast=" true " /><fieldtype name=" Long " class=" SOLR. Trielongfield "precisionstep=" 0 " positionincrementgap=" 0 "/><fieldtype name=" int " class=" Solr. Trieintfield "precisionstep=" 0 " positionincrementgap=" 0 " /><fieldtype name=" date "  Class= "SOLR. Triedatefield "precisionstep=" 8 " positionincrementgap=" 0 " /><fieldtype name=" Text_ik "  class= "SOLR. TextField "><analyzer class=" Org.wltea.analyzer.lucene.IKAnalyzer " /></fieldType>   </types>  <!--define field--><fields><field name= "_version_"  type = "Long"  indexed= "true"  stored= "true"  /><field name= "_root_"  type= "string"   Indexed= "true"  sTored= "false"  /><field name= "id"  type= "int"  indexed= "true"  stored= "true" Required= "true"  multivalued= "false"  /><!--Note: The name in field here corresponds to the name in Data-config.xml-->< The type of the!--field name can be string--><field name= "name"  type= "Text_ik"  indexed= "true"  stored = "true"  /><field name= "Password"  type= "string"  indexed= "true"  stored= "true"  /><field name= "UpdateTime"  type= "date"  indexed= "true"  stored= "true"  /> </fields><uniquekey>id</uniquekey><solrqueryparser defaultoperator= "and"  /> </schema>

Step four: Launch Tomcat, browser access:LOCAHOST:8080/SOLR the original simple medium number does not have any data.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/27/wKiom1gJg1mzQHFVAAEzLfQL15A890.png "title=" 1.png " alt= "Wkiom1gjg1mzqhfvaaezlfql15a890.png"/>


Step five: Import MySQL data into SOLR


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/25/wKioL1gJhEjAWQ8BAAFEUREJWCE860.png "title=" 2.png " alt= "Wkiol1gjhejawq8baafeurejwce860.png"/>

Look at the data again, if shown below, then congratulations, the data import success!

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/25/wKioL1gJh6Kiv8HDAAFRCaGoUng916.png "title=" 26.png "alt=" Wkiol1gjh6kiv8hdaafrcagoung916.png "/>



=====================================================================================

If you want to remove the imported data from SOLR, you can do so ... Clear 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0007.gif "alt=" J_0007.gif "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/29/wKioL1gJzXjw6LC6AAD3LRm78h4615.png "title=" 4.png " alt= "wkiol1gjzxjw6lc6aad3lrm78h4615.png"/>xml command:

<delete><query>*:* </query></delete><commit/>


This article is from the "Simple Life" blog, so be sure to keep this source http://simplelife.blog.51cto.com/9954761/1864154

(SOLR series: IV) Import data from the MySQL database into SOLR

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.