Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

(solr Series: IV) Import data from mysql database into solr and delete imported data from solr

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In the previous blog post, you have completed the deployment of solr in tomcat, added a custom core for solr, and introduced the ik word divider.

So how do you import local mysql data into solr?

Preparatory work:

1. Mysql data source: the user table (8 pieces of data) in the myuser library, in which this updateTime field is the basis for solr to update database data, and must have this field in the table.

/ * 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 (11) NOT NULL AUTO_INCREMENT, `name` varchar (255) NOT NULL, `password` varchar (255) NOT NULL, `updateTime` datetime DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 -Records of user-- INSERT INTO `user`VALUES ('1Qing,' Zhang San', 'abc',' 2016-10-21 10 VALUES 10) INSERT INTO `user`VALUES ('2Qing,' Li Si', 'def',' 2016-10-21 10 def', 10 def','); INSERT INTO `user` VALUES ('3mm,' Wang Wu', 'ghi',' 2016-10-21 10 def', 10RV 58'); INSERT INTO `user`Rich ('415,' Zhao Liu', 'jkl',' 2016-10-21 10RV 10)) INSERT INTO `user`VALUES ('515,' Tian Qi', 'mno',' 2016-10-21 10 mno', 10 mno','); INSERT INTO `user` VALUES ('615,' Lao Ba', 'pqr',' 2016-10-21 10 mno', 10); INSERT INTO `user`Rich ('7Qing,' Jin Jiu', 'stu',' 2016-10-21 10RV 10)) INSERT INTO `user`VALUES ('8mm,' Silver Shih', 'vwx',' 2016-10-21 10 vwx', 10 vwx', 58')

2. Data source configuration file: create a new file: data-config.xml, with the following contents:

3. Mysql driver jar package and jar package required for solr to import data: mysql-connector-java-5.1.32.jar (which everyone has) and solr-dataimporthandler-4.10.2.jar (this can be found in the solr-4.10.2\ dist directory, just copy one)

=

After the preparatory work is complete, you can configure and operate.

Step 1: copy the two jar packages you just prepared to the apache-tomcat-7.0.72\ webapps\ solr\ WEB-INF\ lib directory.

Step 2: copy the prepared data-config.xml file to the solrhome\ simple\ conf directory in the same directory as schema.xml.

Step 3: locate the solrconfig.xml file in the solrhome\ simple\ conf directory and add the following to the file:

Data-config.xml

Step 4: find the schema.xml file in the solrhome\ simple\ conf directory, and open and modify it as follows:

Id

Step 4: start tomcat, browser access: locahost:8080/solr the original number of simple without any data.

Step 5: import mysql data into solr

Check the data again, if shown below, then congratulations, the data import is successful!

=

What to do if you want to delete the imported data in solr, as soon as the picture above. It is clear

Xml command:

1. Delete according to query: delete all data

*: *

Of course, the above is manual deletion, if there is a lot of data to be deleted, what should I do if I want to delete the specified data automatically the next time I automatically synchronize the data?

Scenario: a batch of store data to be auctioned is stored in solr, and the auction data has an auction end time (endTime). When the auction ending time is up, the auction data stored in solr is meaningless. You want to delete this data stored in solr.

It is not impossible to delete it manually according to the above, but this manual operation is too crazy, and it is time-consuming and labor-consuming to manually delete this piece of data in solr, and it is not very realistic.

The solution is:

Add to the entity of data-config.xml:

DeletedPkQuery= "select id from shops where endTime < NOW ()"

This sentence, which is at the same level as entiy's query, aims to query the id of all stores whose end time is less than the current time in the store data.

The store id performs deltaImportQuery operations along with the id collected in deltaQuery, except that one is to add data and the other is to delete data.

For other business scenarios, you can also learn from this blog to add an isdelete field to the table to delete, as long as there is a field that can be identified:

Reference address: http://simplelife.blog.51cto.com/9954761/1883024

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report