Skip to content

Commit 47d7d15

Browse files
committed
feat: 示例更新
1 parent bd6b17c commit 47d7d15

File tree

7 files changed

+175
-79
lines changed

7 files changed

+175
-79
lines changed

codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/ElasticsearchTemplate.java

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public <T extends BaseEsEntity> T save(String index, String type, T entity) thro
280280
|| response.getResult() == DocWriteResponse.Result.UPDATED) {
281281
return entity;
282282
} else {
283-
log.warn("【ES】save 响应结果无效!result: {}", response.getResult());
283+
log.warn("【ES】save 失败,result: {}", response.getResult());
284284
return null;
285285
}
286286
}
@@ -292,45 +292,25 @@ public <T extends BaseEsEntity> boolean saveBatch(String index, String type, Col
292292
return true;
293293
}
294294

295-
BulkRequest bulkRequest = new BulkRequest();
296-
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
297-
for (T entity : list) {
298-
Map<String, Object> map = toMap(entity);
299-
if (MapUtil.isEmpty(map)) {
300-
continue;
301-
}
302-
IndexRequest request = new IndexRequest(index, type).source(map);
303-
if (entity.getDocId() != null) {
304-
request.id(entity.getDocId());
305-
}
306-
bulkRequest.add(request);
307-
}
308-
295+
BulkRequest bulkRequest = toBulkIndexRequest(index, type, list);
309296
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
310-
return response != null && !response.hasFailures();
297+
if (response == null) {
298+
log.warn("【ES】saveBatch 失败,result 为空!list: {}", JsonUtil.toString(list));
299+
return false;
300+
}
301+
if (response.hasFailures()) {
302+
log.warn("【ES】saveBatch 失败,result: {}!", response.buildFailureMessage());
303+
return false;
304+
}
305+
return true;
311306
}
312307

313308
public <T extends BaseEsEntity> void asyncSaveBatch(String index, String type, Collection<T> list,
314309
ActionListener<BulkResponse> listener) {
315-
316310
if (CollectionUtil.isEmpty(list)) {
317311
return;
318312
}
319-
320-
BulkRequest bulkRequest = new BulkRequest();
321-
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
322-
for (T entity : list) {
323-
Map<String, Object> map = toMap(entity);
324-
if (MapUtil.isEmpty(map)) {
325-
continue;
326-
}
327-
IndexRequest request = new IndexRequest(index, type).source(map);
328-
if (entity.getDocId() != null) {
329-
request.id(entity.getDocId());
330-
}
331-
bulkRequest.add(request);
332-
}
333-
313+
BulkRequest bulkRequest = toBulkIndexRequest(index, type, list);
334314
client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, listener);
335315
}
336316

@@ -362,7 +342,7 @@ public <T extends BaseEsEntity> T updateById(String index, String type, T entity
362342
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
363343
return entity;
364344
} else {
365-
log.warn("【ES】updateById 响应结果无效result: {}", response.getResult());
345+
log.warn("【ES】updateById 响应结果无效result: {}", response.getResult());
366346
return null;
367347
}
368348
}
@@ -374,23 +354,49 @@ public <T extends BaseEsEntity> boolean updateBatchIds(String index, String type
374354
return true;
375355
}
376356

377-
BulkRequest bulkRequest = toUpdateBulkRequest(index, type, list);
357+
BulkRequest bulkRequest = toBulkUpdateRequest(index, type, list);
378358
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
379-
return response != null && !response.hasFailures();
359+
if (response == null) {
360+
log.warn("【ES】updateBatchIds 失败,result 为空!list: {}", JsonUtil.toString(list));
361+
return false;
362+
}
363+
if (response.hasFailures()) {
364+
log.warn("【ES】updateBatchIds 失败,result: {}!", response.buildFailureMessage());
365+
return false;
366+
}
367+
return true;
380368
}
381369

382370
public <T extends BaseEsEntity> void asyncUpdateBatchIds(String index, String type, Collection<T> list,
383371
ActionListener<BulkResponse> listener) {
384-
385372
if (CollectionUtil.isEmpty(list)) {
386373
return;
387374
}
388-
389-
BulkRequest bulkRequest = toUpdateBulkRequest(index, type, list);
375+
BulkRequest bulkRequest = toBulkUpdateRequest(index, type, list);
390376
client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, listener);
391377
}
392378

393-
private <T extends BaseEsEntity> BulkRequest toUpdateBulkRequest(String index, String type, Collection<T> list) {
379+
private <T extends BaseEsEntity> BulkRequest toBulkIndexRequest(String index, String type, Collection<T> list) {
380+
BulkRequest bulkRequest = new BulkRequest();
381+
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
382+
for (T entity : list) {
383+
if (entity == null) {
384+
continue;
385+
}
386+
Map<String, Object> map = toMap(entity);
387+
if (MapUtil.isEmpty(map)) {
388+
continue;
389+
}
390+
IndexRequest request = new IndexRequest(index, type).source(map);
391+
if (entity.getDocId() != null) {
392+
request.id(entity.getDocId());
393+
}
394+
bulkRequest.add(request);
395+
}
396+
return bulkRequest;
397+
}
398+
399+
private <T extends BaseEsEntity> BulkRequest toBulkUpdateRequest(String index, String type, Collection<T> list) {
394400
BulkRequest bulkRequest = new BulkRequest();
395401
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
396402
for (T entity : list) {
@@ -426,11 +432,14 @@ public boolean deleteBatchIds(String index, String type, Collection<String> ids)
426432

427433
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
428434
if (response == null) {
429-
log.warn("【ES】batchDeleteById 响应结果为空!");
435+
log.warn("【ES】deleteBatchIds 失败,result 为空!ids: {}", JsonUtil.toString(ids));
430436
return false;
431437
}
432-
433-
return !response.hasFailures();
438+
if (response.hasFailures()) {
439+
log.warn("【ES】deleteBatchIds 失败,result: {}!", response.buildFailureMessage());
440+
return false;
441+
}
442+
return true;
434443
}
435444

436445
public void asyncDeleteBatchIds(String index, String type, Collection<String> ids,
@@ -568,7 +577,8 @@ public <T> PageData<T> pojoPage(String index, String type, int from, int size, Q
568577
/**
569578
* search after 分页
570579
*/
571-
public <T extends BaseEsEntity> ScrollData<T> pojoPageByScrollId(String index, String type, String scrollId, int size,
580+
public <T extends BaseEsEntity> ScrollData<T> pojoPageByScrollId(String index, String type, String scrollId,
581+
int size,
572582
QueryBuilder queryBuilder, Class<T> clazz) throws IOException {
573583

574584
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

codes/javadb/elasticsearch/elasticsearch6/src/test/java/io/github/dunwu/javadb/elasticsearch/BaseElasticsearchTemplateTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.github.dunwu.javadb.elasticsearch.util.JsonUtil;
99
import lombok.extern.slf4j.Slf4j;
1010
import org.assertj.core.api.Assertions;
11+
import org.elasticsearch.ElasticsearchException;
1112
import org.elasticsearch.action.ActionListener;
1213
import org.elasticsearch.action.bulk.BulkResponse;
1314
import org.elasticsearch.action.get.GetResponse;
@@ -59,12 +60,18 @@ public abstract class BaseElasticsearchTemplateTest<T extends BaseEsEntity> {
5960
protected abstract List<T> getMockList(int num);
6061

6162
protected void deleteIndex() throws IOException {
62-
boolean exists = TEMPLATE.isIndexExists(getIndex());
63-
if (!exists) {
64-
return;
63+
try {
64+
Set<String> set = TEMPLATE.getIndexSet(getAlias());
65+
if (CollectionUtil.isNotEmpty(set)) {
66+
for (String index : set) {
67+
log.info("删除 alias: {}, index: {}", getAlias(), index);
68+
TEMPLATE.deleteIndex(index);
69+
}
70+
}
71+
} catch (IOException | ElasticsearchException e) {
72+
log.error("删除索引失败!", e);
6573
}
66-
TEMPLATE.deleteIndex(getIndex());
67-
exists = TEMPLATE.isIndexExists(getIndex());
74+
boolean exists = TEMPLATE.isIndexExists(getIndex());
6875
Assertions.assertThat(exists).isFalse();
6976
}
7077

@@ -98,7 +105,7 @@ protected void saveBatch() throws IOException {
98105
int total = 5000;
99106
List<List<T>> listGroup = CollectionUtil.split(getMockList(total), 1000);
100107
for (List<T> list : listGroup) {
101-
TEMPLATE.saveBatch(getIndex(), getType(), list);
108+
Assertions.assertThat(TEMPLATE.saveBatch(getIndex(), getType(), list)).isTrue();
102109
}
103110
long count = TEMPLATE.count(getIndex(), getType(), new SearchSourceBuilder());
104111
log.info("批量更新记录数: {}", count);

codes/javadb/redis/pom.xml

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.6.3</version>
9+
<version>2.7.18</version>
1010
</parent>
1111

1212
<groupId>io.github.dunwu</groupId>
@@ -36,7 +36,7 @@
3636
<dependency>
3737
<groupId>cn.hutool</groupId>
3838
<artifactId>hutool-all</artifactId>
39-
<version>5.5.9</version>
39+
<version>5.8.27</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>org.projectlombok</groupId>
@@ -51,27 +51,11 @@
5151
<dependency>
5252
<groupId>org.redisson</groupId>
5353
<artifactId>redisson</artifactId>
54-
<version>3.16.8</version>
54+
<version>3.29.0</version>
5555
</dependency>
5656
<!-- database end -->
57-
58-
<dependency>
59-
<groupId>junit</groupId>
60-
<artifactId>junit</artifactId>
61-
<scope>test</scope>
62-
</dependency>
6357
</dependencies>
6458

65-
<dependencyManagement>
66-
<dependencies>
67-
<dependency>
68-
<groupId>org.redisson</groupId>
69-
<artifactId>redisson</artifactId>
70-
<version>${redisson.version}</version>
71-
</dependency>
72-
</dependencies>
73-
</dependencyManagement>
74-
7559
<build>
7660
<plugins>
7761
<plugin>

codes/javadb/redis/src/main/java/io/github/dunwu/javadb/redis/springboot/RedisAutoConfiguration.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package io.github.dunwu.javadb.redis.springboot;
22

3+
import cn.hutool.core.util.StrUtil;
34
import com.fasterxml.jackson.annotation.JsonAutoDetect;
45
import com.fasterxml.jackson.annotation.PropertyAccessor;
56
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import org.redisson.Redisson;
8+
import org.redisson.api.RedissonClient;
9+
import org.redisson.config.Config;
610
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Value;
712
import org.springframework.context.annotation.Bean;
813
import org.springframework.context.annotation.Configuration;
914
import org.springframework.context.annotation.Primary;
1015
import org.springframework.data.redis.connection.RedisConnectionFactory;
11-
import org.springframework.data.redis.core.*;
16+
import org.springframework.data.redis.core.HashOperations;
17+
import org.springframework.data.redis.core.ListOperations;
18+
import org.springframework.data.redis.core.RedisTemplate;
19+
import org.springframework.data.redis.core.SetOperations;
20+
import org.springframework.data.redis.core.ValueOperations;
21+
import org.springframework.data.redis.core.ZSetOperations;
1222
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
1323
import org.springframework.data.redis.serializer.StringRedisSerializer;
1424

@@ -22,6 +32,19 @@ public class RedisAutoConfiguration {
2232
@Autowired
2333
private ObjectMapper objectMapper;
2434

35+
@Value("${spring.redis.host:localhost}")
36+
private String host;
37+
38+
@Value("${spring.redis.port:6379}")
39+
private String port;
40+
41+
@Bean
42+
public RedissonClient redissonClient() {
43+
Config config = new Config();
44+
config.useSingleServer().setAddress(StrUtil.format("redis://{}:{}", host, port));
45+
return Redisson.create(config);
46+
}
47+
2548
@Bean
2649
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
2750
return redisTemplate.opsForHash();
@@ -44,7 +67,6 @@ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factor
4467
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
4568
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
4669
serializer.setObjectMapper(objectMapper);
47-
4870
RedisTemplate<String, Object> template = new RedisTemplate<>();
4971
// 配置连接工厂
5072
template.setConnectionFactory(factory);

0 commit comments

Comments
 (0)