Skip to content

Commit dfdb0b0

Browse files
committed
springboot-17-elasticsearch(完)
1 parent c7a03f8 commit dfdb0b0

File tree

37 files changed

+1433
-0
lines changed

37 files changed

+1433
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ***SpringBoot-Elasticsearch***
2+
3+
# ***官方文档***
4+
5+
* **[Elasticsearch官方文档](https://www.elastic.co/guide/cn/index.html)**
6+
* **[Spring-Data-Elasticsearch官方文档](https://docs.spring.io/spring-data/elasticsearch/docs/3.1.4.RELEASE/reference/html/#elasticsearch.repositories)**
7+
8+
## **logstash与mysql数据同步**
9+
10+
* 现在mysql驱动
11+
* 创建mysql.conf文件
12+
* 将驱动与文件放入logstash根目录下
13+
* 修改mysql.conf文件信息
14+
15+
* 输入命令启动logstash
16+
```java
17+
logstash -f G:\springboot_tools\elasticsearch\elasticsearch\elasticsearch5.6.8\logstash\logstash-5.6.8\mysqllogstash\mysql.conf
18+
```
19+
20+
## **Elasticsearch**
21+
22+
* SpringBoot默认支持两种技术和ES交互
23+
* Jest
24+
* 默认不生效
25+
* 需导入 import io.searchbox.client.JestClient;
26+
* Elasticsearch
27+
28+
**<font color=red size = 6>ElasticSearch访问9300端口问题</font>**
29+
30+
* 本工程使用的Spring-data-elasticsearch版本是3.1.3
31+
* 本工程使用的ES是6.4.3
32+
* 去spring官网查找对应的版本
33+
* [Spring-data-elasticsearch与elasticsearch版本对照表](https://github.com/spring-projects/spring-data-elasticsearch)
34+
35+
36+
| Spring-data-elasticsearch版本 |elasticsearch版本|
37+
|---|---|
38+
| 3.2.x|6.5.0|
39+
| 3.1.x |6.2.2|
40+
| 3.0.x |5.5.0|
41+
| 2.1.x |2.4.0|
42+
| 2.0.x |2.2.0|
43+
| 1.3.x |1.5.2|
44+
45+
<font color=red size=6>当出现应用无法启动的情况</font>
46+
* 安装ES的版本号与ES的jar包版本号对应
47+
* 本工程的ES的jar包的版本号与安装的ES对应
48+
49+
* 你安装的ES版本与工程中的elasticsearch的jar包版本一致,再去查看elasticsearch的jar包版本与Spring-data-elasticsearch版本是否对应
50+
51+
* 出现以下错误查看是不是版本的问题
52+
```java
53+
2019-01-23 22:19:48.898 ERROR 2968 --- [ main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MapperParsingException: No type specified for field [title]
54+
```
55+
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
input {
2+
jdbc {
3+
# mysql jdbc connection string to our backup databse
4+
jdbc_connection_string => "jdbc:mysql://localhost:3306/springboot-elasticsearch?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"
5+
# the user we wish to excute our statement as
6+
jdbc_user => "root"
7+
jdbc_password => "123456"
8+
# the path to our downloaded jdbc driver
9+
jdbc_driver_library => "G:/springboot_tools/elasticsearch/elasticsearch/elasticsearch5.6.8/logstash/logstash-5.6.8/mysqllogstash/mysql-connector-java-8.0.14.jar"
10+
# the name of the driver class for mysql
11+
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
12+
jdbc_paging_enabled => "true"
13+
jdbc_page_size => "50000"
14+
#以下对应着要执行的sql的绝对路径。
15+
#statement_filepath => ""
16+
statement => "select id,title,content from article"
17+
#定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)
18+
schedule => "* * * * *"
19+
}
20+
}
21+
22+
output {
23+
elasticsearch {
24+
#ESIP地址与端口
25+
hosts => "localhost:9200"
26+
#ES索引名称(自己定义的)
27+
index => "springboot_elasticsearch"
28+
#自增ID编号
29+
document_id => "%{id}"
30+
document_type => "article"
31+
}
32+
stdout {
33+
#以JSON格式输出
34+
codec => json_lines
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>SpringBootLearn</artifactId>
7+
<groupId>com.clown</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>springboot-17-elasticsearch</artifactId>
12+
<packaging>pom</packaging>
13+
<modules>
14+
<module>springboot-17-elasticsearch-article</module>
15+
<module>springboot-17-elasticsearch-search</module>
16+
<module>springboot-17-elasticsearch-jest</module>
17+
</modules>
18+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-17-elasticsearch</artifactId>
7+
<groupId>com.clown</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>springboot-17-elasticsearch-article</artifactId>
12+
13+
<dependencies>
14+
15+
<!-- DataJpa-->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-data-jpa</artifactId>
19+
</dependency>
20+
21+
<!-- mysql 驱动-->
22+
<dependency>
23+
<groupId>mysql</groupId>
24+
<artifactId>mysql-connector-java</artifactId>
25+
<scope>runtime</scope>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.clown.article;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author: Richard·Ackerman
8+
* @create: 2019/1/23
9+
**/
10+
@SpringBootApplication
11+
public class ArticleApplication {
12+
public static void main(String[] args){
13+
SpringApplication.run(ArticleApplication.class);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.clown.article.config;
2+
3+
import com.clown.article.utils.IdWorker;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* @author: Richard·Ackerman
9+
* @create: 2019/1/22
10+
**/
11+
@Configuration
12+
public class MyConfig {
13+
14+
@Bean
15+
public IdWorker idWorker(){
16+
return new IdWorker(1,1);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.clown.article.controller;
2+
3+
import com.clown.article.model.Article;
4+
import com.clown.article.service.IArticleService;
5+
import com.clown.article.utils.Result;
6+
import com.clown.article.utils.StatusCode;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
@CrossOrigin
13+
@RestController
14+
@RequestMapping("/article")
15+
public class ArticleController {
16+
17+
@Autowired
18+
private IArticleService articleService;
19+
20+
/**
21+
* 查询所有文章
22+
* @return
23+
*/
24+
@GetMapping
25+
public Result findAll(){
26+
List<Article> articleList = articleService.findAll();
27+
if (null == articleList) {
28+
return new Result(false, StatusCode.ERROR,"无数据");
29+
}
30+
return new Result(true, StatusCode.OK,"查询成功",articleList);
31+
}
32+
33+
/**
34+
* 根据id查询文章
35+
* @param articleId
36+
* @return
37+
*/
38+
@GetMapping("/{articleId}")
39+
public Result findById(@PathVariable("articleId") String articleId){
40+
Article article = articleService.findById(articleId);
41+
if (null==article) {
42+
return new Result(false, StatusCode.ERROR,"无数据");
43+
}
44+
return new Result(true, StatusCode.OK,"查询成功",article);
45+
}
46+
47+
/**
48+
* 添加文章
49+
* @param article
50+
* @return
51+
*/
52+
@PostMapping
53+
public Result add(@RequestBody Article article){
54+
try {
55+
articleService.add(article);
56+
return new Result(true, StatusCode.OK,"添加成功");
57+
} catch (Exception e) {
58+
return new Result(false, StatusCode.ERROR,"添加失败");
59+
}
60+
}
61+
62+
/**
63+
* 更新文章
64+
* @param articleId
65+
* @param article
66+
* @return
67+
*/
68+
@PutMapping("/{articleId}")
69+
public Result update(@PathVariable("articleId") String articleId,@RequestBody Article article){
70+
try {
71+
article.setId(articleId);
72+
articleService.update(article);
73+
return new Result(true, StatusCode.OK,"更新成功");
74+
} catch (Exception e) {
75+
return new Result(false, StatusCode.ERROR,"更新失败");
76+
}
77+
}
78+
79+
/**
80+
* 删除文章
81+
* @param articleId
82+
* @return
83+
*/
84+
@DeleteMapping("/{articleId}")
85+
public Result delete(@PathVariable("articleId") String articleId){
86+
87+
try {
88+
articleService.delete(articleId);
89+
return new Result(true, StatusCode.OK,"删除成功");
90+
} catch (Exception e) {
91+
return new Result(false, StatusCode.ERROR,"删除是啊比");
92+
}
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.clown.article.mapper;
2+
3+
import com.clown.article.model.Article;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6+
7+
public interface ArticleDao extends JpaRepository<Article,String>, JpaSpecificationExecutor<Article> {
8+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.clown.article.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.hibernate.annotations.DynamicInsert;
7+
import org.hibernate.annotations.DynamicUpdate;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.Table;
12+
import java.io.Serializable;
13+
import java.util.Date;
14+
15+
@Data
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
@Entity
19+
@Table(name = "article")
20+
@DynamicInsert
21+
@DynamicUpdate
22+
public class Article implements Serializable {
23+
24+
@Id
25+
private String id; //ID
26+
27+
private String columnid; //专栏ID
28+
29+
private String userid; //用户ID
30+
31+
private String title; //标题
32+
33+
private String content; //文章正文
34+
35+
private String image; //文章封面
36+
37+
private Date createtime; //发表日期
38+
39+
private Date updatetime; //修改日期
40+
41+
private String ispublic; //是否公开 0:不公开 1:公开
42+
43+
private String istop; //是否置顶 0:不置顶 1:置顶
44+
45+
private Long visits; //浏览量
46+
47+
private Long thumbup; //点赞数
48+
49+
private Long comment; //评论数
50+
51+
private String state; //审核状态 0:未审核 1:审核通过
52+
53+
private String channelid; //所属频道
54+
55+
private String url; //url
56+
57+
private String type; //类型 0:分享 1:专栏
58+
59+
}

0 commit comments

Comments
 (0)