private TransportClient createTransportClient(Config config) throws UnknownHostException { TransportClient transportClient; // Set TransportClient settings Settings.Builder settingsBuilder = Settings.builder(); if (config.hasPath(ElasticsearchWriterConfigurationKeys.ELASTICSEARCH_WRITER_SETTINGS)) { settingsBuilder.put(ConfigUtils.configToProperties(config, ElasticsearchWriterConfigurationKeys.ELASTICSEARCH_WRITER_SETTINGS)); } settingsBuilder.put("client.transport.ignore_cluster_name",true); settingsBuilder.put("client.transport.sniff", true); transportClient = new PreBuiltTransportClient(settingsBuilder.build()); this.hostAddresses.forEach(transportClient::addTransportAddress); return transportClient; } }
@Override public TransportClient createClient(Map<String, String> clientConfig) { Settings settings = Settings.builder().put(clientConfig) .put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME) .put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME) .build(); TransportClient transportClient = new PreBuiltTransportClient(settings); for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) { transportClient.addTransportAddress(transport); } // 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; }
@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; } }
@Inject public ElasticsearchClient(ElasticsearchTableDescriptionProvider descriptions, ElasticsearchConnectorConfig config) throws IOException { tableDescriptions = requireNonNull(descriptions, "description is null"); ElasticsearchConnectorConfig configuration = requireNonNull(config, "config is null"); requestTimeout = configuration.getRequestTimeout(); maxAttempts = configuration.getMaxRequestRetries(); maxRetryTime = configuration.getMaxRetryTime(); for (ElasticsearchTableDescription tableDescription : tableDescriptions.getAllTableDescriptions()) { if (!clients.containsKey(tableDescription.getClusterName())) { Settings settings = Settings.builder().put("cluster.name", tableDescription.getClusterName()).build(); TransportAddress address = new TransportAddress(InetAddress.getByName(tableDescription.getHost()), tableDescription.getPort()); TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(address); clients.put(tableDescription.getClusterName(), client); } } this.columnMetadataCache = CacheBuilder.newBuilder() .expireAfterWrite(30, MINUTES) .refreshAfterWrite(15, MINUTES) .maximumSize(500) .build(asyncReloading(CacheLoader.from(this::loadColumns), executor)); }
private void initClient() throws UnknownHostException { Settings.Builder settingsBuilder = Settings.builder(); settingsBuilder.put( Settings.Builder.EMPTY_SETTINGS ); meta.getSettingsMap().entrySet().stream().forEach( ( s ) -> settingsBuilder.put( s.getKey(), environmentSubstitute( s.getValue() ) ) ); PreBuiltTransportClient tClient = new PreBuiltTransportClient( settingsBuilder.build() ); for ( Server server : meta.getServers() ) { tClient.addTransportAddress( new TransportAddress( InetAddress.getByName( environmentSubstitute( server.getAddress() ) ), server.getPort() ) ); } client = tClient; /** With the upgrade to elasticsearch 6.3.0, removed the NodeBuilder, * which was removed from the elasticsearch 5.0 API, see: * https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_java_api_changes * .html#_nodebuilder_removed */ }
properties.forEach(settingBuilder::put); Settings settings = settingBuilder.build(); transportClient = new PreBuiltTransportClient(settings); String[] hostArray = configuration.getHosts().split(","); for (String host : hostArray) {
public ElasticsearchQueryBuilder(List<ElasticsearchColumnHandle> columnHandles, ElasticsearchConnectorConfig config, ElasticsearchSplit split) { requireNonNull(columnHandles, "columnHandles is null"); requireNonNull(config, "config is null"); requireNonNull(split, "split is null"); columns = columnHandles; tupleDomain = split.getTupleDomain(); index = split.getIndex(); shard = split.getShard(); type = split.getType(); Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .build(); try { InetAddress address = InetAddress.getByName(split.getSearchNode()); client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(address, split.getPort())); } catch (UnknownHostException e) { throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, format("Error connecting to search node (%s:%d)", split.getSearchNode(), split.getPort()), e); } scrollTimeout = config.getScrollTimeout(); scrollSize = config.getScrollSize(); }
transportClient = new PreBuiltTransportClient(settingsBuilder.build());
client = new PreBuiltTransportClient(settings.build()); for (String h : nodeList) { String[] nodes = h.split(":");
protected void init() { client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("localhost"), 9300));
.environmentSubstitute( s.getValue() ) ) ); try ( PreBuiltTransportClient client = new PreBuiltTransportClient( settingsBuilder.build() ) ) {
@Test public void transportClientClusterHealth() { try (ElasticsearchContainer container = new ElasticsearchContainer()) { container.start(); TransportAddress transportAddress = new TransportAddress(container.getTcpHost()); String expectedClusterName = "docker-cluster"; Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build(); try (TransportClient transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(transportAddress)) { ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get(); String clusterName = healths.getClusterName(); assertThat(clusterName, is(expectedClusterName)); } } }
/** * create a elasticsearch transport client (remote elasticsearch) * @param addresses an array of host:port addresses * @param clusterName */ public ElasticsearchClient(final String[] addresses, final String clusterName) { // create default settings and add cluster name Settings.Builder settings = Settings.builder() .put("cluster.routing.allocation.enable", "all") .put("cluster.routing.allocation.allow_rebalance", "always"); if (clusterName != null) settings.put("cluster.name", clusterName); // create a client TransportClient tc = new PreBuiltTransportClient(settings.build()); for (String address: addresses) { String a = address.trim(); int p = a.indexOf(':'); if (p >= 0) try { InetAddress i = InetAddress.getByName(a.substring(0, p)); int port = Integer.parseInt(a.substring(p + 1)); tc.addTransportAddress(new InetSocketTransportAddress(i, port)); } catch (UnknownHostException e) { Data.logger.warn("", e); } } this.elasticsearchClient = tc; }
TransportClient trClient = new PreBuiltTransportClient(sBuilder.build()); List<String> addresses = Arrays.asList(transportAddresses.split(",")); for (String tAddr : addresses) {
/** * A method to retrieve a PreBuiltTransportClient. * * @param settings Elasticsearch settings * * @return a PreBuiltTransportClient */ public PreBuiltTransportClient getPreBuiltTransportClient(Settings settings) { return new PreBuiltTransportClient(settings); }
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"); }
private static Client getClient(String hostname, int transportPort) { Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .build(); try { return new PreBuiltTransportClient(settings).addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(hostname), transportPort)); } catch (UnknownHostException e) { throw new RuntimeException(e); } }
private Client createTransportClient(DataContextProperties properties) { final Settings settings = Settings.builder().put().put("name", "MetaModel").put("cluster.name", getCluster(properties)).build(); final TransportClient client = new PreBuiltTransportClient(settings); try { client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(properties.getHostname()), properties.getPort())); } catch (UnknownHostException e) { logger.warn("no IP address for the host with name \"{}\" could be found.", properties.getHostname()); } return client; }
ElasticSearchIdAndVersionStream createElasticSearchIdAndVersionStream(ScrutineerCommandLineOptions options) { try { this.client = new PreBuiltTransportClient(new NodeFactory().createSettings(options)); addTransportClients(options); return new ElasticSearchIdAndVersionStream(new ElasticSearchDownloader(client, options.indexName, options.query, idAndVersionFactory), new ElasticSearchSorter(createSorter()), new IteratorFactory(idAndVersionFactory), SystemUtils.getJavaIoTmpDir().getAbsolutePath()); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void before() throws Throwable { embeddedElastic.start(); client = new PreBuiltTransportClient(Settings.builder().put(CLUSTER_NAME, CLUSTER_NAME_VALUE).build()) .addTransportAddress( new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort())); }