Beispiel #1
0
  /**
   * Builds the Elasticsearch client using the properties this connection was instantiated with
   *
   * @return
   * @throws SQLException
   */
  private Client buildClient() throws SQLException {
    if (props.containsKey("test")) { // used for integration tests
      return ESIntegTestCase.client();
    } else
      try {
        Settings.Builder settingsBuilder = Settings.settingsBuilder();
        for (Object key : this.props.keySet()) {
          settingsBuilder.put(key, this.props.get(key));
        }
        Settings settings = settingsBuilder.build();
        TransportClient client =
            TransportClient.builder()
                .settings(settings)
                .build()
                .addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(host), port));

        // add additional hosts if set in URL query part
        if (this.props.containsKey("es.hosts"))
          for (String hostPort : this.props.getProperty("es.hosts").split(",")) {
            String newHost = hostPort.split(":")[0].trim();
            int newPort =
                (hostPort.split(":").length > 1
                    ? Integer.parseInt(hostPort.split(":")[1])
                    : Utils.PORT);
            client.addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName(newHost), newPort));
            logger.info("Adding additional ES host: " + hostPort);
          }

        // check if index exists
        if (index != null) {
          boolean indexExists =
              client
                  .admin()
                  .indices()
                  .exists(new IndicesExistsRequest(index))
                  .actionGet()
                  .isExists();
          if (!indexExists) throw new SQLException("Index or Alias '" + index + "' does not exist");
        }
        return client;
      } catch (UnknownHostException e) {
        throw new SQLException("Unable to connect to " + host, e);
      } catch (Throwable t) {
        throw new SQLException("Unable to connect to database", t);
      }
  }