Solr从数据库导入数据
- - CSDN博客互联网推荐文章一. 数据导入(DataImportHandler-DIH). DIH 是solr 提供的一种针对数据库、xml/HTTP、富文本对象导入到solr 索引库的工具包. apache-solr-dataimportscheduler-1.1.jar(增量导入使用). 数据库对应的jdbc驱动包这里使用的是Oracle oracle10g.ja放入Tomcat6.0.36/webapps/sol/WEB-INF/lib 中.
java -jar start.jar
<dataConfig> <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/docsearch" user="root" password="root" /> <document name="function"> <entity name="function" query="select functionid as id, name, description, uri as url, num as num_t, leader as leader_t, requestsample as requestsample_t, responsesample as responsesample_t, divideid as divideid_i, remark as remark_t, lastupdatetime as lastupdatetime_t, lastupdateperson as lastupdateperson_t, cachedata as cachedata_t, cachetime as cachetime_t, needlogin as needlogin_t, isapi as isapi_t, createtime as createtime_t, integratedapproach as integratedapproach_t, frequency as frequency_t, dataamount as dataamount_t, mode as mode_t, status_ as status_s, alteradvice as alteradvice_t from function"> </entity> </document> <document name="fields"> <entity name="fields" query="select fieldid as id, num as num_t, fieldname as fieldname_t, classify as classify_t, isnull as isnull_t, lettername as lettername_t, javatype as javatype_t, description as description_t, type as type_t, status_ as status_t, functionid as functionid_i from fields"> </entity> </document> </dataConfig>
<!-- solr client --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.5.0</version> </dependency>
public class SolrQuery { private static final String url = "http://localhost:8983/solr"; private static HttpSolrServer server = null; public static QueryResponse query(SolrParams params){ try { if(server==null){ server = new HttpSolrServer( url ); } return server.query(params); } catch (Exception e) { return new QueryResponse(); } } } ModifiableSolrParams params = new ModifiableSolrParams(); String q = "name:"+word+" or description:"+word+" or leader_t:"+word+" or url:"+word+" or num_t:"+word; params.set("q", q); // params.set("defType", "edismax"); params.set("start",0); params.set("rows", 100); params.set("wt", "josn"); QueryResponse res = SolrQuery.query(params); SolrDocumentList results = res.getResults(); if (logger.isDebugEnabled()) { logger.debug("q=" + q); logger.debug("results.size()=" + results.size()); }