@@ -65,6 +65,7 @@ | |||||
<module>spring-boot-demo-ldap</module> | <module>spring-boot-demo-ldap</module> | ||||
<module>spring-boot-demo-dynamic-datasource</module> | <module>spring-boot-demo-dynamic-datasource</module> | ||||
<module>spring-boot-demo-ratelimit-guava</module> | <module>spring-boot-demo-ratelimit-guava</module> | ||||
<module>spring-boot-demo-elasticsearch-rest-high-level-client</module> | |||||
</modules> | </modules> | ||||
<packaging>pom</packaging> | <packaging>pom</packaging> | ||||
@@ -0,0 +1,85 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" | |||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<parent> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<groupId>com.xkcoding</groupId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<artifactId>spring-boot-demo-elasticsearch-rest-high-level-client</artifactId> | |||||
<name>spring-boot-demo-elasticsearch-rest-high-level-client</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter</artifactId> | |||||
</dependency> | |||||
<!-- test --> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<!-- validator --> | |||||
<dependency> | |||||
<groupId>org.hibernate.validator</groupId> | |||||
<artifactId>hibernate-validator</artifactId> | |||||
<scope>compile</scope> | |||||
</dependency> | |||||
<!-- | |||||
You can easily generate your own configuration metadata file from items annotated with | |||||
@ConfigurationProperties by using the spring-boot-configuration-processor jar. | |||||
--> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-configuration-processor</artifactId> | |||||
</dependency> | |||||
<!-- elasticsearch --> | |||||
<dependency> | |||||
<groupId>org.elasticsearch</groupId> | |||||
<artifactId>elasticsearch</artifactId> | |||||
<version>7.3.0</version> | |||||
</dependency> | |||||
<!-- elasticsearch-rest-high-level-client --> | |||||
<dependency> | |||||
<groupId>org.elasticsearch.client</groupId> | |||||
<artifactId>elasticsearch-rest-high-level-client</artifactId> | |||||
<version>7.3.0</version> | |||||
<exclusions> | |||||
<!-- <exclusion>--> | |||||
<!-- <groupId>org.elasticsearch.client</groupId>--> | |||||
<!-- <artifactId>elasticsearch-rest-client</artifactId>--> | |||||
<!-- </exclusion>--> | |||||
<exclusion> | |||||
<groupId>org.elasticsearch</groupId> | |||||
<artifactId>elasticsearch</artifactId> | |||||
</exclusion> | |||||
</exclusions> | |||||
</dependency> | |||||
<!-- lombok --> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
</project> |
@@ -0,0 +1,20 @@ | |||||
package com.xlcoding.elasticsearch; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
/** | |||||
* ElasticsearchApplication | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 23:10 | |||||
*/ | |||||
@SpringBootApplication | |||||
public class ElasticsearchApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(ElasticsearchApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,85 @@ | |||||
package com.xlcoding.elasticsearch.autoconfigure; | |||||
import org.apache.http.HttpHost; | |||||
import org.elasticsearch.client.RestClient; | |||||
import org.elasticsearch.client.RestClientBuilder; | |||||
import org.elasticsearch.client.RestHighLevelClient; | |||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.util.Assert; | |||||
import org.springframework.util.StringUtils; | |||||
import javax.annotation.Resource; | |||||
import javax.validation.constraints.NotNull; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | |||||
* ElasticsearchAutoConfiguration | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 22:59 | |||||
*/ | |||||
@EnableConfigurationProperties(ElasticsearchProperties.class) | |||||
public class ElasticsearchAutoConfiguration { | |||||
@SuppressWarnings("NullableProblems") | |||||
@NotNull | |||||
@Resource | |||||
private ElasticsearchProperties elasticsearchProperties; | |||||
private List<HttpHost> httpHosts = new ArrayList<>(); | |||||
@Bean | |||||
@ConditionalOnMissingBean | |||||
public RestHighLevelClient restHighLevelClient() { | |||||
List<String> clusterNodes = elasticsearchProperties.getClusterNodes(); | |||||
clusterNodes.forEach(node -> { | |||||
try { | |||||
String[] parts = StringUtils.split(node, ":"); | |||||
Assert.notNull(parts, "Must defined"); | |||||
Assert.state(parts.length == 2, "Must be defined as 'host:port'"); | |||||
httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema())); | |||||
} catch (Exception e) { | |||||
throw new IllegalStateException( | |||||
"Invalid ES nodes " + "property '" + node + "'", e); | |||||
} | |||||
}); | |||||
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0])); | |||||
return getRestHighLevelClient(builder, elasticsearchProperties); | |||||
} | |||||
/** | |||||
* get restHistLevelClient | |||||
* | |||||
* @author fxbin | |||||
* @param builder RestClientBuilder | |||||
* @param elasticsearchProperties elasticsearch default properties | |||||
* @return {@link org.elasticsearch.client.RestHighLevelClient} | |||||
*/ | |||||
private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) { | |||||
// Callback used the default {@link RequestConfig} being set to the {@link CloseableHttpClient} | |||||
builder.setRequestConfigCallback(requestConfigBuilder -> { | |||||
requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout()); | |||||
requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout()); | |||||
requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout()); | |||||
return requestConfigBuilder; | |||||
}); | |||||
// Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance. | |||||
builder.setHttpClientConfigCallback(httpClientBuilder -> { | |||||
httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal()); | |||||
httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute()); | |||||
return httpClientBuilder; | |||||
}); | |||||
return new RestHighLevelClient(builder); | |||||
} | |||||
} |
@@ -0,0 +1,91 @@ | |||||
package com.xlcoding.elasticsearch.autoconfigure; | |||||
import lombok.AllArgsConstructor; | |||||
import lombok.Builder; | |||||
import lombok.Data; | |||||
import lombok.NoArgsConstructor; | |||||
import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
import org.springframework.stereotype.Component; | |||||
import javax.validation.constraints.NotNull; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | |||||
* ElasticsearchProperties | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 22:58 | |||||
*/ | |||||
@Data | |||||
@Builder | |||||
@Component | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
@ConfigurationProperties(prefix = "demo.data.elasticsearch") | |||||
public class ElasticsearchProperties { | |||||
/** | |||||
* 请求协议 | |||||
*/ | |||||
private String schema = "http"; | |||||
/** | |||||
* 集群名称 | |||||
*/ | |||||
private String clusterName = "elasticsearch"; | |||||
/** | |||||
* 集群节点 | |||||
*/ | |||||
@NotNull(message = "集群节点不允许为空") | |||||
private List<String> clusterNodes = new ArrayList<>(); | |||||
/** | |||||
* 连接超时时间(毫秒) | |||||
*/ | |||||
private Integer connectTimeout = 1000; | |||||
/** | |||||
* socket 超时时间 | |||||
*/ | |||||
private Integer socketTimeout = 30000; | |||||
/** | |||||
* 连接请求超时时间 | |||||
*/ | |||||
private Integer connectionRequestTimeout = 500; | |||||
/** | |||||
* 每个路由的最大连接数量 | |||||
*/ | |||||
private Integer maxConnectPerRoute = 10; | |||||
/** | |||||
* 最大连接总数量 | |||||
*/ | |||||
private Integer maxConnectTotal = 30; | |||||
/** | |||||
* 索引配置信息 | |||||
*/ | |||||
private Index index; | |||||
@Data | |||||
@Builder | |||||
public static class Index { | |||||
/** | |||||
* 分片数量 | |||||
*/ | |||||
protected Integer numberOfShards = 3; | |||||
/** | |||||
* 副本数量 | |||||
*/ | |||||
protected Integer numberOfReplicas = 2; | |||||
} | |||||
} |
@@ -0,0 +1,11 @@ | |||||
package com.xlcoding.elasticsearch.contants; | |||||
/** | |||||
* DataTypeTransfer | |||||
* | |||||
* @author fxbin | |||||
* @version 1.0v | |||||
* @since 2019/9/16 18:00 | |||||
*/ | |||||
public enum DataTypeTransfer { | |||||
} |
@@ -0,0 +1,17 @@ | |||||
package com.xlcoding.elasticsearch.contants; | |||||
/** | |||||
* ElasticsearchConstant | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 23:03 | |||||
*/ | |||||
public interface ElasticsearchConstant { | |||||
/** | |||||
* 索引名称 | |||||
*/ | |||||
String INDEX_NAME = "person"; | |||||
} |
@@ -0,0 +1,47 @@ | |||||
package com.xlcoding.elasticsearch.exception; | |||||
import com.xlcoding.elasticsearch.model.ResultCode; | |||||
import lombok.Getter; | |||||
/** | |||||
* ElasticsearchException | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/8/26 1:53 | |||||
*/ | |||||
public class ElasticsearchException extends RuntimeException { | |||||
@Getter | |||||
private int errcode; | |||||
@Getter | |||||
private String errmsg; | |||||
public ElasticsearchException(ResultCode resultCode) { | |||||
this(resultCode.getCode(), resultCode.getMsg()); | |||||
} | |||||
public ElasticsearchException(String message) { | |||||
super(message); | |||||
} | |||||
public ElasticsearchException(Integer errcode, String errmsg) { | |||||
super(errmsg); | |||||
this.errcode = errcode; | |||||
this.errmsg = errmsg; | |||||
} | |||||
public ElasticsearchException(String message, Throwable cause) { | |||||
super(message, cause); | |||||
} | |||||
public ElasticsearchException(Throwable cause) { | |||||
super(cause); | |||||
} | |||||
public ElasticsearchException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | |||||
super(message, cause, enableSuppression, writableStackTrace); | |||||
} | |||||
} |
@@ -0,0 +1,56 @@ | |||||
package com.xlcoding.elasticsearch.model; | |||||
import lombok.AllArgsConstructor; | |||||
import lombok.Builder; | |||||
import lombok.Data; | |||||
import lombok.NoArgsConstructor; | |||||
import java.io.Serializable; | |||||
import java.util.Date; | |||||
/** | |||||
* Person | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 23:04 | |||||
*/ | |||||
@Data | |||||
@Builder | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
public class Person implements Serializable { | |||||
private static final long serialVersionUID = 8510634155374943623L; | |||||
/** | |||||
* 主键 | |||||
*/ | |||||
private Long id; | |||||
/** | |||||
* 名字 | |||||
*/ | |||||
private String name; | |||||
/** | |||||
* 国家 | |||||
*/ | |||||
private String country; | |||||
/** | |||||
* 年龄 | |||||
*/ | |||||
private Integer age; | |||||
/** | |||||
* 生日 | |||||
*/ | |||||
private Date birthday; | |||||
/** | |||||
* 介绍 | |||||
*/ | |||||
private String remark; | |||||
} |
@@ -0,0 +1,84 @@ | |||||
package com.xlcoding.elasticsearch.model; | |||||
import lombok.Data; | |||||
import org.springframework.lang.Nullable; | |||||
import java.io.Serializable; | |||||
/** | |||||
* Result | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/8/26 1:44 | |||||
*/ | |||||
@Data | |||||
public class Result<T> implements Serializable { | |||||
private static final long serialVersionUID = 1696194043024336235L; | |||||
/** | |||||
* 错误码 | |||||
*/ | |||||
private int errcode; | |||||
/** | |||||
* 错误信息 | |||||
*/ | |||||
private String errmsg; | |||||
/** | |||||
* 响应数据 | |||||
*/ | |||||
private T data; | |||||
public Result() { | |||||
} | |||||
private Result(ResultCode resultCode) { | |||||
this(resultCode.code, resultCode.msg); | |||||
} | |||||
private Result(ResultCode resultCode, T data) { | |||||
this(resultCode.code, resultCode.msg, data); | |||||
} | |||||
private Result(int errcode, String errmsg) { | |||||
this(errcode, errmsg, null); | |||||
} | |||||
private Result(int errcode, String errmsg, T data) { | |||||
this.errcode = errcode; | |||||
this.errmsg = errmsg; | |||||
this.data = data; | |||||
} | |||||
/** | |||||
* 返回成功 | |||||
* | |||||
* @param <T> 泛型标记 | |||||
* @return 响应信息 {@code Result} | |||||
*/ | |||||
public static <T> Result<T> success() { | |||||
return new Result<>(ResultCode.SUCCESS); | |||||
} | |||||
/** | |||||
* 返回成功-携带数据 | |||||
* | |||||
* @param data 响应数据 | |||||
* @param <T> 泛型标记 | |||||
* @return 响应信息 {@code Result} | |||||
*/ | |||||
public static <T> Result<T> success(@Nullable T data) { | |||||
return new Result<>(ResultCode.SUCCESS, data); | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
package com.xlcoding.elasticsearch.model; | |||||
import lombok.AllArgsConstructor; | |||||
import lombok.Getter; | |||||
/** | |||||
* ResultCode | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/8/26 1:47 | |||||
*/ | |||||
@Getter | |||||
@AllArgsConstructor | |||||
public enum ResultCode { | |||||
/** | |||||
* 接口调用成功 | |||||
*/ | |||||
SUCCESS(0, "Request Successful"), | |||||
/** | |||||
* 服务器暂不可用,建议稍候重试。建议重试次数不超过3次。 | |||||
*/ | |||||
FAILURE(-1, "System Busy"); | |||||
final int code; | |||||
final String msg; | |||||
} |
@@ -0,0 +1,88 @@ | |||||
package com.xlcoding.elasticsearch.service; | |||||
import com.xlcoding.elasticsearch.autoconfigure.ElasticsearchProperties; | |||||
import com.xlcoding.elasticsearch.exception.ElasticsearchException; | |||||
import com.xlcoding.elasticsearch.model.Person; | |||||
import com.xlcoding.elasticsearch.util.BeanUtils; | |||||
import com.xlcoding.elasticsearch.util.MapUtils; | |||||
import org.elasticsearch.action.index.IndexRequest; | |||||
import org.elasticsearch.client.indices.CreateIndexRequest; | |||||
import org.elasticsearch.common.settings.Settings; | |||||
import org.elasticsearch.common.xcontent.XContentBuilder; | |||||
import org.elasticsearch.common.xcontent.XContentFactory; | |||||
import javax.annotation.Resource; | |||||
import java.io.IOException; | |||||
import java.util.Map; | |||||
/** | |||||
* BaseElasticsearchService | |||||
* | |||||
* @author fxbin | |||||
* @version 1.0v | |||||
* @since 2019/9/16 15:44 | |||||
*/ | |||||
public abstract class BaseElasticsearchService { | |||||
@Resource | |||||
private ElasticsearchProperties elasticsearchProperties; | |||||
/** | |||||
* create elasticsearch index | |||||
* | |||||
* @author fxbin | |||||
* @param index elasticsearch index | |||||
*/ | |||||
public void createIndex(String index, Person person) { | |||||
try { | |||||
CreateIndexRequest request = new CreateIndexRequest(index); | |||||
// Settings for this index | |||||
request.settings(Settings.builder() | |||||
.put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()) | |||||
.put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas())); | |||||
XContentBuilder builder = XContentFactory.jsonBuilder(); | |||||
builder.startObject(); | |||||
{ | |||||
builder.startObject("properties"); | |||||
{ | |||||
builder.startObject("message"); | |||||
{ | |||||
Map<String, Object> map = | |||||
BeanUtils.toMap(person); | |||||
map.keySet().forEach(key -> { | |||||
try { | |||||
builder.field(key, "text"); | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
}); | |||||
} | |||||
builder.endObject(); | |||||
} | |||||
builder.endObject(); | |||||
} | |||||
builder.endObject(); | |||||
request.mapping(builder); | |||||
} catch (IOException e) { | |||||
throw new ElasticsearchException("创建Elasticsearch索引 {" + index + "} 失败"); | |||||
} | |||||
} | |||||
/** | |||||
* build IndexRequest | |||||
* | |||||
* @author fxbin | |||||
* @param index elasticsearch index name | |||||
* @return {@link org.elasticsearch.action.index.IndexRequest} | |||||
*/ | |||||
public IndexRequest buildIndexRequest(String index) { | |||||
return new IndexRequest(index); | |||||
} | |||||
} |
@@ -0,0 +1,11 @@ | |||||
package com.xlcoding.elasticsearch.service; | |||||
/** | |||||
* PersonService | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 23:07 | |||||
*/ | |||||
public interface PersonService { | |||||
} |
@@ -0,0 +1,14 @@ | |||||
package com.xlcoding.elasticsearch.service.impl; | |||||
import com.xlcoding.elasticsearch.service.BaseElasticsearchService; | |||||
import com.xlcoding.elasticsearch.service.PersonService; | |||||
/** | |||||
* PersonServiceImpl | |||||
* | |||||
* @author fxbin | |||||
* @version v1.0 | |||||
* @since 2019/9/15 23:08 | |||||
*/ | |||||
public class PersonServiceImpl extends BaseElasticsearchService implements PersonService { | |||||
} |
@@ -0,0 +1,64 @@ | |||||
package com.xlcoding.elasticsearch.util; | |||||
import com.xlcoding.elasticsearch.model.Person; | |||||
import java.beans.BeanInfo; | |||||
import java.beans.IntrospectionException; | |||||
import java.beans.Introspector; | |||||
import java.beans.PropertyDescriptor; | |||||
import java.lang.reflect.InvocationTargetException; | |||||
import java.lang.reflect.Method; | |||||
import java.util.Map; | |||||
/** | |||||
* BeanUtils | |||||
* | |||||
* @author fxbin | |||||
* @version 1.0v | |||||
* @since 2019/9/16 16:26 | |||||
*/ | |||||
public class BeanUtils { | |||||
/** | |||||
* Java Bean to Map | |||||
* | |||||
* @author fxbin | |||||
* @param object Object | |||||
* @return Map | |||||
*/ | |||||
public static Map<String,Object> toFieldNameAndFieldTypeMap(Object object){ | |||||
Map<String, Object> map = MapUtils.newHashMap(); | |||||
try { | |||||
// 获取javaBean的BeanInfo对象 | |||||
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass(),Object.class); | |||||
// 获取属性描述器 | |||||
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); | |||||
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { | |||||
// 获取属性名 | |||||
String key = propertyDescriptor.getName(); | |||||
Class<?> value = propertyDescriptor.getPropertyType(); | |||||
// 获取该属性的值 | |||||
// Method readMethod = propertyDescriptor.getReadMethod(); | |||||
// 通过反射来调用javaBean定义的getName()方法 | |||||
// Object value = readMethod.invoke(object); | |||||
map.put(key, value); | |||||
} | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
return map; | |||||
} | |||||
public static void main(String[] args) { | |||||
Person person = new Person(); | |||||
Map<String, Object> stringObjectMap = toFieldNameAndFieldTypeMap(person); | |||||
System.out.println(stringObjectMap); | |||||
} | |||||
} |
@@ -0,0 +1,70 @@ | |||||
package com.xlcoding.elasticsearch.util; | |||||
import java.util.HashMap; | |||||
import java.util.LinkedHashMap; | |||||
/** | |||||
* MapUtils | |||||
* | |||||
* @author fxbin | |||||
* @version 1.0v | |||||
* @since 2019/9/16 16:26 | |||||
*/ | |||||
public class MapUtils { | |||||
/** 默认初始大小 */ | |||||
private static final int DEFAULT_INITIAL_CAPACITY = 16; | |||||
/** 默认增长因子,当Map的size达到 容量*增长因子时,开始扩充Map */ | |||||
private static final float DEFAULT_LOAD_FACTOR = 0.75f; | |||||
/** | |||||
* 新建一个HashMap | |||||
* | |||||
* @param <K> Key类型 | |||||
* @param <V> Value类型 | |||||
* @return HashMap对象 | |||||
*/ | |||||
public static <K, V> HashMap<K, V> newHashMap() { | |||||
return new HashMap<>(DEFAULT_INITIAL_CAPACITY); | |||||
} | |||||
/** | |||||
* 新建一个HashMap | |||||
* | |||||
* @param <K> Key类型 | |||||
* @param <V> Value类型 | |||||
* @param size 初始大小,由于默认负载因子0.75,传入的size会实际初始大小为size / 0.75 | |||||
* @param isOrder Map的Key是否有序,有序返回 {@link LinkedHashMap},否则返回 {@link HashMap} | |||||
* @return HashMap对象 | |||||
* @since 3.0.4 | |||||
*/ | |||||
public static <K, V> HashMap<K, V> newHashMap(int size, boolean isOrder) { | |||||
int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR); | |||||
return isOrder ? new LinkedHashMap<K, V>(initialCapacity) : new HashMap<K, V>(initialCapacity); | |||||
} | |||||
/** | |||||
* 新建一个HashMap | |||||
* | |||||
* @param <K> Key类型 | |||||
* @param <V> Value类型 | |||||
* @param size 初始大小,由于默认负载因子0.75,传入的size会实际初始大小为size / 0.75 | |||||
* @return HashMap对象 | |||||
*/ | |||||
public static <K, V> HashMap<K, V> newHashMap(int size) { | |||||
return newHashMap(size, false); | |||||
} | |||||
/** | |||||
* 新建一个HashMap | |||||
* | |||||
* @param <K> Key类型 | |||||
* @param <V> Value类型 | |||||
* @param isOrder Map的Key是否有序,有序返回 {@link LinkedHashMap},否则返回 {@link HashMap} | |||||
* @return HashMap对象 | |||||
*/ | |||||
public static <K, V> HashMap<K, V> newHashMap(boolean isOrder) { | |||||
return newHashMap(DEFAULT_INITIAL_CAPACITY, isOrder); | |||||
} | |||||
} |
@@ -0,0 +1,5 @@ | |||||
demo: | |||||
data: | |||||
elasticsearch: | |||||
cluster-name: elasticsearch | |||||
cluster-nodes: 20.20.0.27:9201 |
@@ -0,0 +1,17 @@ | |||||
package com.xdcoding.elasticsearch; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class ElasticsearchApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |