英文:
How to connect ElasticSearch Index(Using Logstash with MySQL to feed the data) to Java Rest High Level Client(Spring Boot)
问题
以下是您提供的内容的翻译:
我能够通过Java High Level Rest Client连接到ElasticSearch索引,但我想了解以下内容:
- 使用POSTMAN手动创建了名为"students"的索引,使用PUT /students。
- 使用POSTMAN手动设置了字段,使用PUT /students/_mappings。
- 使用Logstash将数据从MySQL连续地传输到索引。
- 如何从Spring Boot应用程序连接到我们创建的这个特定索引,并在该索引中搜索文档?(注意:如果我使用Java Rest High Level Client自动创建索引,则可以连接到索引,但如果我使用Logstash并分离索引的创建,则似乎无法从索引中检索任何记录)。
这是我的Logstash配置文件:
input {
jdbc {
clean_run => true
jdbc_driver_library => "/../mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar"
jdbc_driver_class => "com.mysql.jdbc.driver"
jdbc_connection_string => "jdbc:mysql://localhost:3306/exampledb?autoReconnect=true&characterEncoding=UTF-8&verifyServerCertificate=false&useSSL=false&requireSSL=false"
jdbc_user => "user"
jdbc_password => "pass"
schedule => "* * * * *"
statement => "SELECT STUDENTID, FIRSTNAME, LASTNAME, AGE, PHONE, CITY FROM student WHERE STUDENTID > :sql_last_value;"
use_column_value => true
tracking_column => "studentid"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "students"
}
stdout {
codec => rubydebug
}
}
这是Student.class:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
@Data
@Document(indexName = "students", type = "students", shards = 1)
public class Student {
@Id
private long studentid;
private String firstname;
private String lastname;
private int age;
private String phone;
private String city;
}
这是我从中使用ElasticSearch查询的StudentDAO:
public List<Student> getAllStudents(String term, String order, String value) {
QueryBuilder query;
query = QueryBuilders.boolQuery()
.should(QueryBuilders.matchAllQuery())
.should(QueryBuilders.queryStringQuery("*" + term + "*")
.lenient(true)
.field("studentid")
.field("firstname")
.field("lastname")
.field("age")
.field("phone")
.field("city"));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0, 1000))
.build();
return elasticsearchTemplate.queryForList(nativeSearchQuery, Student.class);
}
这是PUT /students/_mappings:
{
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "keyword",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FIRSTNAME": {
"type": "keyword"
},
"LASTNAME": {
"type": "keyword"
},
"PHONE": {
"type": "keyword"
},
"AGE": {
"type": "keyword"
},
"CITY": {
"type": "keyword"
},
"STUDENTID": {
"type": "long"
}
}
}
英文:
I am able to connect to ElasticSearch Indexes when created through the Java High Level Rest Client but I want to know this:
- Created Index(students) manually using PUT /students in POSTMAN
- Set the Fields manually using PUT /students/_mappings in POSTMAN
- Feeding the Data from MySQL to the Index using Logstash continuously
- How to connect to this specific index that we created and search for documents in this index from the Spring boot application? (Note: Without using Logstash, I am able to connect to the index if I create it automatically using the Java Rest High Level Client, but if I use the Logstash and separate the Index creation, then it doesn't seem like its retrieving any records from the index).
This is my Logstash config file
input{
jdbc{
clean_run => true
jdbc_driver_library => "/../mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar"
jdbc_driver_class => "com.mysql.jdbc.driver"
jdbc_connection_string => "jdbc:mysql://localhost:3306/exampledb?autoReconnect=true&characterEncoding=UTF-8&verifyServerCertificate=false&useSSL=false&requireSSL=false"
jdbc_user => "user"
jdbc_password => "pass"
schedule => "* * * * *"
statement => "SELECT STUDENTID, FIRSTNAME, LASTNAME, AGE, PHONE, CITY FROM student WHERE STUDENTID > :sql_last_value;"
use_column_value => true
tracking_column => "studentid"
}
}
output{
elasticsearch{
hosts=>["localhost:9200"]
index=>"students"
}
stdout{
codec => rubydebug
}
}
This is Student.class
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
@Data
@Document(indexName = "students", type = "students", shards = 1)
public class Student {
@Id
private long studentid;
private String firstname;
private String lastname;
private int age;
private String phone;
private String city;
}
This is the StudentDAO from where I am using the ElasticSearch query.
public List<Student> getAllStudents(String term, String order, String value) {
QueryBuilder query;
query = QueryBuilders.boolQuery()
.should(
QueryBuilders.matchAllQuery()
)
.should(QueryBuilders.queryStringQuery("*" + term + "*").
lenient(true).field("studentid")
.field("firstname")
.field("lastname")
.field("age")
.field("phone")
.field("city"));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(query)
.withPageable(PageRequest.of(0,
1000))
.build();
return elasticsearchTemplate.queryForList(nativeSearchQuery, Student.class);
}
This is the PUT /students/_mappings
{
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "keyword",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FIRSTNAME": {
"type": "keyword"
},
"LASTNAME": {
"type": "keyword"
},
"PHONE": {
"type": "keyword"
},
"AGE": {
"type": "keyword"
},
"CITY": {
"type": "keyword"
},
"STUDENTID": {
"type": "long"
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论