カスタム DataCap プラグイン Java 実装

DataCap はカスタム プラグインをサポートしており、ユーザーは独自のプラグインを作成してシステムに統合できます。このドキュメントでは主に、プラグインを DataCap システムに迅速に統合する方法について説明します。

この記事では、HTTP プロトコルに基づいた統合 QuestDB データ ストレージ システムの使用方法を示します。

pom.xml頼る

<dependency>
    <groupId>io.edurt.datacap</groupId>
    <artifactId>datacap-spi</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>io.edurt.datacap</groupId>
    <artifactId>datacap-common</artifactId>
</dependency>
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <scope>test</scope>
</dependency>

上記の構成では、モジュールdatacap-spidatacap-commonモジュールが追加され、その他は補助的な依存関係です。

プロジェクトを個別に開く場合は、各依存関係のバージョン番号を指定する必要があることに注意してください。

プラグインローダー

public class QuestDBPluginModule
        extends AbstractPluginModule
        implements PluginModule
{
    @Override
    public String getName()
    {
        return "QuestDB";
    }

    @Override
    public PluginType getType()
    {
        return PluginType.HTTP;
    }

    @Override
    public AbstractPluginModule get()
    {
        return this;
    }

    protected void configure()
    {
        Multibinder<Plugin> plugin = Multibinder.newSetBinder(this.binder(), Plugin.class);
        plugin.addBinding().to(QuestDBPlugin.class);
    }
}

システムの起動時にプラグインがシステムに自動的にロードされるように、ローダーはAbstractPluginModuleクラス。PluginModule

親クラスのconfigure()メソッドを。

プラグインエグゼキュータ

@Slf4j
public class QuestDBPlugin
        implements Plugin
{
    private HttpConfigure configure;
    private HttpConnection connection;
    private Response response;

    @Override
    public String name()
    {
        return "QuestDB";
    }

    @Override
    public String description()
    {
        return "Integrate QuestDB data sources";
    }

    @Override
    public PluginType type()
    {
        return PluginType.HTTP;
    }

    @Override
    public void connect(Configure configure)
    {
        try {
            this.response = new Response();
            this.configure = new HttpConfigure();
            BeanUtils.copyProperties(this.configure, configure);
            this.connection = new HttpConnection(this.configure, this.response);
        }
        catch (Exception ex) {
            this.response.setIsConnected(Boolean.FALSE);
            this.response.setMessage(ex.getMessage());
        }
    }

    @Override
    public Response execute(String content)
    {
        if (ObjectUtils.isNotEmpty(this.connection)) {
            log.info("Execute questdb plugin logic started");
            this.response = this.connection.getResponse();
            QuestDBAdapter processor = new QuestDBAdapter(this.connection);
            this.response = processor.handlerExecute(content);
            log.info("Execute questdb plugin logic end");
        }
        this.destroy();
        return this.response;
    }

    @Override
    public void destroy()
    {
        if (ObjectUtils.isNotEmpty(this.connection)) {
            this.connection.destroy();
        }
    }
}

エグゼキューターは、次のメソッドを提供するPluginインターフェース

  • name(): プラグインには一意の名前があり、同じ名前のプラグインは初めてロードされたときにのみ有効になります。
  • description(): プラグインの説明
  • type(): プラグインの種類
  • connect(Configure configure): プラグインには、現在のプラグインなどの接続情報が事前に必要です。これは、プラグインの接続フェーズです (システムは、直接使用される HTTP 接続方法を事前に設定します)。
  • execute(String content): 演算ロジックを具体的に実行する
  • destroy(): プラグインの最終的な破棄。破棄には接続内の情報を含める必要があることに注意してください。

プラグインコンバーター

@Slf4j
public class QuestDBAdapter
        extends HttpAdapter
{
    public QuestDBAdapter(HttpConnection connection)
    {
        super(connection);
    }

    @Override
    public Response handlerExecute(String content)
    {
        Time processorTime = new Time();
        processorTime.setStart(new Date().getTime());
        Response response = this.httpConnection.getResponse();
        HttpConfigure configure = new HttpConfigure();
        if (response.getIsConnected()) {
            List<String> headers = new ArrayList<>();
            List<String> types = new ArrayList<>();
            List<Object> columns = new ArrayList<>();
            try {
                BeanUtils.copyProperties(configure, this.httpConnection.getConfigure());
                configure.setAutoConnected(Boolean.FALSE);
                configure.setRetry(0);
                configure.setMethod(HttpMethod.GET);
                configure.setPath("exec");
                Map<String, String> parameters = Maps.newHashMap();
                parameters.put("query", content);
                configure.setParams(parameters);
                configure.setDecoded(true);
                HttpConnection httpConnection = new HttpConnection(configure, new Response());
                HttpClient httpClient = HttpClient.getInstance(configure, httpConnection);
                String body = httpClient.execute();
                QuestDBResponse requestResponse = JSON.objectmapper.readValue(body, QuestDBResponse.class);
                if (ObjectUtils.isNotEmpty(requestResponse.getQuery())) {
                    response.setIsSuccessful(true);
                    if (ObjectUtils.isNotEmpty(requestResponse.getColumns())) {
                        requestResponse.getColumns()
                                .forEach(schema -> {
                                    headers.add(schema.getName());
                                    types.add(schema.getType());
                                });
                    }
                    requestResponse.getDataset()
                            .forEach(record -> columns.add(handlerFormatter(configure.getFormat(), headers, record)));
                }
                else {
                    response.setIsSuccessful(Boolean.FALSE);
                    response.setMessage(requestResponse.getError());
                }
            }
            catch (Exception ex) {
                log.error("Execute content failed content {} exception ", content, ex);
                response.setIsSuccessful(Boolean.FALSE);
                response.setMessage(ex.getMessage());
            }
            finally {
                response.setHeaders(headers);
                response.setTypes(types);
                response.setColumns(columns);
            }
        }
        processorTime.setEnd(new Date().getTime());
        response.setProcessor(processorTime);
        return response;
    }
}

プラグイン コンバーターは、現在のプラグインの実行後の結果を変換し、DataCap で使用できるロジックに変換するために使用されます。これは主に、Response返された。

本記事はJDBCをベースにしたプラグインなので、一部の機能はHttpAdapter親。

SPIローダー

resourcesソースディレクトリの下にディレクトリMETA-INFを追加しますservices

servicesresourcesディレクトリ内に必要なもの

io.edurt.datacap.spi.PluginModule次の内容のファイルを作成します

io.edurt.datacap.plugin.http.questdb.QuestDBPluginModule

このファイルの内容は、定義したプラグイン読み込みモジュールです。

プラグインの単体テストは、公開されているテスト用プラグインを参照できます。

おすすめ

転載: juejin.im/post/7246934556811722813