使用 DataImportHandler 连接 MongoDB 和 Solr 的步骤 [英] Steps to connect MongoDB and Solr using DataImportHandler
问题描述
我是 SOLR 和 MONGODB 的新手.
I am new to SOLR and MONGODB.
我正在尝试使用 DataImportHandler 将 mongodb 中的数据索引到 SOLR 中,但我找不到我需要遵循的确切步骤.
I am trying to index data from mongodb into SOLR using DataImportHandler but I could not find the exact steps that I need to follow.
您能否帮助我了解使用 DataImportHandler 将 MongoDB 索引到 Solr 的确切步骤?
Could you please help me in getting the exact steps to index MongoDB into Solr using DataImportHandler?
SolrVersion - solr-4.6.0
SolrVersion - solr-4.6.0
MongoDB 版本 - 2.2.7
MongoDB version- 2.2.7
推荐答案
迟回复,但认为人们可能会觉得它有用.
Late to answer, however thought people might find it useful.
以下是使用DataImportHandler从mongodb导入数据到Solr 4.7.0的步骤.
Below are the steps for importing data from mongodb to Solr 4.7.0 using DataImportHandler.
假设你的 Mongodb 有以下数据库和集合
Assume that your Mongodb has following database and collection
Database Name: Test
Collection Name: sample
sample
集合有以下文档
db.sample.find()
{ "_id" : ObjectId("54c0c6666ee638a21198793b"), "Name" : "Rahul", "EmpNumber" : 452123 }
{ "_id" : ObjectId("54c0c7486ee638a21198793c"), "Name" : "Manohar", "EmpNumber" : 784521 }
第 2 步:
在您的 solrhome 文件夹中创建一个 lib
文件夹(其中包含 bin
和 collection1
文件夹)
Step 2:
Create a lib
folder in your solrhome folder( which has bin
and collection1
folders)
将以下 jar 文件添加到 lib
文件夹.你可以从这里下载solr-mongo-importer!
add below jar files to lib
folder. You can download solr-mongo-importer from here!
- solr-dataimporthandler-4.7.0.jar
- solr-mongo-importer-1.0.0.jar
- mongo-java-driver-2.10.1.jar (this is the mongo java driver)
第 3 步:
在schema.xml中声明Solr字段(假设id已经默认定义)
Step 3:
Declare Solr fields in schema.xml(assumed that id is already defined by default)
在
标签.
<field name="Name" type="text_general" indexed="true" stored="true"/>
<field name="EmployeeNumber" type="int" indexed="true" stored="true"/>
第 4 步:
通过在
标签.
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
第 5 步:
在路径 collection1conf 中创建一个 data-config.xml 文件(默认保存 solrconfig.xml 和 schema.xml)
Step 5:
Create a data-config.xml file in the path collection1conf (which by default holds solrconfig.xml and schema.xml)
data-config.xml
data-config.xml
<?xml version="1.0"?>
<dataConfig>
<dataSource name="MyMongo" type="MongoDataSource" database="Test" />
<document name="import">
<!-- if query="" then it imports everything -->
<entity processor="MongoEntityProcessor"
query="{Name:'Rahul'}"
collection="sample"
datasource="MyMongo"
transformer="MongoMapperTransformer" name="sample_entity">
<!-- If mongoField name and the field declared in schema.xml are same than no need to declare below.
If not same than you have to refer the mongoField to field in schema.xml
( Ex: mongoField="EmpNumber" to name="EmployeeNumber"). -->
<field column="_id" name="id"/>
<field column="EmpNumber" name="EmployeeNumber" mongoField="EmpNumber"/>
</entity>
</document>
</dataConfig>
第 6 步:
假设 solr(我使用了 8080 端口)和 mongodb 正在运行,打开以下链接 浏览器中的 http://localhost:8080/solr/dataimport?command=full-import 用于将数据从 mongodb 导入 solr.
Step 6:
Assuming solr (I have used port 8080) and mongodb are running, open the following link http://localhost:8080/solr/dataimport?command=full-import in your browser for importing data from mongodb to solr.
导入的字段是 _id,Name 和 EmpNumber(MongoDB) 作为 id,Name 和 EmployeeNumber(Solr).
fields imported are _id,Name and EmpNumber(MongoDB) as id,Name and EmployeeNumber(Solr).
可以在http://localhost:8080/solr/query?q=*
这篇关于使用 DataImportHandler 连接 MongoDB 和 Solr 的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!