首页 > 代码库 > solr dataimport

solr dataimport

solrconfig.xml

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

 

solrconfig.xml同一个目录下增加 data-config.xml 文件

<dataConfig>    <dataSource type="JdbcDataSource" name="ds-1"             driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"            url="jdbc:sqlserver://localhost;databaseName=My_DB;responseBuffering=adaptive;"            user="you name"             password="you password"/>      <dataSource type="JdbcDataSource" name="ds-2"             driver="com.mysql.jdbc.Driver"             url="jdbc:mysql://db2-host/dbname"             user="db_username"             password="db_password"/>     <dataSource type="FileDataSource" name="ds-3" encoding="UTF-8" />                <document>    <!--add filed for match sql column to solr column-->    <entity name="product" dataSource="ds-1"          query="select top 1 * from Product"        transformer="RegexTransformer,DateFormatTransformer" >          <field column="Id" name="id"/>                <entity name="productCategory" dataSource="ds-1"              query="select top 1 * from Code_Category_Product                 Where Id=${product.CategoryId}">            <field column="Id" name="categoryid"/>            <field column="Description" name="category"/>        </entity>        <field column="Name" name="name" />        <field column="Description" name="description" />        <field column="UnitPrice" name="unitprice"/>        <field column="Status" name="status"/>        <field column="CreatedTime" name="createdtime" dateTimeFormat="M/d/yyyy h:m:s a"/>        <field column="Name" regex=".*Product1" name="name_py" />    </entity>        <!--atch sql column to solr column in the sql query-->    <entity name="product2" dataSource="ds-1"          query="select Id as id,                CategoryId,                Name as name,                 Description as description,                UnitPrice as unitprice,                Status as status,                CreatedTime as createdTime from Product                Where Id=10000000002">            <entity name="productCategory" dataSource="ds-1"              query="select top 1 * from Code_Category_Product                 Where Id=${product2.CategoryId}">            <field column="Id" name="categoryid"/>            <field column="Description" name="category"/>                    </entity>    </entity>        <entity name="product3" dataSource="ds-3"          processor="XPathEntityProcessor"          stream="true"          forEach="/products/product | /products/item"          url="D:\Solr\solr-NewRMS\example\conf\products.xml"        transformer="RegexTransformer,DateFormatTransformer" >              <field column="id" xpath="/products/product/id" />              <field column="name" xpath="/products/product/@name" />              <field column="name_py" xpath="/products/product/name_py" />              <field column="createdtime" xpath="/products/product/createdtime" dateTimeFormat="yyyy-MM-dd‘T‘hh:mm:ss‘Z‘" />              <field column="tag" xpath="/products/product/tag" splitBy=","/>            <field column="description" regex="/products/product/description" />       </entity>    </document></dataConfig>
View Code

 

data-config.xml同一个目录下增加 products.xml

<products>    <product name="product1001">        <id>1001</id>        <createdtime>2011-11-07T17:05:14</createdtime>        <description><![CDATA[this is 1001 desc]]></description>        <tag>tag11,tag22</tag>    </product>    <product name="product1002">        <id>1002</id>        <name_py></name_py>        <createdtime>2012-11-07T17:05:14</createdtime>        <description><![CDATA[this is 1002 description]]></description>        <tag>tag1</tag>        <tag>tag2</tag>    </product></products>
View Code

 

数据库:Product 

 

 如果不想把之前的数据清除 取消 Clean 选项
 在Entity中选择运行项
 点击Execute, 一段时间后点击Refresh Status查看运行结果
 可以去Query中查看是否已经插入数据