繼續上一篇的學習
今天講解的是
使用SpringDataElasticSerach完成搜尋微服務
logstash完成mysql與ElasticSearch的同步
1,使用SpringDataElasticSerach完成搜尋微服務
pom檔案導入依賴的jar包
<dependencies>
<!--elasticsearch 包-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>tom.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
修改aplication.yml配置檔案
server:
port: 9007
spring:
application:
name: tensquare-search
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
建立實體類
package com.tensquare.search.pojo;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import java.io.Serializable;
@Document(indexName = "tensquare_article",type = "article")
//相當于資料庫名稱和表名稱
public class Article implements Serializable {
@Id
private String id;//ID
//是否索引 就是看該域能否被搜尋
//是否分詞 搜尋的時候是全文進行比對還是單詞進行比對
//是否存儲 是否顯示在頁面上
// 建立的時候 查詢的時候
@Field(index= true ,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String title;//标題
@Field(index= true ,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String content;//文章正文
private String state;//稽核狀态
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
上面都有注釋,仔細看看
建立dao層
public interface ArticleDao extends ElasticsearchRepository<Article,String> {
}
注意他要通過繼承ElasticsearchRepository<Article,String>來實作對其的操作,使用的是springdata
建立其他的service和conntroller層
2,logstash完成mysql與ElasticSearch的同步
安裝:
直接解壓就行
測試:
進入bin目錄
輸入:logstash ‐e 'input { stdin { } } output { stdout {} }'
(1)在logstash-5.6.8安裝目錄下建立檔案夾mysqletc (名稱随意)
(2)檔案夾下建立mysql.conf (名稱随意) ,内容如下:
input {
jdbc {
# mysql jdbc connection string to our backup databse
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/tensquare_article?characterEncoding=UTF8"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "969188"
# the path to our downloaded jdbc driver
jdbc_driver_library => "E:\logstash-5.6.8\mysqletc\mysql-connector-java-5.1.46.jar"
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50"
#以下對應着要執行的sql的絕對路徑。
#statement_filepath => ""
statement => "select id,title,content,state from tb_article"
#定時字段 各字段含義(由左至右)分、時、天、月、年,全部為*預設含義為每分鐘都更新(測試結果,不同的話請留言指出)
schedule => "* * * * *"
}
}
output {
elasticsearch {
#ESIP位址與端口
hosts => "127.0.0.1:9200"
#ES索引名稱(自己定義的)
index => "tensquare_article"
#自增ID編号
document_id => "%{id}"
document_type => "article"
}
stdout {
#以JSON格式輸出
codec => json_lines
}
}
(3)将mysql驅動包mysql-connector-java-5.1.46.jar拷貝至D:/logstash5.6.8/mysqletc/ 下 。D:/logstash-5.6.8是你的安裝目錄
(4)執行指令
logstash ‐f ../mysqletc/mysql.conf |