Step 1 : Edit your solrconfig.xml to add the request handler
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
Step 2 : Create a data-config.xml file as follows and save it to the conf dir
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1/dbname" user="test" password="test1" /> <document> <entity name="first" query="SELECT id, name FROM test"> </entity> </document> </dataConfig>
如果上面 的url参数写错,如写成jdbc:mysql://127.0.0.1:8080/dbname
可以看到如下信息:
信息: Creating a connection for entity first with URL: jdbc:mysql://127.0.0.1:8080/dbname
正确的值应当等于下图中host的值:
Step 3 : Ensure that your solr schema (schema.xml) has the fields 'id', 'name', 'desc'. Change the appropriate details in the data-config.xml
Step 4: 把下面组件放到<solr-home>/lib 目录
mysql-connector-java-5.1.19-bin.jar
apache-solr-dataimporthandler-extras-3.5.0.jar
Step 5 : Run the command http://localhost:8983/solr/dataimport?command=full-import
<response> ... <str name="Total Requests made to DataSource">1</str> <str name="Total Rows Fetched">2</str> <str name="Total Documents Skipped">0</str> ... </response>
Step 6: 验证
http://localhost:8983/solr/select/?q=terrya&version=2.2&start=0&rows=10&indent=on
<result name="response" numFound="1" start="0"> <doc> <str name="id">SD453</str> <str name="name">terrya</str> </doc> </result>