设为首页 收藏本站
查看: 841|回复: 0

[经验分享] Solr 4.x 全量索引

[复制链接]

尚未签到

发表于 2016-12-16 08:49:19 | 显示全部楼层 |阅读模式
  以solr4.6.1为例说明。 
  一、准备工作
  1. 拷贝solr-dataimporthandler-4.6.1.jar到Tomcat的Solr lib目录中
  在下载的solr的相关目录,如C:\Software\solr-4.6.1\dist中将solr-dataimporthandler-4.6.1.jar拷贝到C:\Software\apache-tomcat-7.0.50\webapps\solr\WEB-INF\lib 中。
  并确保mysql的驱动包在Tomcat\lib目录下。
  2. 假设在Solr Home(C:\solr-tomcat\solr)中现在有一个Solr core实例,名字为“item”(C:\solr-tomcat\solr\item)当然你可以使用你已有的core实例,或者是新建一个core实例。
  二、solrconfig.xml中配置DataImportHandler
  修改位于C:\solr-tomcat\solr\item\conf下的solrconfig.xml,加入如下内容:

<requestHandler name="/dataimport"  
class="org.apache.solr.handler.dataimport.DataImportHandler">   
<lst name="defaults">   
<str name="config">C:\solr-tomcat\solr\item\conf\data-config.xml</str>   
</lst>   
</requestHandler>  
  注意C:\solr-tomcat\solr\item\conf\data-config.xml前一定不能有空格,否则会出现“找不到相关资源”类似的Exception:Can't find resource '  C:\solr-tomcat\solr\item\conf\data-config.xml ' in classpath or 'C:\solr-tomcat\solr\item\conf'
  三、在solrconfig.xml同级目录中,即在C:\solr-tomcat\solr\item\conf中,增加上面的data-config.xml
  在其中定义数据源,定义实体类。

<dataConfig>
<dataSource name="solrDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/solr" user="root" password="tools2013"/>
<document>
    <entity dataSource="solrDB" name="solr_item"  query="select * from solr_item">
        <field column="ID" name="id"/>
        <field column="NAME" name="name"/>   
        <field column="POPULRITY" name="popularity"/>
    </entity>
</document>
</dataConfig>
  注意:1. URL中 jdbc:mysql://localhost/solr中不能有端口号。
  2. 需要将数据库的驱动包放在Tomcat的lib目录下。
  3. 将url,username和password换成相应的url,username,password.
  4. 其中jdbc:mysql://localhost/solr中的solr是数据库名
  5. 其中solr_item是表名。
   四、需要在Schema.xml中定义不存在的field的信息(本例中除id外的field都不存在,所以都需要定义):

<field name="POPULRITY" type="string" indexed="true" stored="true" omitNorms="true"/>  
<field name="NAME" type="string" indexed="true" stored="true" omitNorms="true"/>
  五、访问solr管理页面点击collection item中的Dataimport,你会发现Dataimport中有了刚刚配置的DataImporthandler相关的信息。
  勾选实体类并勾选commmit及optimize然后点击execute即开始执行导入。也可以选择进行full-import或者是delta-import.
  六、当然你也可以通过代码的方式来进行full-import或者是delta-import. 如下是相关的代码示例。
  1. full import:需要注意的是full import将会删除之前所有的索引并重新做索引,所以当数据量大的时候,性能会比较低,善用。
  代码片段为:
  1)AbstractSolrServer类用于构建Solr的连接

package com.wsheng.solr;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
/**
* @author Josh Wang(Sheng)
*
* @email  josh_wang23@hotmail.com
*/
public class AbstractSolrServer {
/**TODO: Using Spring injection*/
public static SolrServer server;
static {
server = new HttpSolrServer("http://localhost:9898/solr/item"); // 其中item为solr core
}
}

 

  2) SolrImport类用于构建Solr的导入类

package com.wsheng.solr.command;
import com.wsheng.solr.AbstractSolrServer;
import com.wsheng.solr.util.HttpUtils;
/**
* The API also should be moved to ISolrActionService
*
* @author Josh Wang(Sheng)
*
* @email  josh_wang23@hotmail.com
*/
public class SolrImport extends AbstractSolrServer {
private static String CONFIGURATION_URL = "http://localhost:9898/solr/item/dataimport";
private static String FULL_IMPORT_URL = "http://localhost:9898/solr/item/dataimport?command=full-import";
private static String DELTA_IMPORT_URL = "http://localhost:9898/solr/item/dataimport?command=delta-import";

public static String verify() throws Exception {
return HttpUtils.handleSolrReq(CONFIGURATION_URL);
}
public static void fullImport() throws Exception {
String result = HttpUtils.handleSolrReq(FULL_IMPORT_URL);
System.out.println("Import Ended " + result);
}
public static void deltaImport() throws Exception {
String result = HttpUtils.handleSolrReq(DELTA_IMPORT_URL);
System.out.println("Import Ended " + result);
}
}

 

   3)  HttpUtils类为使用jersey的方式来执行Solr的导入等Http请求
package com.wsheng.solr.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
* @author Josh Wang(Sheng)
* @email swang6@email.com
*/
public class HttpUtils {
private static Log logger = LogFactory.getLog(HttpUtils.class);
public static String handleSolrReq(String url) throws Exception {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
logger.error("Failed...." + response.getStatus());
System.out.println("Failed...." + response.getStatus() + "  " + response.getEntity(String.class));
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String result = response.getEntity(String.class);
return result;
}

}

        @Test
public void fullImport() {
try {
// full Import will re-index and replace the former ones
SolrImport.fullImport();
this.query();
System.out.println("=============");
this.queryByManu();
} catch (Exception e) {
e.printStackTrace();
}
}
执行该Unit Test即可完成索引的索引的全部删除与重新构建。  PS:每次在run玩full-import后可查看solr home下相关index目录中文件的变化:如本例中可查看C:\solr-tomcat\solr\item\data\index folder下文件的修改时间。
  2、关于增量索引可访问另一篇博文: Solr 4.x 定时、实时增量索引 - 修改、删除和新增索引

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-314939-1-1.html 上篇帖子: 在tomcat中运行solr3.4(参看solr 3.1 cookbook) 下篇帖子: solr 处理数据库数据索引 DataImportHandler 的使用[转]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表