diff --git a/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml b/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml new file mode 100644 index 0000000..d7a8103 --- /dev/null +++ b/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml new file mode 100644 index 0000000..02897be --- /dev/null +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 73de094..71f1eb8 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,7 @@ java 连接 elasticsearch5.x 集群及Java API代码 ## package elasticsearch6.x -java 连接 elasticsearch6.x 集群及Java API代码 \ No newline at end of file +java 连接 elasticsearch6.x 集群及Java API代码 + +## package elasticsearchAction +elasticsearch 二次开发,包含增删改查底层操作 diff --git a/elasticsearch5.x/pom.xml b/elasticsearch5.x/pom.xml index eee0ba5..2c96b4b 100644 --- a/elasticsearch5.x/pom.xml +++ b/elasticsearch5.x/pom.xml @@ -6,7 +6,11 @@ com.elastic elasticsearch5.x + pom 1.0-SNAPSHOT + + ../elasticsearchAction + diff --git a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java index 10bd354..e6ff0fd 100644 --- a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java +++ b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java @@ -34,9 +34,9 @@ public class SpringDataClientBuild { private String CLUSTER_HOSTNAME_PORT; /** - * 在Spring中,bean可以被定义为两种模式:prototype(多例)和singleton(单例) + * 在Spring中,bean可以被定义为两种模式:prototype(原型)和singleton(单例) * singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。Spring bean 默认是单例模式. - * prototype(多例):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 + * prototype(原型):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 */ @Bean @Scope("prototype") @@ -78,4 +78,4 @@ public Client getClient() { return client; } -} +} \ No newline at end of file diff --git a/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java b/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java index e9ec54b..005dd99 100644 --- a/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java +++ b/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java @@ -46,7 +46,4 @@ public void run() { } } - - -} - +} \ No newline at end of file diff --git a/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java b/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java index 9f76212..4066ce9 100644 --- a/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java +++ b/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java @@ -8,7 +8,6 @@ import org.apache.http.message.BasicHeader; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; -import org.elasticsearch.client.RestHighLevelClient; import java.util.List; import java.util.stream.Collectors; @@ -23,15 +22,16 @@ public class ClientBuilders { /** * 构建一个简单的RestClientBuilder方便测试 + * * @return */ - public RestClientBuilder getSimpleClientBuilder(){ - String [] ipHosts = CLUSTER_HOSTNAME_PORT.split(","); + public RestClientBuilder getSimpleClientBuilder() { + String[] ipHosts = CLUSTER_HOSTNAME_PORT.split(","); List httpHostsList = Stream.of(ipHosts) .map(this::createHttpHost) .collect(Collectors.toList()); - HttpHost [] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]); + HttpHost[] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]); RestClientBuilder builder = RestClient.builder(httpHosts); return builder; @@ -43,12 +43,13 @@ private HttpHost createHttpHost(String ip) { /** * 初始化 clientBuilder的详细说明 + * * @return */ public static RestClientBuilder getClientBulider() { - String [] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); + String[] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); String host; int port; diff --git a/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java b/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java index 79d4c24..810e870 100644 --- a/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java +++ b/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java @@ -1,9 +1,6 @@ package com.elastic.utils; -import com.elastic.exception.ESIoException; -import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; - import java.io.IOException; import java.io.InputStream; import java.util.Properties; @@ -13,65 +10,66 @@ **/ public class ConfigUtils { - /** - * 配置文件 - * */ - private static String esConfigFileName = "elasticsearch.properties"; - - /** - * es集群名 - * */ - private static String esClusterName; - /** - * es集群ip地址 - * */ - private static String esClusterDiscoverHostName; - /** - * es集群是否加入嗅探功能 - * */ - private static String clientTransportSniff; - - private static Properties properties = new Properties(); - static{ - try { - ClassLoader classLoader = ConfigUtils.class.getClassLoader(); - InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName); - properties.load(resourceAsStream); - init(); - }catch (IOException e){ - e.printStackTrace(); - } - - } - - private static void init() { - - esClusterName = properties.getProperty("elastic.cluster.name"); - esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname"); - clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff"); - - if ("".equals(esClusterName)||"".equals(esClusterName)||"".equals(clientTransportSniff)){ - throw new RuntimeException("elasticsearch 集群参数为空异常"); - } - } - - public static String getEsClusterName() { - return esClusterName; - } - - public static String getEsClusterDiscoverHostName() { - return esClusterDiscoverHostName; - } - - public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) { - ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName; - } - - public static String getClientTransportSniff() { - return clientTransportSniff; - } - - public static void setClientTransportSniff(String clientTransportSniff) { - ConfigUtils.clientTransportSniff = clientTransportSniff; - } + /** + * 配置文件 + */ + private static String esConfigFileName = "elasticsearch.properties"; + + /** + * es集群名 + */ + private static String esClusterName; + /** + * es集群ip地址 + */ + private static String esClusterDiscoverHostName; + /** + * es集群是否加入嗅探功能 + */ + private static String clientTransportSniff; + + private static Properties properties = new Properties(); + + static { + try { + ClassLoader classLoader = ConfigUtils.class.getClassLoader(); + InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName); + properties.load(resourceAsStream); + init(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static void init() { + + esClusterName = properties.getProperty("elastic.cluster.name"); + esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname"); + clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff"); + + if ("".equals(esClusterName) || "".equals(esClusterName) || "".equals(clientTransportSniff)) { + throw new RuntimeException("elasticsearch 集群参数为空异常"); + } + } + + public static String getEsClusterName() { + return esClusterName; + } + + public static String getEsClusterDiscoverHostName() { + return esClusterDiscoverHostName; + } + + public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) { + ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName; + } + + public static String getClientTransportSniff() { + return clientTransportSniff; + } + + public static void setClientTransportSniff(String clientTransportSniff) { + ConfigUtils.clientTransportSniff = clientTransportSniff; + } } diff --git a/elasticsearchAction/.gitingore b/elasticsearchAction/.gitingore new file mode 100644 index 0000000..d195cbb --- /dev/null +++ b/elasticsearchAction/.gitingore @@ -0,0 +1,41 @@ +#Eclipse +.classpath +.project +.settings/ + +#Intel Idea +.idea +*.iml +*.iws + +#Maven +log +target +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionBackup +pom.xml.next +release.properties +dependcy-reduced-pom.xml +buildNumber.properties + +#java +*.class +*.war +*.ear + +# bak +*.bak +/bin/ + +# sbt +/target/ +/project/target/ +/project/project/target/ +/project/project/project/target/ +/build-sbt/ +local.sbt + + +# spring +*.springBeans diff --git a/elasticsearchAction/elasticsearchAction.iml b/elasticsearchAction/elasticsearchAction.iml new file mode 100644 index 0000000..fcc9d62 --- /dev/null +++ b/elasticsearchAction/elasticsearchAction.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/elasticsearchAction/pom.xml b/elasticsearchAction/pom.xml new file mode 100644 index 0000000..e68902f --- /dev/null +++ b/elasticsearchAction/pom.xml @@ -0,0 +1,36 @@ + + + + elasticsearch5.x + com.elastic + 1.0-SNAPSHOT + ../elasticsearch5.x/pom.xml + + 4.0.0 + + elasticsearchAction + + + + org.elasticsearch.client + transport + 5.6.15 + + + + org.springframework.data + spring-data-elasticsearch + 3.1.0.RELEASE + + + + com.alibaba + fastjson + 1.2.62 + + + + + \ No newline at end of file diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java new file mode 100644 index 0000000..32ce607 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java @@ -0,0 +1,103 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.elasticsearch.index.VersionType; +import org.springframework.data.annotation.Persistent; + +/** + * Identifies a domain object to be persisted to Elasticsearch. + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Mason Chan + * @author Ivan Greene + * @author Mark Paluch + * @author Peter-Josef Meisch + */ +@Persistent +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +public @interface Document { + + /** + * Name of the Elasticsearch index. + * + */ + String indexName(); + + /** + * Mapping type name.
+ * deprecated as Elasticsearch does not support this anymore + * (@see Elastisearch removal of types documentation) and will remove it in + * Elasticsearch 8. + * + * @deprecated since 4.0 + */ + @Deprecated + String type() default ""; + + /** + * Use server-side settings when creating the index. + */ + boolean useServerConfiguration() default false; + + /** + * Number of shards for the index {@link #indexName()}. Used for index creation.
+ * With version 4.0, the default value is changed from 5 to 1 to reflect the change in the default settings of + * Elasticsearch which changed to 1 as well in Elasticsearch 7.0. + */ + short shards() default 1; + + /** + * Number of replicas for the index {@link #indexName()}. Used for index creation. + */ + short replicas() default 1; + + /** + * Refresh interval for the index {@link #indexName()}. Used for index creation. + */ + String refreshInterval() default "1s"; + + /** + * Index storage type for the index {@link #indexName()}. Used for index creation. + */ + String indexStoreType() default "fs"; + + /** + * Configuration whether to create an index on repository bootstrapping. + */ + boolean createIndex() default true; + + /** + * Configuration of version management. + */ + VersionType versionType() default VersionType.EXTERNAL; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java new file mode 100644 index 0000000..9a3fe68 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java @@ -0,0 +1,78 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import org.springframework.core.annotation.AliasFor; +import org.springframework.data.elasticsearch.annotations.DateFormat; +import org.springframework.data.elasticsearch.annotations.FieldType; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +@Inherited +public @interface Field { + + @AliasFor("name") + String value() default ""; + + @AliasFor("value") + String name() default ""; + + FieldType type() default FieldType.Auto; + + boolean index() default true; + + DateFormat format() default DateFormat.none; + + String pattern() default ""; + + boolean store() default false; + + boolean fielddata() default false; + + String searchAnalyzer() default ""; + + String analyzer() default ""; + + String normalizer() default ""; + + String[] ignoreFields() default {}; + + boolean includeInParent() default false; + + String[] copyTo() default {}; + + int ignoreAbove() default -1; + + boolean coerce() default true; + + boolean docValues() default true; + + boolean ignoreMalformed() default false; + + boolean indexPhrases() default false; + + + boolean norms() default true; + + + String nullValue() default ""; + + + double scalingFactor() default 1; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java b/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java new file mode 100644 index 0000000..2f60bc3 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Zeng Zetang + * @author Peter-Josef Meisch + */ +public enum FieldType { + Auto, + Text, + Keyword, + Long, + Integer, + Short, + Byte, + Double, + Float, + Half_Float, + Scaled_Float, + Date, + Date_Nanos, + Boolean, + Binary, + Integer_Range, + Float_Range, + Long_Range, + Double_Range, + Date_Range, + Ip_Range, + Object, + Nested, + Ip, + TokenCount, + Percolator, + Flattened +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java new file mode 100644 index 0000000..627649b --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import org.springframework.data.annotation.Persistent; + +import java.lang.annotation.*; + +/** + * Elasticsearch Mapping + * + * @author Mohsin Husen + */ +@Persistent +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.FIELD}) +public @interface Mapping { + + String mappingPath() default ""; + +} diff --git a/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java b/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java new file mode 100644 index 0000000..efbb09d --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java @@ -0,0 +1,9 @@ +package com.elastic.beans.domain; + +/** + * 用户实体 + * @Date: 2019/12/19 15:28 + **/ +public class User { + +} diff --git a/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java b/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java new file mode 100644 index 0000000..fb716ad --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java @@ -0,0 +1,29 @@ +package com.elastic.beans.es; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; + +import static org.springframework.data.elasticsearch.annotations.FieldType.Text; + +/** + * 用户实体对应的ES库实体 + * @Date: 2019/12/19 15:25 + **/ +@Document(indexName = "test-index-uuid-keyed", type = "test-type-uuid-keyed", + shards = 1, replicas = 0, refreshInterval = "-1") +public class BaseESEnity { + + @Id + private String id; + + @Field(type = Text, store = true, fielddata = true) + private String type; + + @Field(type = Text, store = true, fielddata = true) + private String message; + + @Version + private Long version; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java b/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java new file mode 100644 index 0000000..815f03b --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java @@ -0,0 +1,76 @@ +package com.elastic.config; + +import org.elasticsearch.client.Client; +import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.elasticsearch.transport.client.PreBuiltTransportClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * ES-Client封装工具类 + * @Date: 2019/12/19 15:32 + **/ +@Configuration("classpath:elasticsearch.properties") +public class ElasticSearchConfig { + + @Value("${elastic.cluster.name}") + private String CLUSTER_NAME; + + @Value("${elastic.cluster.clientTransportSniff}") + private String CLUSTER_CLIENT_TRANSPORTSNIFF; + + @Value("${elastic.cluster.discover.hostname}") + private String CLUSTER_HOSTNAME_PORT; + + /** + * 在Spring中,bean可以被定义为两种模式:prototype(原型)和singleton(单例) + * singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。Spring bean 默认是单例模式. + * prototype(原型):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 + */ + @Bean + @Scope("singleton") + public Client getClient() { + + String[] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); + + Settings settings = Settings.builder() + /*设置ES实例的名称*/ + .put("cluster.name", CLUSTER_NAME) + /*自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中*/ + .put("client.transport.sniff", CLUSTER_CLIENT_TRANSPORTSNIFF) + /*x-pack设置*/ + /*.put("xpack.security.transport.ssl.enabled", false) + //x-pack用户密码 elastic:changme是默认的用户名:密码 + .put("xpack.security.user", "elastic:changme")*/ + .build(); + + /*初始化client*/ + TransportClient transportClient = new PreBuiltTransportClient(settings); + String host; + int port; + String[] temp; + + if (0 != hostNamesPort.length){ + for (String hostPort : hostNamesPort) { + try { + temp = hostPort.split(":"); + host = temp[0].trim(); + port = Integer.parseInt(temp[1].trim()); + transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port)); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + } + + Client client = transportClient; + return client; + } +} \ No newline at end of file