一、实现功能
使用solrj实现对solrcloud的增删改查,从而使整个项目可以实现在项目中应用。
二、环境
1、 solr-4.10.3;
2、 zookeeper-3.4.6;
3、 apache-tomcat-8.5.12;
4、 jdk1.7;
5、 centos6.4;
三、具体代码
1、 添加依赖;
(1)solrj依赖
<!--solr-->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solr.version}</version>
</dependency>
(2)mysql依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
2、 导入单个索引;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.common.SolrInputDocument;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class solrcloudIndexManager {
public void addDocument() throws Exception{
//使用SolrJ连接集群
//参数:zkHost是一个zookeeper集群列表
String zkHost="192.168.184.3:2181,192.168.184.3:2182,192.168.184.3:2183";
CloudSolrServer solrserver=new CloudSolrServer(zkHost);
//设置默认的实例collection
solrserver.setDefaultCollection("collection2");
//创建一个文档对象
SolrInputDocument document=new SolrInputDocument();
document.setField("id","solr cloud 00001");
document.setField("title","solr cloud非常棒,66666");
//提交文档
solrserver.add(document);
//提交修改
solrserver.commit();
}
public static void main(String[] args) throws Exception{
new solrcloudIndexManager().addDocument();
}
}
导入结果
3、 将mysql数据批量导入solr;
(1)修改schema.xml并且按照https://blog.csdn.net/u010886217/article/details/89737832中步骤重新上传配置文件到zookeeper,但是,因为此实例的域名和类型默认的schema.xml都有,所以,不需要上传,默认schema.xml中含有,如下图
(2)代码
package solr;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.common.SolrInputDocument;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class solrcloudIndexManager {
public void importAllItems() throws Exception {
//创建solrJ客户端
String zkHost = "192.168.184.3:2181,192.168.184.3:2182,192.168.184.3:2183";
CloudSolrServer solrServer = new CloudSolrServer(zkHost);
//设置默认的collection
solrServer.setDefaultCollection("collection2");
//使用JDBC连接数据库
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql://hadoop01:3306/test", "root", "Root_123");
//sql语句
String sql = "select * from products";
//创建preparedStatement
preparedStatement = connection.prepareStatement(sql);
//执行查询
resultSet = preparedStatement.executeQuery();
//向solr中导入数据
while(resultSet.next()) {
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
document.addField("id", resultSet.getString("pid"));
document.addField("name", resultSet.getString("name"));
document.addField("category", resultSet.getString("catalog_name"));
document.addField("price", resultSet.getString("price"));
document.addField("content", resultSet.getString("description"));
document.addField("url", resultSet.getString("picture"));
//向solr中添加记录
solrServer.add(document);
}
//提交修改
solrServer.commit();
System.out.println("数据导入完成!");
}
public static void main(String[] args) throws Exception{
new solrcloudIndexManager().importAllItems();
}
}
(3)测试结果
(一路坎坷,一路填坑~)
版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: