Chapter 4, Import Data from Database - DataImport

This section describes how to import data from the database and perform searches

1. Configure dataimport

Add dataimport to the configuration file /conf/solrconfig.xml of the previous core_test

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

 

2. Configure data-config.xml

Create the file data-config.xml in the same directory as solrconfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource name="solrDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
 user="root" password=""/>
<document>
    <entity pk="id" dataSource="solrDB" name="nba_star"
    query="SELECT
			  a.id,
			  a.name,
			  a.age,
			  a.desc
			FROM
			  nba_super_star a"
    deltaImportQuery="SELECT
			  a.id,
			  a.name,
			  a.age,
			  a.desc
			FROM
			  nba_super_star a
			WHERE
			  a.id='${dataimporter.delta.id}'"
    deltaQuery="SELECT id from nba_super_star
                      where modifier_time > '${dataimporter.last_index_time}'">
        <field column="id" name="id"/>
        <field column="name" name="name"/>
		<field column="age" name="age"/>
        <field column="desc" name="desc"/>
    </entity>
</document>
</dataConfig>

 Here is mainly to configure database connection parameters, solr update using SQL, etc.

If there is a field with multiValued="true" (there may be multiple values), the configuration imported by this field can be added to the corresponding entity ( nba_star ) (assuming the corresponding relationship is stored in the solr_test_star_tag table)

<entity name="sport_star_tags"
   query="select tag_text from solr_test_star_tag where star_id='${sport_stars.id}'" >
	<field column="tag_text" name="tags"/>  
</entity>

 

3. Import related packages to WEB-INF/lib

Generally, it can be found in the dist folder of the solr directory.

solr-dataimporthandler-6.1.0.jar、solr-dataimporthandler-extras-6.1.0.jar、mysql-connector-java-5.1.34.jar

 

4. Import all data

Import all:

http://localhost:8080/solr/core_test/dataimport?command=full-import

Check status:

http://localhost:8080/solr/core_test/dataimport?command=status

 

5. Inquiry

Same as previous chapter

 

Attached:

Test database tables and raw data SQL

CREATE DATABASE `test`;
USE `test`;
DROP TABLE IF EXISTS `nba_super_star`;
CREATE TABLE `nba_super_star` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '球星ID',
  `name` varchar(40) DEFAULT NULL COMMENT '姓名',
  `age` smallint(6) DEFAULT NULL COMMENT 'age',
  `desc` text COMMENT 'Introduction',
  `modifier_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

insert into `nba_super_star`(`id`,`name`,`age`,`desc`) values ​​(1,'Michael Jordan',53,'Michael Jordan, born February 17, 1963 in Brooklyn, New York, is a professional Shooting guard, the greatest basketball player in history. On September 11, 2009, Michael Jordan was officially inducted into the NBA Basketball Hall of Fame.'),
	(2,'LeBron James',32,'LeBron James (LeBron James), nicknamed "The Emperor", born on December 30, 1984 in Akron, Ohio, USA, is a world-renowned American male professional basketball player, Small forward for the NBA Cleveland Cavaliers.'),
	(3,'Stephen Curry',28,'Stephen Curry (Stephen Curry), born on March 14, 1988 in Akron, Ohio, USA (Akron, Ohio), an American professional basketball player, a point guard, playing for to the NBA Golden State Warriors.'),
	(4, 'James Harden', 27, 'James Harden, entered the NBA through the draft in 2009, as a shooting guard');

 

Back to Contents

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327030473&siteId=291194637