org.elasticsearch.common.transport.TransportAddress Java Examples

The following examples show how to use org.elasticsearch.common.transport.TransportAddress. 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: 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 #2
Source File: PostgresNetty.java    From crate with Apache License 2.0 6 votes vote down vote up
private TransportAddress bindAddress(final InetAddress hostAddress) {
    PortsRange portsRange = new PortsRange(port);
    final AtomicReference<Exception> lastException = new AtomicReference<>();
    final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>();
    boolean success = portsRange.iterate(portNumber -> {
        try {
            Channel channel = bootstrap.bind(new InetSocketAddress(hostAddress, portNumber)).sync().channel();
            serverChannels.add(channel);
            boundSocket.set((InetSocketAddress) channel.localAddress());
        } catch (Exception e) {
            lastException.set(e);
            return false;
        }
        return true;
    });
    if (!success) {
        throw new BindPostgresException("Failed to bind to [" + port + "]", lastException.get());
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Bound psql to address {{}}", NetworkAddress.format(boundSocket.get()));
    }
    return new TransportAddress(boundSocket.get());
}
 
Example #3
Source File: TransportClientConfig.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private TransportAddress[] parseEsHost()
    throws Exception {
    TransportAddress[] transportAddresses = null;
    if (!elasticsearchHost.isEmpty()) {
        String[] hostIp = elasticsearchHost.split(",");
        transportAddresses = new TransportAddress[hostIp.length];

        for (int i = 0; i < hostIp.length; ++i) {
            String[] hostIpItem = hostIp[i].split(":");
            String ip = hostIpItem[0].trim();
            String port = hostIpItem[1].trim();
            transportAddresses[i] = new TransportAddress(InetAddress.getByName(ip), PORT);
        }
    }
    return transportAddresses;
}
 
Example #4
Source File: TcpTransport.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Sends back an error response to the caller via the given channel
 *
 * @param nodeVersion the caller node version
 * @param features    the caller features
 * @param channel     the channel to send the response to
 * @param error       the error to return
 * @param requestId   the request ID this response replies to
 * @param action      the action this response replies to
 */
public void sendErrorResponse(
        final Version nodeVersion,
        final Set<String> features,
        final TcpChannel channel,
        final Exception error,
        final long requestId,
        final String action) throws IOException {
    try (BytesStreamOutput stream = new BytesStreamOutput()) {
        stream.setVersion(nodeVersion);
        stream.setFeatures(features);
        RemoteTransportException tx = new RemoteTransportException(
            nodeName, new TransportAddress(channel.getLocalAddress()), action, error);
        ThreadContext.bwcWriteHeaders(stream);
        stream.writeException(tx);
        byte status = 0;
        status = TransportStatus.setResponse(status);
        status = TransportStatus.setError(status);
        final BytesReference bytes = stream.bytes();
        final BytesReference header = buildHeader(requestId, status, nodeVersion, bytes.length());
        CompositeBytesReference message = new CompositeBytesReference(header, bytes);
        SendListener onResponseSent = new SendListener(channel, null,
            () -> messageListener.onResponseSent(requestId, action, error), message.length());
        internalSendMessage(channel, message, onResponseSent);
    }
}
 
Example #5
Source File: XPackBaseDemo.java    From elasticsearch-full with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    /**
     * 如果es集群安装了x-pack插件则以此种方式连接集群
     * 1. java客户端的方式是以tcp协议在9300端口上进行通信
     * 2. http客户端的方式是以http协议在9200端口上进行通信
     */
    Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build();
    client = new PreBuiltXPackTransportClient(settings)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9200));
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("elastic", "changeme"));
    restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
            }).build();
}
 
Example #6
Source File: NodeStatsContextFieldResolverTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws UnknownHostException {
    final OsService osService = mock(OsService.class);
    final OsStats osStats = mock(OsStats.class);
    final MonitorService monitorService = mock(MonitorService.class);

    when(monitorService.osService()).thenReturn(osService);
    when(osService.stats()).thenReturn(osStats);
    DiscoveryNode discoveryNode = newNode("node_name", "node_id");

    postgresAddress = new TransportAddress(Inet4Address.getLocalHost(), 5432);
    resolver = new NodeStatsContextFieldResolver(
        () -> discoveryNode,
        monitorService,
        () -> null,
        () -> new HttpStats(20L, 30L),
        mock(ThreadPool.class),
        new ExtendedNodeInfo(),
        () -> new ConnectionStats(2L, 4L),
        () -> postgresAddress,
        () -> 12L,
        () -> 1L
    );
}
 
Example #7
Source File: ElasticsearchConnection.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public void start() throws Exception {
    logger.info("Starting ElasticSearch Client");
    Settings settings = Settings.builder()
            .put("cluster.name", config.getCluster())
            .put("client.transport.ignore_cluster_name", true)
            .build();

    TransportClient esClient = new CustomESTransportClient(settings);
    final int port = config.getPort() == null
        ? 9300
        : config.getPort();

    for(String host : config.getHosts()) {
        String[] tokenizedHosts = host.split(",");
        for(String tokenizedHost : tokenizedHosts) {
            esClient.addTransportAddress(new TransportAddress(InetAddress.getByName(tokenizedHost), port));
            logger.info("Added ElasticSearch Node : {}", host);
        }
    }
    client = esClient;
    logger.info("Started ElasticSearch Client");
}
 
Example #8
Source File: ElasticsearchStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #9
Source File: ElasticsearchClientTransport.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
@SuppressWarnings("squid:S2095")
public void setup() {
  final Settings.Builder settingsBuilder = Settings.builder();

  settingsBuilder.put("node.name", properties.getNodeName())
      .put("cluster.name", properties.getClusterName())
      .put("http.enabled", properties.isHttpEnabled());

  client = new PreBuiltTransportClient(settingsBuilder.build());
  try {
    client.addTransportAddress(new TransportAddress(InetAddress.getByName(properties.getHost()), properties.getPort()));
  } catch (UnknownHostException e) {
    log.error("Error connecting to the Elasticsearch cluster at {}:{}", properties.getHost(), properties.getPort(), e);
  }
}
 
Example #10
Source File: Elasticsearch2ApiCallBridge.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TransportClient createClient(Map<String, String> clientConfig) {
	Settings settings = Settings.settingsBuilder().put(clientConfig).build();

	TransportClient transportClient = TransportClient.builder().settings(settings).build();
	for (TransportAddress address : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
		transportClient.addTransportAddress(address);
	}

	// verify that we actually are connected to a cluster
	if (transportClient.connectedNodes().isEmpty()) {

		// close the transportClient here
		IOUtils.closeQuietly(transportClient);

		throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!");
	}

	if (LOG.isInfoEnabled()) {
		LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes());
	}

	return transportClient;
}
 
Example #11
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #12
Source File: SrvUnicastHostsProvider.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<TransportAddress> getSeedAddresses(HostsResolver hostsResolver) {
    if (query == null) {
        LOGGER.error("DNS query must not be null. Please set '{}'", DISCOVERY_SRV_QUERY);
        return Collections.emptyList();
    }
    try {
        List<DnsRecord> records = lookupRecords();
        LOGGER.trace("Building dynamic unicast discovery nodes...");
        if (records == null || records.size() == 0) {
            LOGGER.debug("No nodes found");
        } else {
            List<TransportAddress> transportAddresses = parseRecords(records);
            LOGGER.info("Using dynamic nodes {}", transportAddresses);
            return transportAddresses;
        }
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOGGER.error("DNS lookup exception:", e);
    }
    return Collections.emptyList();
}
 
Example #13
Source File: Elasticsearch6Module.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 TransportAddress(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 #14
Source File: NettyTransport.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private BoundTransportAddress createBoundTransportAddress(String name, Settings profileSettings, List<InetSocketAddress> boundAddresses) {
    String[] boundAddressesHostStrings = new String[boundAddresses.size()];
    TransportAddress[] transportBoundAddresses = new TransportAddress[boundAddresses.size()];
    for (int i = 0; i < boundAddresses.size(); i++) {
        InetSocketAddress boundAddress = boundAddresses.get(i);
        boundAddressesHostStrings[i] = boundAddress.getHostString();
        transportBoundAddresses[i] = new InetSocketTransportAddress(boundAddress);
    }

    final String[] publishHosts;
    if (DEFAULT_PROFILE.equals(name)) {
        publishHosts = settings.getAsArray("transport.netty.publish_host", settings.getAsArray("transport.publish_host", settings.getAsArray("transport.host", null)));
    } else {
        publishHosts = profileSettings.getAsArray("publish_host", boundAddressesHostStrings);
    }

    final InetAddress publishInetAddress;
    try {
        publishInetAddress = networkService.resolvePublishHostAddresses(publishHosts);
    } catch (Exception e) {
        throw new BindTransportException("Failed to resolve publish address", e);
    }

    final int publishPort = resolvePublishPort(name, settings, profileSettings, boundAddresses, publishInetAddress);
    final TransportAddress publishAddress = new InetSocketTransportAddress(new InetSocketAddress(publishInetAddress, publishPort));
    return new BoundTransportAddress(transportBoundAddresses, publishAddress);
}
 
Example #15
Source File: TcpTransportTest.java    From crate with Apache License 2.0 5 votes vote down vote up
/** Test ipv4 host with port works */
public void testParseV4WithPort() throws Exception {
    TransportAddress[] addresses = TcpTransport.parse("127.0.0.1:2345", 1234);
    assertEquals(1, addresses.length);

    assertEquals("127.0.0.1", addresses[0].getAddress());
    assertEquals(2345, addresses[0].getPort());
}
 
Example #16
Source File: MockTransportService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a node connected behavior that is used for the given delegate service.
 *
 * @return {@code true} if no other node connected behavior was registered for any of the addresses bound by delegate service.
 */
public boolean addNodeConnectedBehavior(TransportService transportService, StubbableConnectionManager.NodeConnectedBehavior behavior) {
    boolean noRegistered = true;
    for (TransportAddress transportAddress : extractTransportAddresses(transportService)) {
        noRegistered &= addNodeConnectedBehavior(transportAddress, behavior);
    }
    return noRegistered;
}
 
Example #17
Source File: AbstractAuditLog.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
public void logMissingPrivileges(String privilege, TransportRequest request, Task task) {
    final String action = null;

    if(!checkTransportFilter(Category.MISSING_PRIVILEGES, privilege, getUser(), request)) {
        return;
    }

    final TransportAddress remoteAddress = getRemoteAddress();
    final List<AuditMessage> msgs = RequestResolver.resolve(Category.MISSING_PRIVILEGES, getOrigin(), action, privilege, getUser(), null, null, remoteAddress, request, getThreadContextHeaders(), task, resolver, clusterService, settings, logRequestBody, resolveIndices, resolveBulkRequests, opendistrosecurityIndex, excludeSensitiveHeaders, null);

    for(AuditMessage msg: msgs) {
        save(msg);
    }
}
 
Example #18
Source File: NodePortExpression.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Integer portFromAddress(TransportAddress address) {
    Integer port = 0;
    if (address instanceof InetSocketTransportAddress) {
        port = ((InetSocketTransportAddress) address).address().getPort();
    }
    return port;
}
 
Example #19
Source File: TransportClientWrapper.java    From heroic with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public TransportClientWrapper(
    @JsonProperty("clusterName") String clusterName,
    @JsonProperty("seeds") List<String> seeds,
    @JsonProperty("sniff") Boolean sniff,
    @JsonProperty("nodeSamplerInterval") String nodeSamplerInterval
) {
    List<TransportAddress> seedAddresses =
        this.parseSeeds(ofNullable(seeds).orElse(DEFAULT_SEEDS));

    /*
    client.transport.sniff: true allows for dynamically adding of new hosts and removal of old
    ones. This works by first connecting to the seed nodes and using the internal cluster
    state API to discover available nodes.
    */
    final Settings settings = Settings.builder()
        .put("cluster.name", ofNullable(clusterName).orElse(DEFAULT_CLUSTER_NAME))
        .put("client.transport.sniff", ofNullable(sniff).orElse(DEFAULT_SNIFF))
        .put("client.transport.nodes_sampler_interval",
            ofNullable(nodeSamplerInterval).orElse(DEFAULT_NODE_SAMPLER_INTERVAL))
        .build();

    this.client = new PreBuiltTransportClient(settings);
    for (final TransportAddress seed : seedAddresses) {
        client.addTransportAddress(seed);
    }
}
 
Example #20
Source File: SrvUnicastHostsProviderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseRecords() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeShort(0); // priority
    buf.writeShort(0); // weight
    buf.writeShort(993); // port
    encodeName("localhost.", buf);
    DnsRecord record = new DefaultDnsRawRecord("_myprotocol._tcp.crate.io.", DnsRecordType.SRV, 30, buf);

    List<TransportAddress> addresses = srvUnicastHostsProvider.parseRecords(Collections.singletonList(record));
    assertThat(addresses.get(0).getAddress(), is("127.0.0.1"));
    assertThat(addresses.get(0).getPort(), is(993));
}
 
Example #21
Source File: TestESPipline.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws UnknownHostException {
    Settings settings = Settings.builder()
            .put("cluster.name", "docker-cluster").build();
    TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
    ResultItems resultItems = new ResultItems();
    resultItems.put("test", 1);
    new ElasticSearchPipline(client, "test", "_doc").process(resultItems);
}
 
Example #22
Source File: EmbeddedElasticsearchNode.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * Returns current address to connect to with HTTP client.
 *
 * @return hostname/port for HTTP connection
 */
public TransportAddress httpAddress() {
    Preconditions.checkState(isStarted, "node is not started");

    NodesInfoResponse response = client().admin().cluster().prepareNodesInfo()
        .execute().actionGet();
    if (response.getNodes().size() != 1) {
        throw new IllegalStateException("Expected single node but got "
            + response.getNodes().size());
    }
    NodeInfo node = response.getNodes().get(0);
    return node.getHttp().address().boundAddresses()[0];
}
 
Example #23
Source File: EsConfig.java    From jstorm with Apache License 2.0 5 votes vote down vote up
List<TransportAddress> getTransportAddresses() throws UnknownHostException {
  List<TransportAddress> transportAddresses = Lists.newArrayList();
  for (String node : nodes) {
    String[] hostAndPort = node.split(DELIMITER);
    Preconditions.checkArgument(hostAndPort.length == 2,
        "Incorrect node format");
    String host = hostAndPort[0];
    int port = Integer.parseInt(hostAndPort[1]);
    InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(
        InetAddress.getByName(host), port);
    transportAddresses.add(inetSocketTransportAddress);
  }
  return transportAddresses;
}
 
Example #24
Source File: AbstractAuditLog.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
public void logFailedLogin(String effectiveUser, boolean securityadmin, String initiatingUser, TransportRequest request, Task task) {
    final String action = null;

    if(!checkTransportFilter(Category.FAILED_LOGIN, action, effectiveUser, request)) {
        return;
    }

    final TransportAddress remoteAddress = getRemoteAddress();
    final List<AuditMessage> msgs = RequestResolver.resolve(Category.FAILED_LOGIN, getOrigin(), action, null, effectiveUser, securityadmin, initiatingUser, remoteAddress, request, getThreadContextHeaders(), task, resolver, clusterService, settings, logRequestBody, resolveIndices, resolveBulkRequests, opendistrosecurityIndex, excludeSensitiveHeaders, null);

    for(AuditMessage msg: msgs) {
        save(msg);
    }
}
 
Example #25
Source File: SSLTest.java    From deprecated-security-ssl with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransportClientSSLFail() throws Exception {
    thrown.expect(NoNodeAvailableException.class);

    final Settings settings = Settings.builder().put("opendistro_security.ssl.transport.enabled", true)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_HTTP_ENABLE_OPENSSL_IF_AVAILABLE, allowOpenSSL)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_TRANSPORT_ENABLE_OPENSSL_IF_AVAILABLE, allowOpenSSL)
            .put(SSLConfigConstants.OPENDISTRO_SECURITY_SSL_TRANSPORT_KEYSTORE_ALIAS, "node-0")
            .put("opendistro_security.ssl.transport.keystore_filepath", getAbsoluteFilePathFromClassPath("node-0-keystore.jks"))
            .put("opendistro_security.ssl.transport.truststore_filepath", getAbsoluteFilePathFromClassPath("truststore.jks"))
            .put("opendistro_security.ssl.transport.enforce_hostname_verification", false)
            .put("opendistro_security.ssl.transport.resolve_hostname", false).build();

    startES(settings);

    final Settings tcSettings = Settings.builder().put("cluster.name", clustername)
            .put("path.home", getAbsoluteFilePathFromClassPath("node-0-keystore.jks").getParent())
            .put("opendistro_security.ssl.transport.keystore_filepath", getAbsoluteFilePathFromClassPath("node-0-keystore.jks"))
            .put("opendistro_security.ssl.transport.truststore_filepath", getAbsoluteFilePathFromClassPath("truststore_fail.jks"))
            .put("opendistro_security.ssl.transport.enforce_hostname_verification", false)
            .put("opendistro_security.ssl.transport.resolve_hostname", false).build();

    try (TransportClient tc = new TransportClientImpl(tcSettings, asCollection(OpenDistroSecuritySSLPlugin.class))) {
        tc.addTransportAddress(new TransportAddress(new InetSocketAddress(nodeHost, nodePort)));
        Assert.assertEquals(3, tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());
    }
}
 
Example #26
Source File: NettyTransport.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public TransportAddress[] addressesFromString(String address, int perAddressLimit) throws Exception {
    return parse(address, settings.get("transport.profiles.default.port",
                          settings.get("transport.netty.port",
                          settings.get("transport.tcp.port",
                          DEFAULT_PORT_RANGE))), perAddressLimit);
}
 
Example #27
Source File: ElasticSearchConfig.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Bean
    public TransportClient transportClient() {

        Settings settings = Settings.builder()
                // 不允许自动刷新地址列表
                .put("client.transport.sniff", false)
                .put("client.transport.ignore_cluster_name", true)
                .build();

        // 初始化地址
        TransportAddress[] transportAddresses = new TransportAddress[esSqlAddress.length];
        for (int i = 0; i < esSqlAddress.length; i++) {
            String[] addressItems = esSqlAddress[i].split(":");
            try {
                transportAddresses[i] = new InetSocketTransportAddress(InetAddress.getByName(addressItems[0]),
                        Integer.valueOf(addressItems[1]));
            } catch (Exception e) {
//                throw new EsOperationException(e);
            }
        }

        PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);

        TransportClient client = preBuiltTransportClient
                .addTransportAddresses(transportAddresses);
        return client;
    }
 
Example #28
Source File: Elasticsearch6Sink.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 TransportAddress(InetAddress.getByName(ip.split(":")[0]),
                        Integer.parseInt(ip.split(":")[1])));
    }
    this.client = client;
    this.bulkBuilder = client.prepareBulk();
    return true;
}
 
Example #29
Source File: IngestAutodiscoverTest.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutodiscover() throws IOException {
    startNode("2");
    Settings.Builder settingsBuilder = Settings.builder()
            .put("cluster.name", getClusterName())
            .put("path.home", System.getProperty("path.home"))
            .put("autodiscover", true);
    int i = 0;
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
    NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    for (NodeInfo nodeInfo : response) {
        TransportAddress ta = nodeInfo.getTransport().getAddress().publishAddress();
        if (ta instanceof InetSocketTransportAddress) {
            InetSocketTransportAddress address = (InetSocketTransportAddress) ta;
            settingsBuilder.put("host." + i++, address.address().getHostName() + ":" + address.address().getPort());
        }
    }
    final IngestTransportClient ingest = ClientBuilder.builder()
            .put(settingsBuilder.build())
            .setMetric(new LongAdderIngestMetric())
            .toIngestTransportClient();
    try {
        ingest.newIndex("test");
    } finally {
        ingest.shutdown();
    }
    if (ingest.hasThrowable()) {
        logger.error("error", ingest.getThrowable());
    }
    assertFalse(ingest.hasThrowable());
}
 
Example #30
Source File: ESTest.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws UnknownHostException {
    Settings.Builder settingBuilder = Settings.builder();
    settingBuilder.put("cluster.name", TestConstant.clusterName);
    Settings settings = settingBuilder.build();
    transportClient = new PreBuiltTransportClient(settings);
    String[] hostArray = TestConstant.esHosts.split(",");
    for (String host : hostArray) {
        int i = host.indexOf(":");
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)),
            Integer.parseInt(host.substring(i + 1))));
    }
}