@Test
  public void testWithSniffing() throws Exception {
    TransportClient client =
        new TransportClient(
            ImmutableSettings.builder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "cluster1")
                .put("node.name", "transport_client_" + this.getTestName() + "_1")
                .put("client.transport.nodes_sampler_interval", "1s")
                .put(
                    TransportModule.TRANSPORT_SERVICE_TYPE_KEY,
                    InternalTransportService.class.getName())
                .put(HEADER_SETTINGS)
                .build());
    try {
      client.addTransportAddress(address);

      InternalTransportService service =
          (InternalTransportService) client.injector.getInstance(TransportService.class);

      if (!service.clusterStateLatch.await(5, TimeUnit.SECONDS)) {
        fail("takes way too long to get the cluster state");
      }

      assertThat(client.connectedNodes().size(), is(1));
      assertThat(client.connectedNodes().get(0).getAddress(), is((TransportAddress) address));
    } finally {
      client.close();
    }
  }
  public void testWithSniffing() throws Exception {
    try (TransportClient client =
        TransportClient.builder()
            .settings(
                Settings.builder()
                    .put("client.transport.sniff", true)
                    .put("cluster.name", "cluster1")
                    .put("node.name", "transport_client_" + this.getTestName() + "_1")
                    .put("client.transport.nodes_sampler_interval", "1s")
                    .put(HEADER_SETTINGS)
                    .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
                    .build())
            .addPlugin(InternalTransportService.TestPlugin.class)
            .build()) {
      client.addTransportAddress(address);

      InternalTransportService service =
          (InternalTransportService) client.injector.getInstance(TransportService.class);

      if (!service.clusterStateLatch.await(5, TimeUnit.SECONDS)) {
        fail("takes way too long to get the cluster state");
      }

      assertThat(client.connectedNodes().size(), is(1));
      assertThat(client.connectedNodes().get(0).getAddress(), is((TransportAddress) address));
    }
  }
  @Override
  protected Client createClient(ImmutableSettings.Builder builder) {
    if (_transportAddresses.isEmpty()) {
      throw new IllegalStateException("There must be at least one transport address");
    }

    Class<?> clazz = getClass();

    builder.classLoader(clazz.getClassLoader());

    builder.loadFromClasspath(getConfigFileName());

    TransportClient transportClient = new TransportClient(builder);

    builder.put("client.transport.sniff", true);
    builder.put("cluster.name", getClusterName());

    for (String transportAddress : _transportAddresses) {
      String[] transportAddressParts = StringUtil.split(transportAddress, StringPool.COLON);

      try {
        InetAddress inetAddress = InetAddress.getByName(transportAddressParts[0]);

        int port = GetterUtil.getInteger(transportAddressParts[1]);

        transportClient.addTransportAddress(new InetSocketTransportAddress(inetAddress, port));
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to add transport address " + transportAddress, e);
        }
      }
    }

    return transportClient;
  }
Ejemplo n.º 4
0
 @Bean
 public Client client() {
   TransportClient client = new TransportClient();
   TransportAddress address = new InetSocketTransportAddress(elasticHost, elasticPort);
   client.addTransportAddress(address);
   return client;
 }
  public void run() {
    Client client = null;
    TransportClient transportClient = null;
    try {
      transportClient = new TransportClient();
      client =
          transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9300));

      SearchResponse response =
          client
              .prepareSearch("tms-allflat")
              .setTypes("personal")
              .setQuery(QueryBuilders.matchAllQuery())
              .addAggregation(
                  AggregationBuilders.terms("aggs1")
                      .field("skill_1")
                      .size(20)
                      .order(Terms.Order.count(false)))
              .execute()
              .actionGet();

      Terms terms = response.getAggregations().get("aggs1");
      terms
          .getBuckets()
          .stream()
          .forEach(s -> System.out.println(s.getKeyAsText() + "(" + s.getDocCount() + ")"));

    } finally {
      transportClient.close();
      client.close();
    }
  }
  @PostConstruct
  public void init() {
    final ImmutableSettings.Builder settings = settingsBuilder();
    settings.put("cluster.name", "es-events");
    client = new TransportClient(settings);
    LOGGER.info("Adding ES hosts:");
    for (String host : config.getEsHosts().split(",")) {
      LOGGER.info(host);
      client.addTransportAddress(new InetSocketTransportAddress(host, config.getEsPort()));
    }
    LOGGER.info("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");

    define();
  }
 protected void buildClient() throws Exception {
   client = TransportClient.builder().settings(settings()).build();
   Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing.");
   for (String clusterNode : split(clusterNodes, COMMA)) {
     String hostName = substringBeforeLast(clusterNode, COLON);
     String port = substringAfterLast(clusterNode, COLON);
     Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'");
     Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
     logger.info("adding transport node : " + clusterNode);
     client.addTransportAddress(
         new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port)));
   }
   client.connectedNodes();
 }
 public static void main(String[] args) {
   TransportClient client = new TransportClient();
   client.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
   TestBulkProcessor test = new TestBulkProcessor(client);
   int count = 0;
   int processors = Runtime.getRuntime().availableProcessors() - 1;
   while (count++ < 20) {
     int count2 = 0;
     while (count2++ < 3) {
       test.testBulkProcessor(processors);
     }
     processors++;
   }
   client.close();
 }
Ejemplo n.º 9
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);
      }
  }
  private Client createTransportClient() {
    Settings settings =
        ImmutableSettings.settingsBuilder()
            .put("cluster.name", configuration.getClusterName())
            .build();
    TransportClient transportClient = new TransportClient(settings);

    List<HostAddress> adresses = configuration.getHostsAddresses();

    for (HostAddress address : adresses) {
      transportClient.addTransportAddress(
          new InetSocketTransportAddress(address.getHostname(), address.getPort()));
    }

    return transportClient;
  }
  @Override
  protected Client buildClient(Settings headersSettings, GenericAction[] testedActions) {
    TransportClient client =
        new TransportClient(
            ImmutableSettings.builder()
                .put("client.transport.sniff", false)
                .put("node.name", "transport_client_" + this.getTestName())
                .put(
                    TransportModule.TRANSPORT_SERVICE_TYPE_KEY,
                    InternalTransportService.class.getName())
                .put(HEADER_SETTINGS)
                .build());

    client.addTransportAddress(address);
    return client;
  }
  @Override
  protected Client buildClient(Settings headersSettings, GenericAction[] testedActions) {
    TransportClient client =
        TransportClient.builder()
            .settings(
                Settings.builder()
                    .put("client.transport.sniff", false)
                    .put("cluster.name", "cluster1")
                    .put("node.name", "transport_client_" + this.getTestName())
                    .put(headersSettings)
                    .build())
            .addPlugin(InternalTransportService.TestPlugin.class)
            .build();

    client.addTransportAddress(address);
    return client;
  }
  public void testNodeVersionIsUpdated() {
    TransportClient client = (TransportClient) internalCluster().client();
    TransportClientNodesService nodeService = client.nodeService();
    Node node =
        new Node(
            Settings.builder()
                .put(internalCluster().getDefaultSettings())
                .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
                .put("node.name", "testNodeVersionIsUpdated")
                .put("http.enabled", false)
                .put(Node.NODE_DATA_SETTING.getKey(), false)
                .put("cluster.name", "foobar")
                .put(
                    InternalSettingsPreparer.IGNORE_SYSTEM_PROPERTIES_SETTING.getKey(),
                    true) // make sure we get what we set :)
                .build());
    node.start();
    try {
      TransportAddress transportAddress =
          node.injector().getInstance(TransportService.class).boundAddress().publishAddress();
      client.addTransportAddress(transportAddress);
      assertThat(
          nodeService.connectedNodes().size(),
          greaterThanOrEqualTo(
              1)); // since we force transport clients there has to be one node started that we
                   // connect to.
      for (DiscoveryNode discoveryNode :
          nodeService.connectedNodes()) { // connected nodes have updated version
        assertThat(discoveryNode.getVersion(), equalTo(Version.CURRENT));
      }

      for (DiscoveryNode discoveryNode : nodeService.listedNodes()) {
        assertThat(discoveryNode.id(), startsWith("#transport#-"));
        assertThat(
            discoveryNode.getVersion(), equalTo(Version.CURRENT.minimumCompatibilityVersion()));
      }

      assertThat(nodeService.filteredNodes().size(), equalTo(1));
      for (DiscoveryNode discoveryNode : nodeService.filteredNodes()) {
        assertThat(
            discoveryNode.getVersion(), equalTo(Version.CURRENT.minimumCompatibilityVersion()));
      }
    } finally {
      node.close();
    }
  }
Ejemplo n.º 14
0
 @Override
 public Client client(Node node, String clusterName, Random random) {
   TransportAddress addr =
       ((InternalNode) node)
           .injector()
           .getInstance(TransportService.class)
           .boundAddress()
           .publishAddress();
   TransportClient client =
       new TransportClient(
           settingsBuilder()
               .put("client.transport.nodes_sampler_interval", "1s")
               .put("name", "transport_client_" + node.settings().get("name"))
               .put("cluster.name", clusterName)
               .put("client.transport.sniff", sniff)
               .build());
   client.addTransportAddress(addr);
   return client;
 }
  /** 创建客服端 */
  @Override
  protected Client createInstance() throws Exception {

    if (typology == null)
      throw new BeanCreationException(
          "Error creating "
              + Client.class.getName()
              + ": 'typology' property is required. Between "
              + EsClientType.values());

    switch (typology) {
      case local:
        NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
        node = nodeBuilder.node();
        esClient = node.client();
        break;

      case remote:
        if (StringUtils.isBlank(nodes)) {
          throw new BeanCreationException(
              "Error creating "
                  + Client.class.getName()
                  + ": 'nodes' property is required if 'remote' typology is set");
        }
        Collection<InetSocketTransportAddress> addresses = fromNodes(nodes);
        Settings settings =
            ImmutableSettings.settingsBuilder()
                .put("client.transport.sniff", true)
                .put("cluster.name", clusName)
                .build();
        esClient = new TransportClient(settings);
        for (InetSocketTransportAddress address : addresses) {
          ((TransportClient) esClient).addTransportAddress(address);
        }
        break;
    }

    return esClient;
  }
Ejemplo n.º 16
0
  synchronized Client getClient() {
    if (nodeInfo == null) {
      throw new IllegalStateException("Node has not started yet");
    }
    if (client == null) {
      TransportAddress addr = nodeInfo.getTransport().getAddress().publishAddress();
      // verify that the end node setting will have network enabled.

      Settings clientSettings =
          settingsBuilder()
              .put(externalNodeSettings)
              .put("client.transport.nodes_sampler_interval", "1s")
              .put("name", "transport_client_" + nodeInfo.getNode().name())
              .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), clusterName)
              .put("client.transport.sniff", false)
              .build();
      TransportClient client = TransportClient.builder().settings(clientSettings).build();
      client.addTransportAddress(addr);
      this.client = client;
    }
    return client;
  }
 /*
  * (non-Javadoc)
  *
  * @see com.datatorrent.lib.db.Connectable#connect()
  */
 @Override
 public void connect() throws IOException {
   client = new TransportClient();
   client.addTransportAddress(new InetSocketTransportAddress(hostName, port));
 }
Ejemplo n.º 18
0
 public static Client createClient(String cluster, String url, int port) {
   Settings s = ImmutableSettings.settingsBuilder().put("cluster.name", cluster).build();
   TransportClient tmp = new TransportClient(s);
   tmp.addTransportAddress(new InetSocketTransportAddress(url, port));
   return tmp;
 }