From adcc3040f66f5ab8586b2de8435290348f10546c Mon Sep 17 00:00:00 2001 From: fxbin Date: Mon, 16 Sep 2019 19:30:13 +0800 Subject: [PATCH] init --- pom.xml | 1 + .../pom.xml | 85 +++++++++++++++++ .../ElasticsearchApplication.java | 20 ++++ .../ElasticsearchAutoConfiguration.java | 85 +++++++++++++++++ .../ElasticsearchProperties.java | 91 +++++++++++++++++++ .../contants/DataTypeTransfer.java | 11 +++ .../contants/ElasticsearchConstant.java | 17 ++++ .../exception/ElasticsearchException.java | 47 ++++++++++ .../xlcoding/elasticsearch/model/Person.java | 56 ++++++++++++ .../xlcoding/elasticsearch/model/Result.java | 84 +++++++++++++++++ .../elasticsearch/model/ResultCode.java | 31 +++++++ .../service/BaseElasticsearchService.java | 88 ++++++++++++++++++ .../elasticsearch/service/PersonService.java | 11 +++ .../service/impl/PersonServiceImpl.java | 14 +++ .../elasticsearch/util/BeanUtils.java | 64 +++++++++++++ .../xlcoding/elasticsearch/util/MapUtils.java | 70 ++++++++++++++ .../src/main/resources/application.yml | 5 + .../ElasticsearchApplicationTests.java | 17 ++++ 18 files changed, 797 insertions(+) create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java diff --git a/pom.xml b/pom.xml index 0ced8b35a..412062757 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ spring-boot-demo-ldap spring-boot-demo-dynamic-datasource spring-boot-demo-ratelimit-guava + spring-boot-demo-elasticsearch-rest-high-level-client pom diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml b/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml new file mode 100644 index 000000000..8b190697f --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + spring-boot-demo + com.xkcoding + 1.0.0-SNAPSHOT + + + spring-boot-demo-elasticsearch-rest-high-level-client + spring-boot-demo-elasticsearch-rest-high-level-client + Demo project for Spring Boot + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.hibernate.validator + hibernate-validator + compile + + + + + org.springframework.boot + spring-boot-configuration-processor + + + + + org.elasticsearch + elasticsearch + 7.3.0 + + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.3.0 + + + + + + + org.elasticsearch + elasticsearch + + + + + + + org.projectlombok + lombok + true + + + + + diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java new file mode 100644 index 000000000..11ba5b3bc --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java @@ -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); + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java new file mode 100644 index 000000000..495252438 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java @@ -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 httpHosts = new ArrayList<>(); + + @Bean + @ConditionalOnMissingBean + public RestHighLevelClient restHighLevelClient() { + + List 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); + } + + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java new file mode 100644 index 000000000..e1ba233c2 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java @@ -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 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; + + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java new file mode 100644 index 000000000..24530321d --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java @@ -0,0 +1,11 @@ +package com.xlcoding.elasticsearch.contants; + +/** + * DataTypeTransfer + * + * @author fxbin + * @version 1.0v + * @since 2019/9/16 18:00 + */ +public enum DataTypeTransfer { +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java new file mode 100644 index 000000000..5d1b34920 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java @@ -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"; + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java new file mode 100644 index 000000000..97c4ce6ee --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java @@ -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); + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java new file mode 100644 index 000000000..dbb79bc24 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java @@ -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; + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java new file mode 100644 index 000000000..4737d77d3 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java @@ -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 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 泛型标记 + * @return 响应信息 {@code Result} + */ + public static Result success() { + return new Result<>(ResultCode.SUCCESS); + } + + + /** + * 返回成功-携带数据 + * + * @param data 响应数据 + * @param 泛型标记 + * @return 响应信息 {@code Result} + */ + public static Result success(@Nullable T data) { + return new Result<>(ResultCode.SUCCESS, data); + } + + + + + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java new file mode 100644 index 000000000..0d0301378 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java @@ -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; + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java new file mode 100644 index 000000000..a7b691b85 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java @@ -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 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); + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java new file mode 100644 index 000000000..258ed4eb0 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java @@ -0,0 +1,11 @@ +package com.xlcoding.elasticsearch.service; + +/** + * PersonService + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:07 + */ +public interface PersonService { +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java new file mode 100644 index 000000000..63de2ae85 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java @@ -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 { +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java new file mode 100644 index 000000000..fe3126e50 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java @@ -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 toFieldNameAndFieldTypeMap(Object object){ + Map 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 stringObjectMap = toFieldNameAndFieldTypeMap(person); + + System.out.println(stringObjectMap); + + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java new file mode 100644 index 000000000..b8b492bbd --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java @@ -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 Key类型 + * @param Value类型 + * @return HashMap对象 + */ + public static HashMap newHashMap() { + return new HashMap<>(DEFAULT_INITIAL_CAPACITY); + } + + /** + * 新建一个HashMap + * + * @param Key类型 + * @param 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 HashMap newHashMap(int size, boolean isOrder) { + int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR); + return isOrder ? new LinkedHashMap(initialCapacity) : new HashMap(initialCapacity); + } + + /** + * 新建一个HashMap + * + * @param Key类型 + * @param Value类型 + * @param size 初始大小,由于默认负载因子0.75,传入的size会实际初始大小为size / 0.75 + * @return HashMap对象 + */ + public static HashMap newHashMap(int size) { + return newHashMap(size, false); + } + + /** + * 新建一个HashMap + * + * @param Key类型 + * @param Value类型 + * @param isOrder Map的Key是否有序,有序返回 {@link LinkedHashMap},否则返回 {@link HashMap} + * @return HashMap对象 + */ + public static HashMap newHashMap(boolean isOrder) { + return newHashMap(DEFAULT_INITIAL_CAPACITY, isOrder); + } + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml new file mode 100644 index 000000000..da858a698 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml @@ -0,0 +1,5 @@ +demo: + data: + elasticsearch: + cluster-name: elasticsearch + cluster-nodes: 20.20.0.27:9201 diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java new file mode 100644 index 000000000..f9c426e1e --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java @@ -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() { + } + + +}