From 49b00ac27bd6c59f7622211832878e57e10c95fc Mon Sep 17 00:00:00 2001 From: mhuang Date: Tue, 8 Oct 2019 17:48:03 +0800 Subject: [PATCH] =?UTF-8?q?!1=20demo=E5=90=88=E5=B9=B6=20*=201.1.0=20?= =?UTF-8?q?=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 13 ++++ pom.xml | 56 +++++++++++++++ .../sample/ESSampleApplication.java | 44 ++++++++++++ .../springboot/sample/TestES.java | 69 +++++++++++++++++++ src/main/resources/application.properties | 3 + 5 files changed, 185 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java create mode 100644 src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java create mode 100644 src/main/resources/application.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f8ca9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.settings +target +.project +.classpath +bin +logs +.springBeans +*.iml +.idea +~* +*.log +.metadata +*module_*.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a7b41e7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + tech.mhuang + hmtool-ext-elasticsearch-springboot-sample + 1.1.0 + + 1.1.0 + UTF-8 + 1.8 + 2.1.5.RELEASE + 2.9.9 + + + + + tech.mhuang + hmtool-ext-elasticsearch-springboot + ${hmtool.ext.elasticsearch.springboot.version} + + + + org.springframework.boot + spring-boot-starter + ${springboot.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + \ No newline at end of file diff --git a/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java new file mode 100644 index 0000000..f7ebd3e --- /dev/null +++ b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java @@ -0,0 +1,44 @@ +package tech.mhuang.ext.elasticsearch.springboot.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import tech.mhuang.ext.elasticsearch.admin.external.IESExternal; +import tech.mhuang.ext.elasticsearch.admin.factory.IESFactory; +import tech.mhuang.ext.elasticsearch.server.ESFactory; + +/** + * + * jwt测试 + * + * @author mhuang + * @since 1.0.0 + */ +@SpringBootApplication +public class ESSampleApplication { + + /** + * 自定义ES创建类 + * @return + */ + static class CustomESExternal implements IESExternal { + @Override + public IESFactory create(String key) { + System.out.println("自定义ES生产key"+key); + return new ESFactory(); + } + } + + /** + * 自定义ES + * @return + */ + @Bean + public IESExternal CustomESExternal(){ + return new CustomESExternal(); + } + + public static void main(String[] args) { + SpringApplication.run(ESSampleApplication.class,args); + } +} diff --git a/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java new file mode 100644 index 0000000..6d22f04 --- /dev/null +++ b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java @@ -0,0 +1,69 @@ +package tech.mhuang.ext.elasticsearch.springboot.sample; + + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; +import tech.mhuang.ext.elasticsearch.admin.ESFramework; +import tech.mhuang.ext.elasticsearch.admin.factory.IESFactory; +import tech.mhuang.ext.elasticsearch.server.annoation.ESTable; + +import java.io.IOException; + +/** + * + * 测试 + * + * @author mhuang + * @since 1.0.0 + */ +@Slf4j +@Component +public class TestES implements ApplicationRunner { + + @Autowired + private ESFramework esFramework; + @Override + public void run(ApplicationArguments args) throws Exception { + try { + IESFactory esFactory = esFramework.getFactory("zhangsan"); + //ES新增 + IndexResponse response = esFactory.insert("{\"age\":22}", "test", "test"); + log.info("新增1:{}", response); + Test test = new Test(); + IndexResponse response2 = esFactory.insert(test); + log.info("新增2:{}", response2); + String id = response2.getId(); + test.setName("你好"); + //ES修改 + UpdateResponse response3 = esFactory.update(test, id); + log.info("修改:{}", response3); + //ES删除 + DeleteResponse response4 = esFactory.delete("test", "test", id); + log.info("删除:{}", response4); + //ES清空索引所有数据 + AcknowledgedResponse response5 = esFactory.delete("test"); + log.info("删除索引:{}", response5); + //获取RestHighLevelClient对象 + RestHighLevelClient client = esFactory.getClient(); + log.info("打印信息{}", client.info(RequestOptions.DEFAULT)); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Data + @ESTable(index = "test", type = "test") + static class Test { + private String name = "张三"; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..0fdfbad --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,3 @@ +mhuang.elasticsearch.enable=true +mhuang.elasticsearch.bean-map.zhangsan.enable=true +mhuang.elasticsearch.bean-map.zhangsan.ip=192.168.1.210 \ No newline at end of file -- Gitee