花了一天半的時間研究了一下Elasticsearch:java
1)安裝Elasticsearch:下載zip包,解壓以後,可將整個文件夾拷到C盤根目錄,在C:\elasticsearch-6.6.2\bin目錄下執行elasticsearch.bat, 便可啓動,訪問 http://localhost:9200/ 驗證是否安裝成功。node
2)安裝Elasticsearch Head:預先安裝node.js, 拉取源代碼, 根據github上的安裝提示進行啓動,訪問 http://localhost:9100/ 驗證是否安裝成功。mysql
3)用Elasticsearch Head 訪問 Elasticsearch, 根據git
須要在Elasticsearch配置跨源訪問,在C:\elasticsearch-6.6.2\config\elasticsearch.yml添加github
http.cors.enabled: true
http.cors.allow-origin: "*"spring
訪問 http://localhost:9100/ 鏈接elasticsearch, 可經過head對數據進行查詢。sql
4)安裝Kibana,下載zip包,解壓以後經過bin\kibana.bat啓動,經過 http://localhost:5601 可訪問。數據庫
5)在Spring Boot項目中使用Elasticsearch,參考json
6) Logstash的安裝和使用,下載zip包,解壓並進入bin目錄,經過一下指令可驗證Logstash是否工做(注意,要用雙引號,而非單引號,不然會出錯)ruby
logstash -e "input { stdin { } } output { stdout {codec=>rubydebug} }"
7)使用Logstash將mysql數據庫中的數據導入到Elasticsearch,寫好配置文件,對象爲本地數據庫xiasha中atom表,內容以下:
input { jdbc { jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/xiasha?serverTimezone=Asia/Shanghai&characterEncoding=UTF8" jdbc_user => "root" jdbc_password => "root" jdbc_driver_library => "C:\Users\zhusi\.m2\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_paging_enabled => "true" jdbc_page_size => "20" statement => "SELECT id, name FROM atom" schedule => "* * * * *" } } output { elasticsearch { hosts => "127.0.0.1:9200" index => "atom" document_id => "%{id}" } stdout { codec=>json_lines } }
Logstash的基本流程,也是配置文件的主要內容:
注意建立conf文件時將編碼格式改成UTF-8,內容不要有中文。導入成功後,可經過Head來查詢導入的數據,注意_index和_type的值!如:
8)經過寫一個Controller訪問Atom。
Atom在Spring Boot中已經存在,且被JPA @Entity標註。經過第5)步的流程增長對Elasticsearch中Atom的訪問。(注意indexName和type的值與Head中查詢到的_index和_type相對應)
package hello.model; import org.springframework.data.elasticsearch.annotations.Document; import javax.persistence.*; @Entity @Document(indexName="atom",type="doc",refreshInterval="-1") public class Atom { @org.springframework.data.annotation.Id private Long id; private String name; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(length = 5) public String getName() { return name; } public void setName(String name) { this.name = name; } }
這樣直接啓動Spring Boot的話,會有失敗,有‘No property index found for type ’ 異常, 可參考
須要將JPA和Elasticsearch的repository文件放在兩個不一樣包裏,並在啓動類添加相應的配置指定basePackages。
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling @EnableJpaRepositories(basePackages = {"hello.dao"}) @EnableElasticsearchRepositories(basePackages = "hello.es") public class Application { public static void main(String[] args) { // The SpringApplication class, providing static convenience methods that make it easy to write a stand-alone Spring Application. // Its sole job is to create and refresh an appropriate Spring ApplicationContext SpringApplication.run(Application.class, args); } }
在Spring Boot中同時使用JPA和Elasticsearch的關係: