CONAN ZONE

你越挣扎我就越兴奋

BlogJava 首页 新随笔 联系 聚合 管理
  0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks

The Data Import Handler Framework

Solr includes a very popular contrib module for importing data known as the DataImportHandler (DIH in short). It's a data processing pipeline built specificallyfor Solr. Here's a summary of notable capabilities:

•     Imports data from databases through JDBC (Java Database Connectivity)
     ° Supports importing only changed records, assuming a last-updated date
•     Imports data from a URL (HTTP GET)
•     Imports data from files (that is it crawls files)
•     Imports e-mail from an IMAP server, including attachments
•     Supports combining data from different sources
•     Extracts text and metadata from rich document formats
•     Applies XSLT transformations and XPath extraction on XML data
•     Includes a diagnostic/development tool

 

 

The DIH is not considered a core part of Solr, even though it comes with the Solr download, and so you must add its Java JAR files to your Solr setup to use it. If this isn't done, you'll eventually see a ClassNotFoundException error. The DIH's JAR files are located in Solr's dist directory: apache-solr-dataimporthandler-3.4.0.jar and apache-solr-dataimporthandler-extras-3.4.0.jar. The easiest way to add JAR files to a Solr configuration is to copy them to the <solr_home>/lib directory; you may need to create it. Another method is to reference them from solrconfig.xml via <lib/> tags—see Solr's example configuration for examples of that. You will most likely need some additional JAR files as well. If you'll be communicating with a database, then you'll need to get a JDBC driver for it. If you will be extracting text from various document formats then you'll need to add the JARs in /contrib/extraction/lib. Finally, if you'll be indexing e-mail then you'll need to add the JARs in /contrib /dataimporthandler/lib.

 

 

The DIH needs to be registered with Solr in solrconfig.xml like so:

<requestHandler name="/dih_artists_jdbc" 
      class
="org.apache.solr.handler.dataimport.DataImportHandler">
    
<lst name="defaults">
        
<str name="config">mb-dih-artists-jdbc.xml</str>
    
</lst>
</requestHandler>

 


This reference mb-dih-artists-jdbc.xml is located in <solr-home>/conf, which specifies the details of a data importing process. We'll get to that file in a bit.

 

DIHQuickStart

http://wiki.apache.org/solr/DIHQuickStart

 

Index a DB table directly into Solr

Step 1 : Edit your solrconfig.xml to add the request handle

<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://localhost/dbname" 
              user
="user-name" 
              password
="password"/>
  
<document>
    
<entity name="id" 
            query
="select id,name,desc from mytable">
    
</entity>
  
</document>
</dataConfig>

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: Drop your JDBC driver jar file into the <solr-home>/lib directory .

 

Step 5 : Run the command

http://solr-host:port/solr/dataimport?command=full-import .

Keep in mind that every time a full-import is executed the index is cleaned up. If you do not wish that to happen add clean=false. For example:

http://solr-host:port/solr/dataimport?command=full-import&clean=false

 

Index the fields in different names

 

Step: 1 Change the data-config as follows:

<dataConfig>
  
<dataSource type="JdbcDataSource" 
              driver
="com.mysql.jdbc.Driver"
              url
="jdbc:mysql://localhost/dbname" 
              user
="user-name" 
              password
="password"/>
  
<document>
    
<entity name="id" 
            query
="select id,name,desc from mytable">
       
<field column="id" name="solr_id"/>
       
<field column="name" name="solr_name"/>
       
<field column="desc" name="solr_desc"/>
    
</entity>
  
</document>
</dataConfig>


 

Step 2 : This time the fields will be written to the solr fields 'solr_id', 'solr_name', solr_desc'. You must have these fields in the schema.xml.

 

Step 3 : Run the command http://solr-host:port/dataimpor?command=full-import

 

Index data from multiple tables into Solr

Step: 1 Change the data-config as follows :

 

<dataConfig>
  
<dataSource type="JdbcDataSource" 
              driver
="com.mysql.jdbc.Driver"
              url
="jdbc:mysql://localhost/dbname" 
              user
="user-name" 
              password
="password"/>
  
<document>
    
<entity name="outer" 
            query
="select id,name,desc from mytable">
       
<field column="id" name="solr_id"/>
       
<field column="name" name="solr_name"/>
       
<field column="desc" name="solr_desc"/>
       
<entity name="inner"
               query
="select details from another_table where id ='${outer.id}'">
              
<field column="details" name="solr_details"/> 
       
</entity>
    
</entity>
  
</document>
</dataConfig>


 

Step 2: The schema.xml should have the solr_details field

 

Step 3: Run the full-import command

 

配置数据源

将 dataSource标签直接添加到dataConfig下面,即成为dataConfig的子元素.

 

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="db_username" password="db_password"/>
  • driver(必需的):jdbc驱动名称
  • url(必需的):jdbc链接
  • user:用户名
  • password:密码
  • 批量大小:jdbc链接中的批量大小
  • 数据源也可以配置在solrconfig.xml中
  • 属性type 指定了实现的类型。它是可选的。默认的实现是JdbcDataSource。
  • 属性 name  是datasources的名字,当有多个datasources时,可以使用name属性加以区分
  • 其他的属性都是随意的,根据你使用的DataSource实现而定。
  • 当然 你也可以实现自己的DataSource。

数据源

 

<dataSource type="JdbcDataSource" name="ds-1" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db1-host/dbname" user="db_username" password="db_password"/>

<dataSource type="JdbcDataSource" name="ds-2" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db2-host/dbname" user="db_username" password="db_password"/>

使用:

..

<entity name="one" dataSource="ds-1" >

   ..

</entity>

<entity name="two" dataSource="ds-2" >

   ..

</entity>

..

配置data-config.xml

    solr document是schema,它的域上的值可能来自于多个表.

    data-config.xml的根元素是document。一个document元素代表了一种文档。一个document元素中包含了一个或者多个root实体。一个root实体包含着一些子实体,这些子实体能够包含其他的实体。实体就是,关系数据库上的表或者视图。每个实体都能够包含多个域,每个域对应着数据库返回结果中的一列。域的名字跟列的名字默认是一样的。如果一个列的名字跟solr field的名字不一样,那么属性name就应该要给出。其他的需要的属性在solrschema.xml文件中配置。

    为了能够从数据库中取得想要的数据,我们的设计支持标准sql规范。这使得用户能够使用他任何想要的sql语句。root实体是一个中心表,使用它的列可以把表连接在一起。

 

dataconfig的结构

    dataconfig 的结构不是一成不变的,entity和field元素中的属性是随意的,这主要取决于processor和transformer。

    以下是entity的默认属性

  •  name(必需的):name是唯一的,用以标识entity
  • processor:只有当datasource不是RDBMS时才是必需的。默认值是 SqlEntityProcessor
  • transformer:转换器将会被应用到这个entity上,详情请浏览transformer部分。
  • pk:entity的主键,它是可选的,但使用“增量导入”的时候是必需。它跟schema.xml中定义的 uniqueKey没有必然的联系,但它们可以相同。
  • rootEntity:默认情况下,document元素下就是根实体了,如果没有根实体的话,直接在实体下面的实体将会被看做跟实体。对于根实体对应的数据库中返回的数据的每一行,solr都将生成一个document。

    一下是SqlEntityProcessor的属性

  • query (required) :sql语句

  • deltaQuery : 只在“增量导入”中使用

  • parentDeltaQuery : 只在“增量导入”中使用

  • deletedPkQuery : 只在“增量导入”中使用

  • deltaImportQuery : (只在“增量导入”中使用) . 如果这个存在,那么它将会在“增量导入”中导入phase时代替query产生作用。这里有一个命名空间的用法${dataimporter.delta.}

Commands

    打开导入数据界面http://192.168.0.248:9080/solr/admin/dataimport.jsp,看到几种按钮分别调用不同的导数据命令。

  • full-import : "完全导入"这个操作可以通过访问URL http://192.168.0.248:9080/solr/dataimport?command=full-import 完成。

    • 这个操作,将会新起一个线程。response中的attribute属性将会显示busy。

    • 这个操作执行的时间取决于数据集的大小。

    • 当这个操作运行完了以后,它将在conf/dataimport.properties这个文件中记录下这个操作的开始时间

    • 当“增量导入”被执行时,stored timestamp这个时间戳将会被用到

    • solr的查询在“完全导入”时,不是阻塞的

    • 它还有下面一些参数:

      • clean : (default 'true'). 决定在建立索引之前,删除以前的索引。

      • commit : (default 'true'). 决定这个操作之后是否要commit

      • optimize : (default 'true'). 决定这个操作之后是否要优化。

      • debug : (default false). 工作在debug模式下。详情请看 the interactive development mode (see here )

  • delta-import : 当遇到一些增量的输入,或者发生一些变化时使用http://192.168.0.248:9080/solr/dataimport?command= delta-import .它同样支持  clean, commit, optimize and debug 这几个参数.

  • status : 想要知道命令执行的状态 , 访问 URL http://192.168.0.248:9080/solr/dataimport .它给出了关于文档创建、删除,查询、结果获取等等的详细状况。

  • reload-config : 如果data-config.xml已经改变,你不希望重启solr,而要重新加载配置时,运行一下的命令http://192.168.0.248:9080/solr/dataimport?command=reload-config

  • abort : 你可以通过访问 http://192.168.0.248:9080/solr/dataimport?command=abort 来终止一个在运行的操作

 

Full Import 例子

data-config.xml 如下:

 

<dataConfig>

    
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />

    
<document name="products">

        
<entity name="item" query="select * from item">

            
<field column="ID" name="id" />

            
<field column="NAME" name="name" />

            
<field column="MANU" name="manu" />

            
<field column="WEIGHT" name="weight" />

            
<field column="PRICE" name="price" />

            
<field column="POPULARITY" name="popularity" />

            
<field column="INSTOCK" name="inStock" />

            
<field column="INCLUDES" name="includes" />

            
<entity name="feature" query="select description from feature where item_id='${item.ID}'">

                
<field name="features" column="description" />

            
</entity>

            
<entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">

                
<entity name="category" query="select description from category where id = '${item_category.CATEGORY_ID}'">

                    
<field column="description" name="cat" />

                
</entity>

            
</entity>

        
</entity>

    
</document>

</dataConfig>


这里, 根实体是一个名叫“item”的表,它的主键是id。我们使用语句 "select * from item"读取数据. 每一项都拥有多个特性。看下面feature实体的查询语句:

  <entity name="feature" query="select description from feature where item_id='${item.id}'">
       
<field name="feature" column="description" />
   
</entity> 


feature表中的外键item_id跟item中的主键连在一起从数据库中取得该row的数据。相同地,我们将item和category连表(它们是多对多的关系)。注意,我们是怎样使用中间表和标准sql连表的:

 <entity name="item_category" query="select category_id from item_category where item_id='${item.id}'">
    
<entity name="category" query="select description from category where id = '${item_category.category_id}'">
        
<field column="description" name="cat" />
    
</entity>
</entity>

短一点的 data-config

在上面的例子中,这里有好几个从域到solr域之间的映射。如果域的名字和solr中域的名字是一样的话,完全避免使用在实体中配置域也是可以的。当然,如果你需要使用转换器的话,你还是需要加上域实体的。

 

<dataConfig>

    
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />

    
<document>

        
<entity name="item" query="select * from item">                    

            
<entity name="feature" query="select description as features from feature where item_id='${item.ID}'"/>            

            
<entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">

                
<entity name="category" query="select description as cat from category where id = '${item_category.CATEGORY_ID}'"/>                        

            
</entity>

        
</entity>

    
</document>

</dataConfig>




 

访问 http://localhost:8983/solr/dataimport?command=full-import 执行一个“完全导入”

 

使用“增量导入”命令

你可以通过访问URL http://localhost:8983/solr/dataimport?command=delta-import  来使用增量导入。操作将会新起一个线程,response中的属性statue也将显示busy now。操作执行的时间取决于你的数据集的大小。在任何时候,你都可以通过访问 http://localhost:8983/solr/dataimport 来查看状态。

当 增量导入被执行的时候,它读取存储在conf/dataimport.properties中的“start time”。它使用这个时间戳来执行增量查询,完成之后,会更新这个放在conf/dataimport.properties中的时间戳。

Delta-Import 例子

我们将使用跟“完全导入”中相同的数据库。注意,数据库已经被更新了,每个表都包含有一个额外timestamp类型的列  叫做last_modified。或许你需要重新下载数据库,因为它最近被更新了。我们使用这个时间戳的域来区别出那一行是上次索引以来有更新的。

看看下面的这个 data-config.xml:

 

<dataConfig>

    
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />

    
<document name="products">

            
<entity name="item" pk="ID" query="select * from item"

                deltaQuery
="select id from item where last_modified > '${dataimporter.last_index_time}'">           



            
<entity name="feature" pk="ITEM_ID" 

                    query
="select description as features from feature where item_id='${item.ID}'">                

            
</entity>

            
<entity name="item_category" pk="ITEM_ID, CATEGORY_ID"

                    query
="select CATEGORY_ID from item_category where ITEM_ID='${item.ID}'">

                
<entity name="category" pk="ID"

                       query
="select description as cat from category where id = '${item_category.CATEGORY_ID}'">                    

                
</entity>

            
</entity>

        
</entity>

    
</document>

</dataConfig>

注意到item实体的 属性deltaquery了吗,它包含了一个能够查出最近更新的sql语句。注意,变量{dataimporter.last_index_time } 是DataImporthandler传过来的变量,我们叫它时间戳,它指出“完全导入”或者“部分导入”的最后运行时间。你可以在data- config.xml文件中的sql的任何地方使用这个变量,它将在processing这个过程中被赋值。

 

上面例子中deltaQuery 只能够发现item中的更新,而不能发现其他表的。你可以像下面那样在一个sql语句中指定所有的表的更新:

 

deltaQuery="select id from item where id in

                                (select item_id as id from feature where last_modified > '${dataimporter.last_index_time}')

                                or id in 

                                (select item_id as id from item_category where item_id in 

                                    (select id as item_id from category where last_modified > '${dataimporter.last_index_time}')

                                or last_modified > '${dataimporter.last_index_time}')

                                or last_modified > '${dataimporter.last_index_time}'"

 写一个类似上面的庞大的deltaQuery 并不是一件很享受的工作,我们还是选择其他的方法来达到这个目的

<dataConfig>

    
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />

    
<document>

            
<entity name="item" pk="ID" query="select * from item"

                deltaQuery
="select id from item where last_modified > '${dataimporter.last_index_time}'">

                
<entity name="feature" pk="ITEM_ID" 

                    query
="select DESCRIPTION as features from FEATURE where ITEM_ID='${item.ID}'"

                    deltaQuery
="select ITEM_ID from FEATURE where last_modified > '${dataimporter.last_index_time}'"

                    parentDeltaQuery
="select ID from item where ID=${feature.ITEM_ID}"/>              

            
            
<entity name="item_category" pk="ITEM_ID, CATEGORY_ID"

                    query
="select CATEGORY_ID from item_category where ITEM_ID='${item.ID}'"

                    deltaQuery
="select ITEM_ID, CATEGORY_ID from item_category where last_modified > '${dataimporter.last_index_time}'"

                    parentDeltaQuery
="select ID from item where ID=${item_category.ITEM_ID}">

                
<entity name="category" pk="ID"

                        query
="select DESCRIPTION as cat from category where ID = '${item_category.CATEGORY_ID}'"

                        deltaQuery
="select ID from category where last_modified > '${dataimporter.last_index_time}'"

                        parentDeltaQuery
="select ITEM_ID, CATEGORY_ID from item_category where CATEGORY_ID=${category.ID}"/>

            
</entity>

        
</entity>

    
</document>

</dataConfig>

 

  • deltaQuery 取得从上次索引更新时间以来有更新的实体的主键。

  • parentDeltaQuery 从deltaQuery中取得当前表中更新的行,并把这些行提交给父表。因为,当子表中的一行发生改变时,我们需要更新它的父表的solr文档。

下面是一些值得注意的地方:

  • 对于query语句返回的每一行,子实体的query都将被执行一次

  • 对于deltaQuery返回的每一行,parentDeltaQuery都将被执行。

  • 一旦根实体或者子实体中的行发生改变,我们将重新生成包含该行的solr文档。


 



 








posted on 2012-05-30 14:33 CONAN 阅读(4698) 评论(0)  编辑  收藏 所属分类: Solr