热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

solr从数据库建立索引(转)

TheDataImportHandlerFrameworkSolrincludesaverypopularcontribmoduleforimporti

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 /lib directory; you may need to create it. Another method is to reference them from solrconfig.xml via 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.xmlstr>
    lst>
requestHandler>

 

 

This reference mb-dih-artists-jdbc.xml is located in /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.xmlstr>
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 /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文档。

       


推荐阅读
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文为Codeforces 1294A题目的解析,主要讨论了Collecting Coins整除+不整除问题。文章详细介绍了题目的背景和要求,并给出了解题思路和代码实现。同时提供了在线测评地址和相关参考链接。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
author-avatar
mobiledu2502890297
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有