Solr学习笔记之3、Solr导入SQLServer数据建立索引

一、下载MSSQLServer的JDBC驱动

下载:Microsoft JDBC Driver 4.0 for SQL Server

地址:http://www.microsoft.com/zh-CN/download/details.aspx?displaylang=en&id=11774

二、配置Solr dataimport for SQLServer

1、依赖jar包配置

将MSSQLServer的JDBC驱动中的sqljdbc4.jar复制到\tomcat\webapps\solr\WEB-INF\lib文件夹中。

将solr-dataimporthandler-4.7.0.jar和solr-dataimporthandler-extras-4.7.0.jar复制到\tomcat\webapps\solr\WEB-INF\lib文件夹中。

2、solrconfig.xml配置

在Solr对应core的配置文件中(如:SolrSingle\collection1\conf),打开solrconfig.xml文件,新增如下节点:

  <!--导入-->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>

然后在同一目录下新增data-config.xml文件。

3、data-config.xml文件配置如下:

一对一模式:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=SolrDemoDB;username=sa;password=123456" user="sa" password="123456" batchSize="100"/>
<document>
<entity
name="Article"
pk="ArticleId"
query="select * from Article where IsDelete=0"
deltaQuery="select ArticleId from Article where CreateDate > '${dataimporter.last_index_time}'"
deletedPkQuery="select ArticleId from Article where IsDelete=1"
deltaImportQuery="select * from Article where ArticleId='${dataimporter.delta.ArticleId}'"
transformer="ClobTransformer,HTMLStripTransformer,DateFormatTransformer" >
<field column="ArticleId" name="ArticleId" />
<field column="Title" name="Title" />
<field column="CreateDate" name="CreateDate" dateTimeFormat="yyyy-MM-dd" />
<field column="IsDelete" name="IsDelete" />
<field column="Content" name="Content" />
<field column="TypeId" name="TypeId" />
<entity name="ArticleType" query="select * from ArticleType where TypeId=${Article.TypeId}">
<field column="ArticleTypeName" name="ArticleTypeName" />
</entity>
</entity>
</document>
</dataConfig>

<!--query:查询数据库表符合记录数据-->
<!--deltaQuery:查询出需要增量索引的数据,所有经过修改的记录的Id,可能是修改操作、添加操作、删除操作产生的(此查询只对增量导入起作用,而且只能返回Id值) -->
<!--deletedPkQuery:查询出需要删除的数据记录主键Id,solr通过它来删除索引里面对应的数据(此查询只对增量导入起作用,而且只能返回ID值)-->
<!--deltaImportQuery:次查询是获取以上两步的Id,然后把其全部数据获取,根据获取的数据,对索引库进行更新操作,可能是删除,添加,修改(此查询只对增量导入起作用,可以返回多个字段的值,一般情况下,都是返回所有字段的列)-->
<!--transformer:格式转化-->

对应在schema.xml文件中的field设置为:

    <!--Article Begin-->
<field name="ArticleId" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
<field name="Title" type="text_general" indexed="true" stored="true"/>
<field name="CreateDate" type="date" indexed="true" stored="true"/>
<field name="IsDelete" type="boolean" indexed="true" stored="true"/>
<field name="Content" type="text_general" indexed="true" stored="true"/>
<field name="TypeId" type="int" indexed="true" stored="true"/>
<field name="ArticleTypeName" type="string" indexed="true" stored="true" />
<!--Article End--> <uniqueKey>ArticleId</uniqueKey>

一对多模式:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=SolrDemoDB;username=sa;password=123456" user="sa" password="123456" batchSize="100"/>
<document>
<entity name="ArticleType" pk="TypeId" query="select * from ArticleType">
<field column="ArticleTypeName" name="ArticleTypeName" />
<field column="TypeId" name="TypeId" />
<entity name="Article" pk="ArticleId" query="select * from Article where IsDelete=0 and TypeId=${ArticleType.TypeId}">
<field column="ArticleId" name="ArticleId" />
<field column="Title" name="Title" />
<field column="CreateDate" name="CreateDate" dateTimeFormat="yyyy-MM-dd" />
<field column="IsDelete" name="IsDelete" />
<field column="Content" name="Content" />
<field column="TypeId" name="TypeId" />
</entity>
</entity>
</document>
</dataConfig>

对应在schema.xml文件中的field设置为:

    <!--Article Begin-->
<field name="ArticleId" type="string" indexed="true" stored="true" required="true" multiValued="true"/>
<field name="Title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="CreateDate" type="date" indexed="true" stored="true" multiValued="true"/>
<field name="IsDelete" type="boolean" indexed="true" stored="true" multiValued="true"/>
<field name="Content" type="text_general" indexed="true" stored="true" multiValued="true"/> <field name="TypeId" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
<field name="ArticleTypeName" type="string" indexed="true" stored="true" />
<!--Article End--> <uniqueKey>TypeId</uniqueKey>

4、在数据库中添加相应的数据库及数据表

此文示例数据库为SolrDemoDB,示例数据表为:Article。

5、重启tomcat,访问http://localhost:8080/solr/。看到如下页面,则说明成功。

6、solr dataimport commond

全量索引:http://ip:port/webapp_name/core_name/dataimport?command=full-import&clean=false&commit=true
增量索引:http://ip:port/webapp_name/core_name/dataimport?command=delta-import&clean=false&commit=true

备注:可在Client端通过http请求,来发送命令。

参数说明:

  • full-import : "全量导入"这个操作可以通过访问URL http://:/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://:/solr/dataimport?command=delta-import . 它同样支持  clean, commit, optimize and debug 这几个参数.

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

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

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

转载请保留本文地址:http://www.cnblogs.com/wangwangfei/p/3598462.html

Solr学习笔记之3、Solr dataimport - 从SQLServer导入数据建立索引的更多相关文章

  1. Solr学习笔记之2、集成IK中文分词器

    Solr学习笔记之2.集成IK中文分词器 一.下载IK中文分词器 IK中文分词器 此文IK版本:IK Analyer 2012-FF hotfix 1 完整分发包 二.在Solr中集成IK中文分词器 ...

  2. Solr学习笔记之1、环境搭建

    Solr学习笔记之1.环境搭建 一.下载相关安装包 1.JDK 2.Tomcat 3.Solr 此文所用软件包版本如下: 操作系统:Win7 64位 JDK:jdk-7u25-windows-i586 ...

  3. Solr学习笔记之5、Component(组件)与Handler(处理器)学习

    Solr学习笔记之5.Component(组件)与Handler(处理器)学习 一.搜索篇 拼写检查(spellCheck) 作用:用来检查用户输入的检索内容是否存在,如果不存在则给它提示出相近或相似 ...

  4. Solr学习笔记之4、Solr配置文件简介

    Solr学习笔记之4.Solr配置文件简介 摘自<Solr in Action>. 1. solr.xml – Defines one or more cores per Solr ser ...

  5. solr学习笔记-入门

    solr学习笔记 1.安装前准备 solr依赖java 8 运行环境,所以我们先安装java.如果没有java环境无法启动solr服务,并且会看到如下提示: [root@localhost solr- ...

  6. SQL反模式学习笔记22 伪键洁癖,整理数据

    目标:整理数据,使不连续的主键Id数据记录变的连续. 反模式:填充断档的数据空缺. 1.不按照顺序分配编号 在插入新行时,通过遍历表,找到的第一个未分配的主键编号分配给新行,来代替原来自动分配的伪主键 ...

  7. 《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据

    相关博客: 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA) <机器学习实战>学习笔记第十三章 —— 利用PCA来简化数据 奇异值分解(SVD)原理与在降维中的应用 机器学习( ...

  8. solr学习笔记

    目录 前言 linux部署 使用 配置 使用 前言 solr是apach基于Lucene开发的成熟的框架,这里我们学习如何部署.使用.关于集群会在后面继续添加 linux部署 mkdir /usr/l ...

  9. Solr学习笔记-在Tomcat上部署执行Solr

    上一篇我们初识了Solr而且学习了Jetty的启动方式.查看了Solr的管理界面,这一篇我们来实如今Tomcat上部署执行Solr. 部署环境: window7 jdk1.6.0_14 Solr-4. ...

随机推荐

  1. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  2. **对比$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')

    最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下. public function __construct($token, $wxuser = ''){ $ ...

  3. ServletContext与ServletConfig的详解及区别

    转自http://hi.baidu.com/huaxuelili/item/1704a03dbb5cd7f22784f4c6 一.ServletContext详解ServletContext是serv ...

  4. Enter直接登录

    2.2  按Enter键调用登录按钮 [实例描述] 为了方便用户操作,在登录邮箱或论坛时,如果用户输入了用户名和密码,按Enter键时,都会自动调用登录按钮.本例学习如何实现此功能. [实现代码] & ...

  5. Xamarin如何生成Android项目的APK

    Xamarin如何生成Android项目的APK 首先需要选择Release模式生成项目.然后从“生成”菜单中选择Export Android Package命令,就可以导出APK包.APK保存在An ...

  6. Swift 1.1语言函数参数的特殊情况本地参数名外部参数名

    Swift 1.1语言函数参数的特殊情况本地参数名外部参数名 7.4  函数参数的特殊情况 声明定义有参函数时,为函数的每一个参数都定义了参数名称.根据参数名定义的形式不同,函数参数包括本地参数和外部 ...

  7. ural 1243. Divorce of the Seven Dwarfs

    1243. Divorce of the Seven Dwarfs Time limit: 1.0 secondMemory limit: 64 MB After the Snow White wit ...

  8. BZOJ2102 : [Usaco2010 Dec]The Trough Game

    暴力枚举答案然后检验. #include<cstdio> int n,m,i,j,k,a[100],b[100],cnt,ans;char s[20]; int main(){ for(s ...

  9. BZOJ3498 : PA2009 Cakes

    令三元环(i,j,k)中i>j>k,则每条边只需要从大点连向小点 设d[x]表示从x连出的边的条数 从1到n枚举点i,然后枚举所有与i相连的边(i,x)(x<i) 如果$d[x]\l ...

  10. chrome developer tool 调试技巧

    这篇文章是根据目前 chrome 稳定版(19.0.1084.52 m)写的, 因为 google 也在不断完善chrome developer tool, 所以 chrome 版本不同可能稍有差别. ...