solr连接MySQL8出现问题的解决方案。

org.apache.solr.client.solrj.SolrServerException: Server refused connection at: http://localhost:8080/solr/solrCore666
	at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:559)
	at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
	at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
	at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124)
	at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:68)
	at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:54)
	at org.abcdit.solr.test.ProductDaoTest.createIndex(ProductDaoTest.java:43)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
	at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
	at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
	at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:448)yi

POJO文件:

 1 package org.abcdit.solr.pojo;
 2 
 3 public class Product {
 4 
 5      private Integer pid; //INT(11) NOT NULL AUTO_INCREMENT COMMENT '商品编号',
 6      private String name; //VARCHAR(255) NULL DEFAULT NULL COMMENT '商品名称',
 7      private String catalogName; //VARCHAR(50) NULL DEFAULT NULL COMMENT '商品分类名称',
 8      private Double price; //DOUBLE NULL DEFAULT NULL COMMENT '价格',
 9      private String description; //LONGTEXT NULL COMMENT '商品描述',
10      private String picture; //VARCHAR(255) NULL DEFAULT NULL COMMENT '图片名称',
11 
12     public Integer getPid() {
13         return pid;
14     }
15 
16     public void setPid(Integer pid) {
17         this.pid = pid;
18     }
19 
20     public String getName() {
21         return name;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public String getCatalogName() {
29         return catalogName;
30     }
31 
32     public void setCatalogName(String catalogName) {
33         this.catalogName = catalogName;
34     }
35 
36     public Double getPrice() {
37         return price;
38     }
39 
40     public void setPrice(Double price) {
41         this.price = price;
42     }
43 
44     public String getDescription() {
45         return description;
46     }
47 
48     public void setDescription(String description) {
49         this.description = description;
50     }
51 
52     public String getPicture() {
53         return picture;
54     }
55 
56     public void setPicture(String picture) {
57         this.picture = picture;
58     }
59 
60     public Product(Integer pid, String name, String catalogName, Double price, String description, String picture) {
61         this.pid = pid;
62         this.name = name;
63         this.catalogName = catalogName;
64         this.price = price;
65         this.description = description;
66         this.picture = picture;
67     }
68 
69     public Product() {
70         super();
71     }
72 
73     @Override
74     public String toString() {
75         return "Product{" +
76                 "pid=" + pid +
77                 ", name='" + name + '\'' +
78                 ", catalogName='" + catalogName + '\'' +
79                 ", price=" + price +
80                 ", description='" + description + '\'' +
81                 ", picture='" + picture + '\'' +
82                 '}';
83     }
84 }

DAO层文件:

  1 package org.abcdit.solr.dao;
  2 
  3 import org.abcdit.solr.pojo.Product;
  4 
  5 import java.sql.*;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 import org.apache.solr.common.SolrInputDocument;
 10 
 11 public class ProductDao {
 12 
 13     private Connection connection;
 14     private PreparedStatement pst;
 15     private ResultSet rs;
 16 
 17     public List<Product> findAll(){
 18 
 19         List<Product> products = new ArrayList<>();
 20 
 21         try {
 22             //1.加载数据驱动
 23             Class.forName("com.mysql.cj.jdbc.Driver");
 24             //2.获取connection连接
 25             connection = DriverManager.getConnection(
 26                     "jdbc:mysql://localhost:3306/solr?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true",
 27                     "root",
 28                     "myroot"
 29             );
 30             //3.执行PreparedStatement,预编译
 31             pst = connection.prepareStatement("SELECT pid, name, catalog_name, price, description, picture FROM products");
 32             //4.执行SQL搜索
 33             rs = pst.executeQuery();
 34 
 35             Product prc = null;
 36             while (rs.next()){
 37                 prc = new Product();
 38 
 39                 prc.setPid(rs.getInt("pid"));
 40                 prc.setName(rs.getString("name"));
 41                 prc.setPrice(rs.getDouble("price"));
 42                 prc.setPicture(rs.getString("picture"));
 43                 prc.setCatalogName(rs.getString("catalog_name"));
 44                 prc.setDescription(rs.getString("description"));
 45 
 46                 products.add(prc);
 47             }
 48 
 49         } catch (Exception e) {
 50             e.printStackTrace();
 51         } finally {
 52             if (rs != null) {
 53                 try {
 54                     rs.close();
 55                 } catch (SQLException e) {
 56                     e.printStackTrace();
 57                 }
 58             }
 59             if (pst != null){
 60                 try {
 61                     pst.close();
 62                 } catch (SQLException e) {
 63                     e.printStackTrace();
 64                 }
 65             }
 66             if (connection != null){
 67                 try {
 68                     connection.close();
 69                 } catch (SQLException e) {
 70                     e.printStackTrace();
 71                 }
 72             }
 73 
 74         }
 75 
 76         return products;
 77     }
 78 
 79 
 80     public List<SolrInputDocument> getDocuments(List<Product> products) {
 81 
 82         List<SolrInputDocument> docs = new ArrayList<>();
 83         SolrInputDocument doc = null;
 84 
 85         for (Product product : products) {
 86 
 87             doc = new SolrInputDocument();
 88             doc.addField("id", product.getPid()); //编号
 89             doc.addField("productName", product.getName()); //名称
 90             doc.addField("productCatalogName", product.getCatalogName());//类别
 91             doc.addField("productPrice", product.getPrice()); //价格
 92             doc.addField("productPicture", product.getPicture());//图片
 93             doc.addField("productDescription", product.getDescription());//描述
 94 
 95             docs.add(doc);
 96 
 97         }
 98 
 99         return docs;
100 
101     }
102 
103 
104 
105 }

一、查看是否修改schema.xml,修改配置如下:

<!--product-->
<field name="id" type="string" indexed="true" stored="true" required="true" 
multiValued="false" />
<field name="productName" type="text_general" indexed="true" stored="true"/>
<field name="productCatalogName" type="string" indexed="true" stored="true" />
<field name="productPrice"  type="double" indexed="true" stored="true"/>
<field name="productDescription" type="text_general" indexed="true" stored="false" />
<field name="productPicture" type="string" indexed="false" stored="true" />

1.我们发现,索引库设置的字段名和数据库的字段名不相同,为什么呢?

在实际开发中,我们会保持索引库的字段和数据的字段一致的。本示例之所以配置不一样,是为了让我们知道,索引的字段不等同数据库的字段

连接mysql8具体配置:

查看我之前文章的链接 :  1.mybatis连接MySQL8数据库   2.mybatis连接MySQL8.0出现的问题

 

创建测试类:

 1 @Test
 2     public void createIndex(){
 3         // 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
 4         // 参数:solr服务器的访问地址
 5         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/solrCore0719");
 6         // 2、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。
 7         ProductDao dao = new ProductDao();
 8         try {
 9             server.add(dao.getDocuments(dao.getAllProducts()));
10             // 3、 提交。
11             server.commit();            
12             System.out.println("创建索引库成功!!!");
13             
14         } catch (SolrServerException e) {
15             e.printStackTrace();
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19     }

猜你喜欢

转载自www.cnblogs.com/abcdjava/p/11354104.html
今日推荐