如何存储,版本和部署elasticsearch河流插件设置?

时间:2014-12-23 12:30:55

标签: java-ee elasticsearch

我正在Java EE项目中部署ES embedded并尝试提供设置整个应用程序所需的配置,包括war文件中的ES和ES插件配置。目前我正在整合jdbc river plugin。有很多关于如何通过ES REST API配置(插件)的文档,它工作正常,但我找不到有关如何将配置捆绑到我可以提供给ES的设置文件中的信息设置河流。

如何创建ES的自包含版本控制包? 是否有一种ES方式可以像使用elasticsearch.yml那样进行核心设置,还是需要手动/以编程方式更新我的ES设置?

注意:war嵌入式ES配置是一项要求,但随时可以回答如何将其存档为独立的ES配置。

1 个答案:

答案 0 :(得分:0)

为了记录:我最终编写了一个配置ES的ElasticSearchStarter:

package com.abc.xyz.resources.search;

import java.io.IOException;
import java.io.InputStream;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.slf4j.Logger;

@Singleton
@Startup
public class ElasticSearchStarter {

  private static final String TRANSPORT_TCP_PORT = "transport.tcp.port";

  private static final String TAGLINE = "tagline";

  private static final String DISCOVERY_ZEN_PING_MULTICAST_ENABLED = "discovery.zen.ping.multicast.enabled";

  public static final String INDEX = "abc";

  @Inject
  Logger logger;

  // es settings
  private static final String HTTP_ENABLED = "http.enabled";
  private static final String PATH_DATA = "path.data";
  private static final String PATH_WORK = "path.work";
  private static final String PATH_LOGS = "path.logs";
  private static final String PATH_UNICAST_HOSTS = "discovery.zen.ping.unicast.hosts";

  @Resource(lookup = "java:global/xyz/ELASTIC_SEARCH_PATH_DATA")
  private String pathData;

  @Resource(lookup = "java:global/xyz/ELASTIC_SEARCH_PATH_WORK")
  private String pathWork;

  @Resource(lookup = "java:global/xyz/ELASTIC_SEARCH_PATH_LOGS")
  private String pathLogs;

  @Resource(lookup = "java:global/xyz/ELASTIC_SEARCH_TRANSPORT_TCP_PORT")
  private String transportTcpPort;

  @Resource(lookup = "java:global/xyz/ELASTIC_SEARCH_UNICAST_HOSTS")
  private String unicastHosts;

  protected Node node;

  @PostConstruct
  void init() throws ElasticSearchStartUpException {
    ImmutableSettings.Builder settings = buildSettings();

    node = NodeBuilder.nodeBuilder().settings(settings).node();

    getNode().start();
  }

  private ImmutableSettings.Builder buildSettings() throws ElasticSearchStartUpException {
    // ES settings
    ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
    InputStream resourceAsStream = getClass().getResourceAsStream("elasticsearch.yml");
    if (resourceAsStream != null) {
      settings.loadFromStream("elasticsearch.yml", resourceAsStream);
      logger.info("Settings loaded from elasticsearch.yml");
      try {
        resourceAsStream.close();
      } catch (IOException e) {
        String msg = "Error loading Settings loaded from elasticsearch.yml";
        logger.error(msg + e);
        throw new ElasticSearchStartUpException(msg, e);
      }
    } else {
      String msg = "Loading elasticsearch.yml failed! resourceAsStream was null";
      logger.error(msg );
      throw new ElasticSearchStartUpException(msg);
    }

    // See also elasticsearch.yml
    settings.put(TAGLINE, "You know nothing... Ask me!");
    settings.put(HTTP_ENABLED, false);
    settings.put(PATH_DATA, pathData);
    settings.put(PATH_WORK, pathWork);
    settings.put(PATH_LOGS, pathLogs);
    settings.put(TRANSPORT_TCP_PORT, transportTcpPort);
    settings.put(DISCOVERY_ZEN_PING_MULTICAST_ENABLED, false);

    int i = 0;
    for (String unicastHost : unicastHosts.split(",")) {
      settings.put(PATH_UNICAST_HOSTS + "." + i, unicastHost.trim());
      i++;
    }

    return settings;
  }

  @PreDestroy
  void kill() {
    // stop and close node
    if (!getNode().isClosed()) {
      getNode().close();
    }
  }

  public Node getNode() {
    return node;
  }

  @Produces
  public Client getClient() {
    return node.client();
  }
}

然后,我通过CDI事件对构造做出了反应,并通过Java API推送了JDBC River设置,但最终将JDBC河流全部转储,因为它会绕过JavaEE和Application Server提供的所有功能。 (例如,使用应用程序服务器定义的数据源)

修改:https://github.com/dadoonet/elasticsearch-beyonder可能有用。

ps:更好的答案仍然欢迎。