org.elasticsearch.common.transport.InetSocketTransportAddress Java Examples

The following examples show how to use org.elasticsearch.common.transport.InetSocketTransportAddress. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
/**
	 * 创建TransportClient
	 * @return
	 */
	private static Client createTransportClient() {
		//创建settings
		Settings settings = ImmutableSettings.settingsBuilder()
			.put("cluster.name", "elasticsearch")//设置集群名称
//		    .put("shield.user", "admin:sysadmin")
			.build();
		Client client = null;
		try {
			client = new org.elasticsearch.client.transport.TransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return client;
	}
 
Example #2
Source File: Elasticsearch2Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();
        TransportClient client = TransportClient.builder().settings(settings).build();
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        LOG.info("Connection to instance %s at %s established, user %s");
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #3
Source File: Elasticsearch5Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();

        TransportClient client = new PreBuiltTransportClient(settings);
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #4
Source File: ElasticsearchServiceImpl.java    From canal-elasticsearch with Apache License 2.0 6 votes vote down vote up
public ElasticsearchServiceImpl(EsConf esConf) throws UnknownHostException {

        String clusterName = esConf.getClusterName();
        String address = esConf.getAddress();
        String[] hostPort = address.split(":");

        logger.info("Connect to elasticsearch  {}:{}", clusterName, address);

        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.valueOf(hostPort[1])));


        logger.info("Complete the connection to elasticsearch");
    }
 
Example #5
Source File: EsClient.java    From hsweb-learning with Apache License 2.0 6 votes vote down vote up
public static TransportClient getClient() {
    try {
        if (tclient == null) {
            String EsHosts = "192.168.1.41:9300,192.168.1.42:9300,192.168.1.43:9300";
            Settings settings = Settings.settingsBuilder()
                    .put("cluster.name", "dkes")//设置集群名称
                    .put("tclient.transport.sniff", true).build();//自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中

            tclient = TransportClient.builder().settings(settings).build();
            String[] nodes = EsHosts.split(",");
            for (String node : nodes) {
                if (node.length() > 0) {//跳过为空的node(当开头、结尾有逗号或多个连续逗号时会出现空node)
                    String[] hostPort = node.split(":");
                    tclient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));

                }
            }
        }//if
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tclient;
}
 
Example #6
Source File: ESClient.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    try {
        LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
        Settings settings = Settings.builder()
                .put("cluster.name", staticValue.getEsClusterName()).build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example #7
Source File: TestElasticSearchSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseConfigurationUsingDefaults() {
  parameters.put(HOSTNAMES, "10.5.5.27");
  parameters.remove(INDEX_NAME);
  parameters.remove(INDEX_TYPE);
  parameters.remove(CLUSTER_NAME);

  fixture = new ElasticSearchSink();
  fixture.configure(new Context(parameters));

  InetSocketTransportAddress[] expected = { new InetSocketTransportAddress(
      "10.5.5.27", DEFAULT_PORT) };

  assertEquals(DEFAULT_INDEX_NAME, fixture.getIndexName());
  assertEquals(DEFAULT_INDEX_TYPE, fixture.getIndexType());
  assertEquals(DEFAULT_CLUSTER_NAME, fixture.getClusterName());
  assertArrayEquals(expected, fixture.getServerAddresses());
}
 
Example #8
Source File: DefaultClientFactory.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
@Override
public ShellNativeClient newTransportClient(String... addresses) {
    TransportAddress[] transportAddresses = new TransportAddress[addresses.length];
    for (int i = 0; i < addresses.length; i++) {
        String address = addresses[i];

        String[] splitAddress = address.split(":");
        String host = shellSettings.settings().get(ShellSettings.TRANSPORT_HOST);

        if (splitAddress.length>=1) {
            host = splitAddress[0];
        }

        int port = shellSettings.settings().getAsInt(ShellSettings.TRANSPORT_PORT, null);
        if (splitAddress.length>=2) {
            try {
                port = Integer.valueOf(splitAddress[1]);
            } catch(NumberFormatException e) {
                logger.warn("Unable to parse port [{}]", splitAddress[1], e);
            }
        }

        transportAddresses[i] = new InetSocketTransportAddress(host, port);
    }
    return newTransportClient(transportAddresses);
}
 
Example #9
Source File: EshClientFactory.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
public TransportClient getClient()   {
    if (esClient == null) {
        synchronized (this) {
            if (esClient == null) {
                try {
                    //判断配置
                    Preconditions.checkNotNull(clusterName, "es 服务clusterName未配置");
                    Preconditions.checkNotNull(addresses, "es 服务ip未配置");
                    //Preconditions.checkArgument(esPort > 0, "es 服务服务port未配置");
                    //设置集群的名字
                    Settings settings = Settings.settingsBuilder().put("client.node", true).put("cluster.name", clusterName).put("client.transport.sniff", sniff).build();
                    //Settings settings = Settings.settingsBuilder().put("client.transport.sniff", sniff).build();
                    //创建集群client并添加集群节点地址
                    esClient = TransportClient.builder().settings(settings).build();
                    for (String address : addresses) {
                        String[] inetAddress = address.split(":");
                        esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress[0]), new Integer(inetAddress[1])));
                    }
                  }catch (Exception e){
                     LOGGER.error("客户端连接初始化异常",e);
                }
            }
        }
    }
    return esClient;
}
 
Example #10
Source File: SearchES.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * 多字段查询
 */
public static void multisearch() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2","clients");
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("id","5"))
                        .should(QueryBuilders.prefixQuery("content","oracle")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #11
Source File: SearchClientServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
protected Client createClient() {
	if (client == null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Creating client for Search!");
		}
		// Try starting search client at context loading
		try {
			final Settings settings = ImmutableSettings
					.settingsBuilder()
					.put(ElasticSearchReservedWords.CLUSTER_NAME.getText(),
							searchServerClusterName).build();
			TransportClient transportClient = new TransportClient(settings);
			transportClient = transportClient
					.addTransportAddress(new InetSocketTransportAddress(
							"localhost", 9300));
			if (transportClient.connectedNodes().size() == 0) {
				logger.error("There are no active nodes available for the transport, it will be automatically added once nodes are live!");
			}
			client = transportClient;
		} catch (final Exception ex) {
			// ignore any exception, dont want to stop context loading
			logger.error("Error occured while creating search client!", ex);
		}
	}
	return client;
}
 
Example #12
Source File: ElasticSearchTransportClientProvider.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public Client get() {

    Settings settings = Settings.builder()
            .put("client.transport.ignore_cluster_name", true)
            .put("client.transport.sniff", true)
            .build();

    TransportClient tc = new PreBuiltTransportClient(settings);

    List<URI> clusterAddresses = configuration.getURIs();

    if (clusterAddresses.isEmpty()) {
        logger.warn(ElasticSearchConfiguration.ELASTIC_SEARCH_URL_PROPERTY_NAME +
                " is not set.  Indexing will remain DISABLED.");
    }
    for (URI hostAddress : clusterAddresses) {
        int port = Optional.ofNullable(hostAddress.getPort()).orElse(9200);
        try {
            tc.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAddress.getHost()), port));
        } catch (UnknownHostException uhe){
            throw new ProvisionException("Invalid host" + hostAddress.getHost(), uhe);
        }
    }
    return tc;
}
 
Example #13
Source File: TestElasticSearchSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseConfiguration() {
  parameters.put(HOSTNAMES, "10.5.5.27");
  parameters.put(CLUSTER_NAME, "testing-cluster-name");
  parameters.put(INDEX_NAME, "testing-index-name");
  parameters.put(INDEX_TYPE, "testing-index-type");
  parameters.put(TTL, "10");

  fixture = new ElasticSearchSink();
  fixture.configure(new Context(parameters));

  InetSocketTransportAddress[] expected = { new InetSocketTransportAddress(
      "10.5.5.27", DEFAULT_PORT) };

  assertEquals("testing-cluster-name", fixture.getClusterName());
  assertEquals("testing-index-name", fixture.getIndexName());
  assertEquals("testing-index-type", fixture.getIndexType());
  assertEquals(TimeUnit.DAYS.toMillis(10), fixture.getTTLMs());
  assertArrayEquals(expected, fixture.getServerAddresses());
}
 
Example #14
Source File: ElasticSearchTransportClient.java    From ingestion with Apache License 2.0 6 votes vote down vote up
/**
 * Open client to elaticsearch cluster
 * 
 * @param clusterName
 */
private void openClient(String clusterName) {
  logger.info("Using ElasticSearch hostnames: {} ",
      Arrays.toString(serverAddresses));
  Settings settings = ImmutableSettings.settingsBuilder()
      .put("cluster.name", clusterName).build();

  TransportClient transportClient = new TransportClient(settings);
  for (InetSocketTransportAddress host : serverAddresses) {
    transportClient.addTransportAddress(host);
  }
  if (client != null) {
    client.close();
  }
  client = transportClient;
}
 
Example #15
Source File: ClientFactory.java    From storm-trident-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public TransportClient makeClient(Map conf) {

    String clusterHosts = (String)conf.get(HOSTS);
    String clusterName  = (String)conf.get(NAME);

    Preconditions.checkNotNull(clusterHosts,"no setting found for Transport Client, make sure that you set property " + HOSTS);

    TransportClient client = new TransportClient(buildSettings(clusterName));

    for(String hostAndPort : StringUtils.split(clusterHosts, HOST_SEPARATOR)) {
        int portPos = hostAndPort.indexOf(PORT_SEPARATOR);
        boolean noPortDefined = portPos == -1;
        int port =  ( noPortDefined ) ? DEFAULT_PORT : Integer.parseInt(hostAndPort.substring(portPos + 1, hostAndPort.length()));
        String host  = (noPortDefined) ? hostAndPort : hostAndPort.substring(0, portPos);
        client.addTransportAddress(new InetSocketTransportAddress(host, port));
    }
    return client;
}
 
Example #16
Source File: BaseTransportClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
protected boolean connect(Collection<InetSocketTransportAddress> addresses, boolean autodiscover) {
    logger.info("trying to connect to {}", addresses);
    client.addTransportAddresses(addresses);
    if (client.connectedNodes() != null) {
        List<DiscoveryNode> nodes = client.connectedNodes();
        if (!nodes.isEmpty()) {
            logger.info("connected to {}", nodes);
            if (autodiscover) {
                logger.info("trying to auto-discover all cluster nodes...");
                ClusterStateRequestBuilder clusterStateRequestBuilder =
                        new ClusterStateRequestBuilder(client, ClusterStateAction.INSTANCE);
                ClusterStateResponse clusterStateResponse = clusterStateRequestBuilder.execute().actionGet();
                DiscoveryNodes discoveryNodes = clusterStateResponse.getState().getNodes();
                client.addDiscoveryNodes(discoveryNodes);
                logger.info("after auto-discovery connected to {}", client.connectedNodes());
            }
            return true;
        }
        return false;
    }
    return false;
}
 
Example #17
Source File: ElasticIndexer.java    From bluima with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
@Override
public void initialize(UimaContext context)
		throws ResourceInitializationException {
	super.initialize(context);

	Settings settings = settingsBuilder().put("cluster.name", clusterName)
			.build();
	client = new TransportClient(settings)
			.addTransportAddress(new InetSocketTransportAddress(host, port));
	if (((TransportClient) client).connectedNodes().size() == 0) {
		throw new RuntimeException(
				"Could not connect to ES cluster, using host: " + host);
	}
	bulkRequest = client.prepareBulk();
}
 
Example #18
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private Client initTransportClient() throws UnknownHostException {
    TransportClient client = null;
    try {
        Settings settings = Settings.builder()
                                    .put("cluster.name", "docker-node")
                                    .put("client.transport.sniff", false)
                                    .build();

        client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress
            .getByName(host), 9300));
    } catch (UnknownHostException e) {
        logger.error("create client error", e);
        throw e;
    }
    return client;
}
 
Example #19
Source File: ElasticsearchContainer.java    From logstash with Apache License 2.0 6 votes vote down vote up
public Client createClient() {
    final AtomicReference<Client> elasticsearchClient = new AtomicReference<>();
    await().atMost(30, TimeUnit.SECONDS).pollDelay(1, TimeUnit.SECONDS).until(() -> {
        Client c = new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", elasticsearchClusterName).build()).addTransportAddress(new InetSocketTransportAddress(getIpAddress(), 9300));
        try {
            c.admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet();
        } catch (ElasticsearchException e) {
            c.close();
            return false;
        }
        elasticsearchClient.set(c);
        return true;
    });
    assertEquals(elasticsearchClusterName, elasticsearchClient.get().admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet().getClusterName());
    return elasticsearchClient.get();
}
 
Example #20
Source File: XmlPluginTest.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthResponse() throws Exception {
    InetSocketTransportAddress httpAddress = findHttpAddress(client("1"));
    if (httpAddress == null) {
        throw new IllegalArgumentException("no HTTP address found");
    }
    URL base = new URL("http://" + httpAddress.getHost() + ":" + httpAddress.getPort());
    URL url = new URL(base, "/_cluster/health?xml");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    if ((line = reader.readLine()) != null) {
        assertTrue(line.startsWith("<root xmlns=\"http://elasticsearch.org/ns/1.0/\">"));
        assertTrue(line.endsWith("</root>"));
    }
    reader.close();
}
 
Example #21
Source File: XmlPluginTest.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrettyHealthResponse() throws Exception {
    InetSocketTransportAddress httpAddress = findHttpAddress(client("1"));
    if (httpAddress == null) {
        throw new IllegalArgumentException("no HTTP address found");
    }
    URL base = new URL("http://" + httpAddress.getHost() + ":" + httpAddress.getPort());
    URL url = new URL(base, "/_cluster/health?xml&pretty");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    // check only first line
    if ((line = reader.readLine()) != null) {
        assertTrue(line.startsWith("<root xmlns=\"http://elasticsearch.org/ns/1.0/\">"));
    }
    reader.close();
}
 
Example #22
Source File: ElasticsearchSinkITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected ElasticsearchSinkBase<Tuple2<Integer, String>, Client> createElasticsearchSinkForNode(
		int bulkFlushMaxActions,
		String clusterName,
		ElasticsearchSinkFunction<Tuple2<Integer, String>> elasticsearchSinkFunction,
		String ipAddress) throws Exception {

	List<TransportAddress> transports = new ArrayList<>();
	transports.add(new InetSocketTransportAddress(InetAddress.getByName(ipAddress), 9300));

	return new ElasticsearchSink<>(
		Collections.unmodifiableMap(createUserConfig(bulkFlushMaxActions, clusterName)),
		transports,
		elasticsearchSinkFunction);
}
 
Example #23
Source File: ElasticsearchUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to convert a {@link List} of {@link InetSocketAddress} to Elasticsearch {@link TransportAddress}.
 *
 * @param inetSocketAddresses The list of {@link InetSocketAddress} to convert.
 */
public static List<TransportAddress> convertInetSocketAddresses(List<InetSocketAddress> inetSocketAddresses) {
	if (inetSocketAddresses == null) {
		return null;
	} else {
		List<TransportAddress> converted;
		converted = new ArrayList<>(inetSocketAddresses.size());
		for (InetSocketAddress address : inetSocketAddresses) {
			converted.add(new InetSocketTransportAddress(address));
		}
		return converted;
	}
}
 
Example #24
Source File: ESBaseBulkAdapter.java    From opensoc-streaming with Apache License 2.0 5 votes vote down vote up
@Override
public boolean initializeConnection(String ip, int port,
		String cluster_name, String index_name, String document_name,
		int bulk_size, String date_format) throws Exception {

	bulk_set = new HashBag();

	_LOG.trace("[OpenSOC] Initializing ESBulkAdapter...");

	try {
		_ip = ip;
		_port = port;
		_cluster_name = cluster_name;
		_index_name = index_name;
		_document_name = document_name;
		_bulk_size = bulk_size;

		_LOG.trace("[OpenSOC] Bulk indexing is set to: " + _bulk_size);

		settings = ImmutableSettings.settingsBuilder()
				.put("cluster.name", _cluster_name).build();
		client = new TransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress(_ip,
						_port));

		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}
 
Example #25
Source File: ElasticsearchUtil.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public Client getEsClient(Map<String, Integer> esAddresses,Map<String, String> esConfig){
    Settings settings = Settings.builder().put(esConfig).put("client.transport.sniff", true).build();
    TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (Map.Entry<String, Integer> esIpAndPort: esAddresses.entrySet()) {
        transportClient.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(esIpAndPort.getKey(), esIpAndPort.getValue())));
    }
    List<DiscoveryNode> discoveryNodes = transportClient.connectedNodes();
    if (discoveryNodes.isEmpty()) {
        throw new RuntimeException("Cannot connect to any elasticsearch nodes");
    }
   return transportClient;
}
 
Example #26
Source File: ESStorageEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
@Override
public void initialise() throws StorageEngineException {
	if (config.isUseNodeClient()) {
		Node node = new NodeBuilder().client(true).clusterName(config.getClusterName()).build();
		client = node.client();
	} else {
		TransportAddress[] serverAddresses = config.getServers().stream()
				.map(HostAndPort::fromString)
				.map(hp -> new InetSocketTransportAddress( new InetSocketAddress(hp.getHostText(), hp.getPortOrDefault(DEFAULT_PORT))))
				.toArray(size -> new TransportAddress[size]);
		client = TransportClient.builder().build().addTransportAddresses(serverAddresses);
	}
}
 
Example #27
Source File: Elasticsearch5Sink.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Override
public boolean open(long partitionId, long version)
        throws Exception
{
    TransportClient client = clientFactory.createClient();
    for (String ip : config.getHosts().split(",")) {
        client.addTransportAddress(new InetSocketTransportAddress(
                InetAddress.getByName(ip.split(":")[0]), Integer.valueOf(ip.split(":")[1])));
    }
    this.client = client;
    this.bulkBuilder = client.prepareBulk();
    return true;
}
 
Example #28
Source File: MaprElasticSearchServiceBuilder.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the {@link MaprElasticSearchService} according to the specified properties.
 *
 * @return instence of {@link MaprElasticSearchService}, which can be started via {@link MaprElasticSearchService#start()}.
 * @throws IllegalStateException in case when some of the required properties are missed.
 */
public MaprElasticSearchService build() {

    ensureFieldNonNull("port", this.port);
    ensureFieldNonNull("hostname", this.inetAddress);
    ensureFieldNonNull("indexName", this.indexName);
    ensureFieldNonNull("typeName", this.typeName);
    ensureFieldNonNull("changelog", this.changelog);
    ensureFieldNonNull("fields", this.fields);

    return () -> {

        // Create ES Client
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(inetAddress, port));

        // Create CDC Listener
        ChangelogListener listener = ChangelogListenerImpl.forChangelog(changelog);

        // Set 'onInsert' callback
        listener.onInsert(new SaveIndexCDCCallback(client));

        // Set 'onUpdate' callback
        listener.onUpdate(new SaveIndexCDCCallback(client));

        // Define and set 'onDelete' callback
        listener.onDelete((id) -> client.prepareDelete(indexName, typeName, id).get());

        listener.listen();

    };
}
 
Example #29
Source File: ElasticsearchClientManager.java    From streams with Apache License 2.0 5 votes vote down vote up
private synchronized void start() {

    try {
      // We are currently using lazy loading to start the elasticsearch cluster, however.
      LOGGER.info("Creating a new TransportClient: {}", this.config.getHosts());

      Settings settings = Settings.settingsBuilder()
          .put("cluster.name", this.config.getClusterName())
          .put("client.transport.ping_timeout", "90s")
          .put("client.transport.nodes_sampler_interval", "60s")
          .build();

      // Create the client
      client = TransportClient.builder().settings(settings).build();
      for (String h : config.getHosts()) {
        LOGGER.info("Adding Host: {}", h);
        InetAddress address;

        if ( InetAddresses.isInetAddress(h)) {
          LOGGER.info("{} is an IP address", h);
          address = InetAddresses.forString(h);
        } else {
          LOGGER.info("{} is a hostname", h);
          address = InetAddress.getByName(h);
        }
        client.addTransportAddress(
            new InetSocketTransportAddress(
                address,
                config.getPort().intValue()));
      }
    } catch (Exception ex) {
      LOGGER.error("Could not Create elasticsearch Transport Client: {}", ex);
    }

  }
 
Example #30
Source File: NodeTestUtils.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
protected void findNodeAddress() {
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
    NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    Object obj = response.iterator().next().getTransport().getAddress()
            .publishAddress();
    if (obj instanceof InetSocketTransportAddress) {
        InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
        host = address.address().getHostName();
        port = address.address().getPort();
    }
}