《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
代码测试
调用接口进行添加数据
新增数据:
POST http://localhost:8086/api/user
{“id”:1,“name”:“张三”,“age”:20,“description”:“张三是个Java开发工程师”,“createtm”:“2018-4-25 11:07:42”}
{“id”:2,“name”:“李四”,“age”:24,“description”:“李四是个测试工程师”,“createtm”:“1980-2-15 19:01:32”}
{“id”:3,“name”:“王五”,“age”:25,“description”:“王五是个运维工程师”,“createtm”:“2016-8-21 06:11:32”}
进行全文查询请求
进行全文查询请求
http://localhost:8086/api/user?searchContent=工程师
返回
[{“id”:2,“name”:“李四”,“age”:14,“description”:“李四是个测试工程师”,“createtm”: “1980-2-15 19:01:32”},
{“id”:1,“name”:“张三”,“age”:20,“description”:“张三是个Java开发工程师”, “createtm”: “2018-4-25 11:07:42”},
{“id”:3,“name”:“王五”,“age”:25,“description”:“王五是个运维工程师”,“createtm”: “2016-8-21 06:11:32”}]
进行分页查询请求
http://localhost:8086/api/user?pageNumber=0&pageSize=2&searchContent=工程师
返回
[{“id”:2,“name”:“李四”,“age”:14,“description”:“李四是个测试工程师”},{“id”:1,“name”:“张三”,“age”:20,“description”:“张三是个Java开发工程师”}]
进行权重查询请求
http://localhost:8086/api/user2?searchContent=李四
返回
[{“id”:2,“name”:“李四”,“age”:24,“description”:“李四是个测试工程师”,“createtm”:“1980-2-15 19:01:32”}]
权重查询打印的语句:
查询的语句:{{
“function_score” : {
“functions” : [ {
“filter” : {
“bool” : {
“should” : {
“match” : {
“name” : {
“query” : “李四”,
“type” : “boolean”
}
}
}
}
},
“weight” : 10.0
}, {
“filter” : {
“bool” : {
“should” : {
“match” : {
“description” : {
“query” : “李四”,
“type” : “boolean”
}
}
}
}
},
“weight” : 100.0
} ],
“min_score” : 2.0
}
}
注:测试中,因为设置了setMinScore最小权重分为2的,所以无关的数据是不会显示出来的。如果想显示的话,在代码中去掉即可。
新增完数据之后,可以在浏览器输入:http://localhost:9200/_plugin/head/ 然后点击基本查询,便可以查看添加的数据。如果想用语句查询,可以将程序中控制台打印的查询语句粘贴到查询界面上进行查询!
注:这里的ElasticSearch是我在windows上安装的,并安装了ES插件head,具体安装步骤在文章末尾。
除了SpringData之外,其实还有其它的方法操作ElasticSearch的。比如使用原生ElasticSearch的Api,使用TransportClient类实现。或者使用由Spring封装,只需在Service层,进行注入Bean即可。示例:
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
但是,上述方法中都有其局限性,也就是随着ElasticSearch的版本变更,相关的Java API也在做不断的调整,就是ElasticSearch服务端版本进行更改之后,客户端的代码可能需要重新编写。因此介绍一个相当好用的第三方工具JestClient,它对ElasticSearch进行封装,填补了 ElasticSearch HttpRest接口 客户端的空白,它适用于ElasticSearch2.x以上的版本,无需因为ElasticSearch服务端版本更改而对代码进行更改!
JestClient
首先在Maven中添加如下依赖:
io.searchbox jest 5.3.3然后编写相关的测试代码。代码中的注释应该很完整,所以这里就不再对代码过多的讲述了。
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import com.pancm.pojo.User;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.BulkResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
public class JestTest {
private static JestClient jestClient;
private static String indexName = “userindex”;
// private static String indexName = “userindex2”;
private static String typeName = “user”;
private static String elasticIps=“http://192.169.2.98:9200”;
// private static String elasticIps=“http://127.0.0.1:9200”;
public static void main(String[] args) throws Exception {
jestClient = getJestClient();
insertBatch();
serach1();
serach2();
serach3();
jestClient.close();
}
private static JestClient getJestClient() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(elasticIps).connTimeout(60000).readTimeout(60000).multiThreaded(true).build());
return factory.getObject();
}
public static void insertBatch() {
List objs = new ArrayList();
objs.add(new User(1L, “张三”, 20, “张三是个Java开发工程师”,“2018-4-25 11:07:42”));
objs.add(new User(2L, “李四”, 24, “李四是个测试工程师”,“1980-2-15 19:01:32”));
objs.add(new User(3L, “王五”, 25, “王五是个运维工程师”,“2016-8-21 06:11:32”));
boolean result = false;
try {
result = insertBatch(jestClient,indexName, typeName,objs);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(“批量新增:”+result);
}
/**
- 全文搜索
*/
public static void serach1() {
String query =“工程师”;
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery(query));
//分页设置
searchSourceBuilder.from(0).size(2);
System.out.println(“全文搜索查询语句:”+searchSourceBuilder.toString());
System.out.println(“全文搜索返回结果:”+search(jestClient,indexName, typeName, searchSourceBuilder.toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
- 精确搜索
*/
public static void serach2() {
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery(“age”, 24));
System.out.println(“精确搜索查询语句:”+searchSourceBuilder.toString());
System.out.println(“精确搜索返回结果:”+search(jestClient,indexName, typeName, searchSourceBuilder.toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
- 区间搜索
*/
public static void serach3() {
String createtm=“createtm”;
String from=“2016-8-21 06:11:32”;
String to=“2018-8-21 06:11:32”;
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.rangeQuery(createtm).gte(from).lte(to));
System.out.println(“区间搜索语句:”+searchSourceBuilder.toString());
System.out.println(“区间搜索返回结果:”+search(jestClient,indexName, typeName, searchSourceBuilder.toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
- 创建索引
- @param indexName
- @return
- @throws Exception
*/
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {
JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());
return jr.isSucceeded();
}
/**
- 新增数据
- @param indexName
- @param typeName
- @param source
- @return
- @throws Exception
*/
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {
PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();
JestResult jr = jestClient.execute(putMapping);
return jr.isSucceeded();
}
/**
- 查询数据
- @param indexName
- @param typeName
- @return
- @throws Exception
*/
public static String getIndexMapping(JestClient jestClient,String indexName, String typeName) throws Exception {
GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();
JestResult jr =jestClient.execute(getMapping);
return jr.getJsonString();
}
/**
- 批量新增数据
- @param indexName
- @param typeName
- @param objs
- @return
- @throws Exception
*/
public static boolean insertBatch(JestClient jestClient,String indexName, String typeName, List objs) throws Exception {
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);
for (Object obj : objs) {
Index index = new Index.Builder(obj).build();
bulk.addAction(index);
}
BulkResult br = jestClient.execute(bulk.build());
return br.isSucceeded();
}
/**
- 全文搜索
- @param indexName
- @param typeName
- @param query
- @return
- @throws Exception
*/
public static String search(JestClient jestClient,String indexName, String typeName, String query) throws Exception {
Search search = new Search.Builder(query)
.addIndex(indexName)
.addType(typeName)
.build();
JestResult jr = jestClient.execute(search);
// System.out.println(“–”+jr.getJsonString());
// System.out.println(“–”+jr.getSourceAsObject(User.class));
return jr.getSourceAsString();
}
/**
- 删除索引
- @param indexName
- @return
- @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName) throws Exception {
JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
return jr.isSucceeded();
}
/**
- 删除数据
- @param indexName
- @param typeName
- @param id
- @return
- @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName, String typeName, String id) throws Exception {
DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());
return dr.isSucceeded();
}
注:测试之前先说明下,本地windows系统安装的是ElasticSearch版本是2.3.5,linux服务器上安装的ElasticSearch版本是6.2。
测试结果
全文搜索
全文搜索查询语句:{
“from” : 0,
“size” : 2,
“query” : {
“query_string” : {
“query” : “工程师”
}
}
}
全文搜索返回结果:{“id”:1,“name”:“张三”,“age”:20,“description”:“张三是个Java开发工程师”,“createtm”:“2018-4-25 11:07:42”},{“id”:2,“name”:“李四”,“age”:24,“description”:“李四是个测试工程师”,“createtm”:“1980-2-15 19:01:32”}
匹配搜索
匹配搜索
精确搜索查询语句:{
“query” : {
“term” : {
“age” : 24
}
}
}
精确搜索返回结果:{“id”:2,“name”:“李四”,“age”:24,“description”:“李四是个测试工程师”,“createtm”:“1980-2-15 19:01:32”}
时间区间搜索
区间搜索语句:{
“query” : {
“range” : {
“createtm” : {
“from” : “2016-8-21 06:11:32”,
“to” : “2018-8-21 06:11:32”,
“include_lower” : true,
“include_upper” : true
}
}
}
}
区间搜索返回结果:{“id”:1,“name”:“张三”,“age”:20,“description”:“张三是个Java开发工程师”,“createtm”:“2018-4-25 11:07:42”}
新增完数据之后,我们可以上linux的 Kibana中进行相关的查询,查询结果如下:
注:Kibana 是属于ELK中一个开源软件。Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
上述代码中测试返回的结果符合我们的预期。其中关于JestClient只是用到了很少的一部分,更多的使用可以查看JestClient的官方文档。
Windows安装ElasticSearch
1,文件准备 下载地址: https://www.elastic.co/downloads 选择ElasticSearch相关版本, 然后选择后缀名为ZIP文件进行下载,下载之后进行解压。
2,启动Elasticsearch 进入bin目录下,运行 elasticsearch.bat 然后在浏览上输入: localhost:9200 成功显示一下界面表示成功!
3,安装ES插件 web管理界面head 安装 进入bin目录下,打开cmd,进入dos界面 输入:plugin install mobz/elasticsearch-head
进行下载 成功下载之后,在浏览器输入:http://localhost:9200/_plugin/head/ 若显示一下界面,则安装成功!
4,注册服务 进入bin目录下,打开cmd,进入dos界面 依次输入: service.bat install service.bat start 成功之后,再输入 services.msc 跳转到Service服务界面,可以直接查看es的运行状态!
Kafka实战笔记
关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图
- Kafka入门
- 为什么选择Kafka
- Karka的安装、管理和配置
- Kafka的集群
- 第一个Kafka程序
afka的生产者
- Kafka的消费者
- 深入理解Kafka
- 可靠的数据传递
- Spring和Kalka的整合
- Sprinboot和Kafka的整合
- Kafka实战之削峰填谷
- 数据管道和流式处理(了解即可)
- Kafka实战之削峰填谷
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
Kafka的消费者
- 深入理解Kafka
- 可靠的数据传递
[外链图片转存中…(img-KUrBvPzQ-1714757749229)]
[外链图片转存中…(img-9c4lQD3l-1714757749229)]
- Spring和Kalka的整合
- Sprinboot和Kafka的整合
- Kafka实战之削峰填谷
- 数据管道和流式处理(了解即可)
[外链图片转存中…(img-iSC8Qcf4-1714757749230)]
- Kafka实战之削峰填谷
[外链图片转存中…(img-ccKUUnkB-1714757749230)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!