java solr_通过Java访问Solr服务实例及相关配置
一、通過Java訪問Solr服務(wù)(手動(dòng)創(chuàng)建索引庫)
1、創(chuàng)建項(xiàng)目,配置環(huán)境(導(dǎo)包及相關(guān)文件)
1、SolrJ核心包 /solr-4.10.3/dist/solr-solrj-4.10.3.jar
2、SolrJ依賴包 /solr-4.10.3/dist/solrj-lib下的所有包
3、日志依賴包 /solr-4.10.3/example/lib/ext目錄下的所有jar包
4、JDBC驅(qū)動(dòng)包 mysql-connector-java-5.1.10-bin.jar
5、拷貝log4j.properties到src目錄下。(或者創(chuàng)建一個(gè)Source Folder)
項(xiàng)目結(jié)構(gòu)如下所示
2、編寫代碼
【1】采集數(shù)據(jù)
1】創(chuàng)建pojo,創(chuàng)建Product類
package cn.jinshan.pojo;
public class Product {
private Integer pid;
private String name;
private String catalogName;
private double price;
private String description;
private String picture;
// 補(bǔ)全get、set方法}
【2】創(chuàng)建連接數(shù)據(jù)的 ProductDao類
package cn.jinshan.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.jinshan.pojo.Product;
public class ProductDao {
public List getAllProducts() {
List products = new ArrayList<>();
//連接數(shù)據(jù)庫 Connection connection = null;
PreparedStatement prepareStatement=null;
ResultSet resultSet = null;
try {
//加載驅(qū)動(dòng) Class.forName("com.mysql.jdbc.Driver");
//獲取連接 connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/solr", "root", "gzsxt");
//獲取預(yù)處理編譯對(duì)象 prepareStatement = connection.prepareStatement("select pid,name,price,description,catalog_name,picture from products");
//執(zhí)行 resultSet = prepareStatement.executeQuery();
Product product = null;
while (resultSet.next()) {
product = new Product();
product.setPid(resultSet.getInt("pid"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getDouble("price"));
product.setDescription(resultSet.getString("description"));
product.setCatalogName(resultSet.getString("catalog_name"));
product.setPicture(resultSet.getString("picture"));
products.add(product);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (resultSet !=null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (prepareStatement != null) {
try {
prepareStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection !=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return products;
}
}
【3】創(chuàng)建一個(gè)測(cè)試類,測(cè)試能否取出數(shù)據(jù)
package cn.jinshan.test;
import org.junit.Test;
import cn.jinshan.dao.ProductDao;
public class ProductDaoTest {
@Test
public void getAll() {
ProductDao dao = new ProductDao();
System.out.println(dao.getAllProducts());
}
}
注:運(yùn)行測(cè)試類,控制臺(tái)可以打印出數(shù)據(jù),則證明數(shù)據(jù)采集正常
【2】將數(shù)據(jù)轉(zhuǎn)換成Solr文檔SolrInputDocument
1】將文檔的域配置在Solr實(shí)例的schema.xml配置文件中,如下圖所示:
2】通過代碼將數(shù)據(jù)轉(zhuǎn)為SolrInputDocument文檔
//將采集的數(shù)據(jù)轉(zhuǎn)為SolrInputDocument文檔 public List getSolrDocument(List products) {
List solrInputDocuments = new ArrayList<>();
SolrInputDocument solrInputDocument = null;
for (Product product : products) {
solrInputDocument = new SolrInputDocument();
//此處addField的域,必須跟實(shí)例的schema.xml配置文件中配置的域相同 solrInputDocument.addField("id", product.getPid());
solrInputDocument.addField("product_name", product.getName());
solrInputDocument.addField("product_catalog_name", product.getCatalogName());
solrInputDocument.addField("product_price", product.getPrice());
solrInputDocument.addField("product_description", product.getDescription());
solrInputDocument.addField("product_picture", product.getPicture());
solrInputDocuments.add(solrInputDocument);
}
return solrInputDocuments;
}
3】手動(dòng)創(chuàng)建索引庫(執(zhí)行方法后,課查看實(shí)例中是否生成數(shù)據(jù)(位置:D:\java\solr\solrHome\collection1\data\index))
//手動(dòng)創(chuàng)建索引庫 @Test
public void createIndex() {
ProductDao productDao = new ProductDao();
//將數(shù)據(jù)加入到Solr的索引庫 //第一步:連接Solr HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8888/solr/solr1");
try {
solrServer.add(productDao.getSolrDocument(productDao.getAllProducts()));
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
三、Solr服務(wù)插件及其他配置(通過配置文件系統(tǒng)自動(dòng)創(chuàng)建索引)
可以在管理界面直接從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到索引庫,不需要通過手動(dòng)創(chuàng)建索引的全部操作
1、安裝DataImport插件
【1】配置依賴包
將/solr-4.10.3/dist/solr-dataimporthandler-4.10.3.jar拷貝到/depJar/contrib/dataimporthandler/lib目錄下
將jdbc驅(qū)動(dòng)包拷貝到 /depJar/contrib/db/lib 目錄下
【2】 加載依賴包的相關(guān)配置
在相關(guān)實(shí)例的solrconfig.xml文件中,加載這兩個(gè)jar依賴,如下所示:
2、配置 數(shù)據(jù)庫表和solr域的映射關(guān)系
【1】創(chuàng)建配置文件
在solr實(shí)例的conf目錄下,配置數(shù)據(jù)庫映射文件data-config.xml(建議不要修改配置文件名字),文件內(nèi)容如下:
為數(shù)據(jù)庫連接的相關(guān)配置
...數(shù)據(jù)庫表和solr域的關(guān)聯(lián)相關(guān)配置(column為表的字段,name為solr域的名稱)
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/solr"
user="root"
password="gzsxt"/>
配置完成,重啟solr服務(wù);
訪問solr服務(wù)控制臺(tái)如下,則插件安裝完成
2、配置中文分析器 ( IKAnalyzer)
【1】 把IKAnalyzer2012FF_u1.jar添加到solr/WEB-INF/lib目錄下
【2】 拷貝IkAnalyzer的配置文件IKAnalyzer.cfg.xml到solr/WEB-INF/classes目錄下
IK Analyzer 擴(kuò)展配置
stopword.dic;
【3】 在schema.xml中自定義一個(gè)FieldType,指定中文分詞器IKAnalyzer
注:指定中文分詞器后,solr域中的個(gè)別域的類型就不能寫text_general就需要改成text_ik
總結(jié)
以上是生活随笔為你收集整理的java solr_通过Java访问Solr服务实例及相关配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R8500 MPv2 版本 刷梅林改版固
- 下一篇: java错误switch找不到符号,Ja