From 77fe3f6f60292417f9d72b523b52ecc6ce5bbf4c Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Wed, 28 Sep 2022 14:37:40 +0800 Subject: [PATCH] plugin: Add elasticsearch --- README.md | 1 + plugin/jdbc/elasticsearch/pom.xml | 60 ++++++++++++++++ .../elasticsearch/ElasticSearchAdapter.java | 13 ++++ .../ElasticSearchConnection.java | 14 ++++ .../elasticsearch/ElasticSearchPlugin.java | 72 +++++++++++++++++++ .../ElasticSearchPluginModule.java | 38 ++++++++++ .../io.edurt.datacap.spi.PluginModule | 1 + .../ElasticSearchPluginModuleTest.java | 30 ++++++++ .../ElasticSearchPluginTest.java | 43 +++++++++++ plugin/jdbc/pom.xml | 1 + server/pom.xml | 5 ++ shared/plugin/elasticsearch.svg | 23 ++++++ 12 files changed, 301 insertions(+) create mode 100644 plugin/jdbc/elasticsearch/pom.xml create mode 100644 plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchAdapter.java create mode 100644 plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchConnection.java create mode 100644 plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPlugin.java create mode 100644 plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModule.java create mode 100644 plugin/jdbc/elasticsearch/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule create mode 100644 plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModuleTest.java create mode 100644 plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginTest.java create mode 100644 shared/plugin/elasticsearch.svg diff --git a/README.md b/README.md index 64ee6b5c..93bd874d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Here are some of the major database solutions that are supported: Redis PostgreSQL Trino + ElasticSearch

## Join Us diff --git a/plugin/jdbc/elasticsearch/pom.xml b/plugin/jdbc/elasticsearch/pom.xml new file mode 100644 index 00000000..ba571d80 --- /dev/null +++ b/plugin/jdbc/elasticsearch/pom.xml @@ -0,0 +1,60 @@ + + + + datacap-plugin-jdbc + io.edurt.datacap.plugin.jdbc + 1.0.0.20221015 + + 4.0.0 + + datacap-plugin-jdbc-elasticsearch + DataCap plugin for jdbc (ElasticSearch) + + + 6.8.11 + jdbc-elasticsearch + + + + + io.edurt.datacap + datacap-spi + + + org.elasticsearch.plugin + x-pack-sql-jdbc + ${x-pack-sql-jdbc.version} + + + commons-beanutils + commons-beanutils + + + + + + + maven-assembly-plugin + ${assembly-plugin.version} + + ${plugin.name} + + ../../../configure/assembly/assembly-plugin.xml + + ../../../dist/plugins/${plugin.name} + + + + make-assembly + package + + single + + + + + + + \ No newline at end of file diff --git a/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchAdapter.java b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchAdapter.java new file mode 100644 index 00000000..1994c048 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchAdapter.java @@ -0,0 +1,13 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import io.edurt.datacap.spi.adapter.JdbcAdapter; +import io.edurt.datacap.spi.connection.JdbcConnection; + +public class ElasticSearchAdapter + extends JdbcAdapter +{ + public ElasticSearchAdapter(JdbcConnection jdbcConnection) + { + super(jdbcConnection); + } +} diff --git a/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchConnection.java b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchConnection.java new file mode 100644 index 00000000..2aa4a557 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchConnection.java @@ -0,0 +1,14 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import io.edurt.datacap.spi.connection.JdbcConfigure; +import io.edurt.datacap.spi.connection.JdbcConnection; +import io.edurt.datacap.spi.model.Response; + +public class ElasticSearchConnection + extends JdbcConnection +{ + public ElasticSearchConnection(JdbcConfigure jdbcConfigure, Response response) + { + super(jdbcConfigure, response); + } +} diff --git a/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPlugin.java b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPlugin.java new file mode 100644 index 00000000..8265b5b9 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPlugin.java @@ -0,0 +1,72 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import io.edurt.datacap.spi.Plugin; +import io.edurt.datacap.spi.PluginType; +import io.edurt.datacap.spi.adapter.JdbcAdapter; +import io.edurt.datacap.spi.connection.JdbcConfigure; +import io.edurt.datacap.spi.model.Configure; +import io.edurt.datacap.spi.model.Response; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.beanutils.BeanUtils; + +@Slf4j +public class ElasticSearchPlugin + implements Plugin +{ + private JdbcConfigure jdbcConfigure; + private ElasticSearchConnection connection; + private Response response; + + @Override + public String name() + { + return "ElasticSearch"; + } + + @Override + public String description() + { + return "Integrate ElasticSearch data sources"; + } + + @Override + public PluginType type() + { + return PluginType.SOURCE; + } + + @Override + public void connect(Configure configure) + { + try { + this.response = new Response(); + this.jdbcConfigure = new JdbcConfigure(); + BeanUtils.copyProperties(this.jdbcConfigure, configure); + this.jdbcConfigure.setJdbcDriver("org.elasticsearch.xpack.sql.jdbc.EsDriver"); + this.jdbcConfigure.setJdbcType("es"); + this.connection = new ElasticSearchConnection(this.jdbcConfigure, this.response); + } + catch (Exception ex) { + this.response.setIsSuccessful(Boolean.FALSE); + this.response.setMessage(ex.getMessage()); + throw new RuntimeException(ex); + } + } + + @Override + public Response execute(String content) + { + log.info("Execute elasticsearch plugin logic started"); + this.response = this.connection.getResponse(); + JdbcAdapter processor = new ElasticSearchAdapter(this.connection); + this.response = processor.handlerJDBCExecute(content); + log.info("Execute elasticsearch plugin logic end"); + return this.response; + } + + @Override + public void destroy() + { + this.connection.destroy(); + } +} diff --git a/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModule.java b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModule.java new file mode 100644 index 00000000..a222236d --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/main/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModule.java @@ -0,0 +1,38 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import com.google.inject.multibindings.Multibinder; +import io.edurt.datacap.spi.AbstractPluginModule; +import io.edurt.datacap.spi.Plugin; +import io.edurt.datacap.spi.PluginModule; +import io.edurt.datacap.spi.PluginType; + +public class ElasticSearchPluginModule + extends AbstractPluginModule + implements PluginModule +{ + @Override + public String getName() + { + return "ElasticSearch"; + } + + @Override + public PluginType getType() + { + return PluginType.SOURCE; + } + + @Override + public AbstractPluginModule get() + { + return this; + } + + protected void configure() + { + Multibinder module = Multibinder.newSetBinder(this.binder(), String.class); + module.addBinding().toInstance(this.getClass().getSimpleName()); + Multibinder plugin = Multibinder.newSetBinder(this.binder(), Plugin.class); + plugin.addBinding().to(ElasticSearchPlugin.class); + } +} diff --git a/plugin/jdbc/elasticsearch/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule b/plugin/jdbc/elasticsearch/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule new file mode 100644 index 00000000..3f6061e5 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule @@ -0,0 +1 @@ +io.edurt.datacap.plugin.jdbc.elasticsearch.ElasticSearchPluginModule diff --git a/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModuleTest.java b/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModuleTest.java new file mode 100644 index 00000000..dfd08537 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginModuleTest.java @@ -0,0 +1,30 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import io.edurt.datacap.spi.Plugin; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +public class ElasticSearchPluginModuleTest +{ + private Injector injector; + + @Before + public void before() + { + this.injector = Guice.createInjector(new ElasticSearchPluginModule()); + } + + @Test + public void test() + { + Set plugins = injector.getInstance(Key.get(new TypeLiteral>() {})); + Assert.assertTrue(plugins.size() > 0); + } +} diff --git a/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginTest.java b/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginTest.java new file mode 100644 index 00000000..d85cb878 --- /dev/null +++ b/plugin/jdbc/elasticsearch/src/test/java/io/edurt/datacap/plugin/jdbc/elasticsearch/ElasticSearchPluginTest.java @@ -0,0 +1,43 @@ +package io.edurt.datacap.plugin.jdbc.elasticsearch; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import io.edurt.datacap.spi.Plugin; +import io.edurt.datacap.spi.model.Configure; +import org.junit.Before; +import org.junit.Test; + +import java.util.Optional; +import java.util.Set; + +public class ElasticSearchPluginTest +{ + private Injector injector; + private Configure configure; + + @Before + public void before() + { + injector = Guice.createInjector(new ElasticSearchPluginModule()); + configure = new Configure(); + configure.setHost("127.0.0.1"); + configure.setPort(9200); + } + + @Test + public void test() + { + Set plugins = injector.getInstance(Key.get(new TypeLiteral>() {})); + Optional pluginOptional = plugins.stream() + .filter(v -> v.name().equalsIgnoreCase("ElasticSearch")) + .findFirst(); + if (pluginOptional.isPresent()) { + Plugin plugin = pluginOptional.get(); + plugin.connect(configure); + System.out.println(plugin.execute("SHOW TABLES")); + plugin.destroy(); + } + } +} diff --git a/plugin/jdbc/pom.xml b/plugin/jdbc/pom.xml index 282fcf31..c37dd69a 100644 --- a/plugin/jdbc/pom.xml +++ b/plugin/jdbc/pom.xml @@ -20,5 +20,6 @@ postgresql redis trino + elasticsearch \ No newline at end of file diff --git a/server/pom.xml b/server/pom.xml index 127cf9e5..bce1894c 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -99,6 +99,11 @@ datacap-plugin-jdbc-postgresql ${project.version} + + io.edurt.datacap.plugin.jdbc + datacap-plugin-jdbc-elasticsearch + ${project.version} + diff --git a/shared/plugin/elasticsearch.svg b/shared/plugin/elasticsearch.svg new file mode 100644 index 00000000..bdd28446 --- /dev/null +++ b/shared/plugin/elasticsearch.svg @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + +