solr 有几种导入数据的方式

solr 有几种导入数据的方式,第1张

solr数据导入,经过这几天的查资料,我觉得solr数据导入可以有三种方式:

1、编写数据xml文件,通过post.jar导入;

2、宏洞通过DIH导入;

3、利用solrj导入数据;

现针对第三种方式进行研究,在第一步中写了一段小的测试代码,可以参考:http://wiki.apache.org/solr/Solrj#Streaming_documents_for_an_update

具体的代码解释如下:

String url = "http://localhost:8080/solr"

HttpSolrServer server = new HttpSolrServer(url)

//If you wish to delete all the data from the index, do this

//server.deleteByQuery( "*:*" )

//Construct a document

SolrInputDocument doc1 = new SolrInputDocument()

doc1.addField( "id", "id1_solrj" )

doc1.addField( "type", "doc1_solrj" )

doc1.addField( "name", "name1_solrj" )

//Construct another document

SolrInputDocument doc2 = new SolrInputDocument()

doc2.addField( "id", "id2" )

doc2.addField( "type", "doc2_solrj" )

doc2.addField( "name", "name2_solrj" )

//Create a collection of documents

Collection<SolrInputDocument>docs = new ArrayList<SolrInputDocument>()

docs.add(doc1)

docs.add(doc2)

//Do a commit

try {

server.add(docs)

server.commit()

} catch (SolrServerException e) {

System.out.println("server commit error, error code:")

e.printStackTrace()

} catch (IOException e) {

System.out.println("server commit error, error code:")

e.printStackTrace()

}

}

该端代码执行后报异常:expect mime type application/octet-stream but got text/html

没找到这个的解决办法,根据提示好像是说期望的类型和服务器反馈的类型不匹配

最后的解决办法是这样的:

之前在配置solr服务器的时候将solr解压路径\solr-4.8.1\example\solr下的solr.xml用\solr-4.8.1\example\multicore下的solr.xml文件进行了替换,目的是为了引入core0和core1,现在需要将这个动作进行回滚,并且修改collection1下的conf下的schema.xml文件,修改为对应的需要的列定义。然后执行以上的代码就不会产生问题。

原因我也不太明白,感觉应该是collection1的配置和core1、core0、乃至之前文章提到过的solrtest的配置应该不太一样。原消岩因待查。不过现在已经可以通过客户端的方式将数据导入solr服务器,拿绝御并在前端可以查询到相应的数据。

solr的三空察个配置文件:

1、solrconfig.xml

只配置一次就够了

2、data-config.xml

配置斗友茄数据库与solr搜索的映射告闹关系,需要按实际情况处理

3、schema.xml

配置solr搜索字段

在solr与tomcat整合文章中,我段竖用的索引库是mycore,现在就以这个为例。

首先要准备jar包:solr-dataimporthandler-4.8.1.jar、solr-dataimporthandler-extras-4.8.1.jar和mysql-connector-java-5.0.7-bin.jar这三个包到solr的tomcat的webapps\solr\WEB-INF\lib下

在这个文件夹的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>

在solrconfig.xml的同目录下创建data-config.xml。

配置:

复制代码

<dataConfig>

<dataSource type="JdbcDataSource"纳则

driver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/courseman"

user="root"

password="mysql" />

<document>

<entity name="student"

query="SELECT * FROM student">

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

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

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

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

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

</entity>

</document>

</dataConfig>

复制代码

schemal.xml的配置

复制代码

<?xml version="1.0" ?>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11989497.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存