Java Code Examples for org.elasticsearch.transport.client.PreBuiltTransportClient#addTransportAddress()

The following examples show how to use org.elasticsearch.transport.client.PreBuiltTransportClient#addTransportAddress() . 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: ElasticSearchService.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
public static ElasticSearchService connect(String host, Integer port, String clusterName) {
    ElasticSearchSettings settings = ElasticSearchSettings.of(host, port, clusterName);

    try {
        // WARNING pbtc is never closed -> Close the transportClient as well...
        PreBuiltTransportClient pbtc = new PreBuiltTransportClient(
                Settings.builder().put("cluster.name", clusterName).build());
        TransportClient transportClient = pbtc
                .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

        log.info("Connected to ElasticSearch [host: {}, port: {}, cluster: {}] : {}", host, port, clusterName,
                transportClient.listedNodes().toString());

        return new ElasticSearchService(settings, transportClient);

    } catch (UnknownHostException ex) {
        log.error("Error while connecting to ElasticSearch [host: {}, port: {}, cluster: {}]", host, port,
                clusterName, ex);
        throw new ConnectionException("Error whilst connecting to ElasticSearch", ex);
    }
}
 
Example 2
Source File: DefaultMahutaTest.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
@Test
public void connectViaTransportClient() throws Exception {

    String host = ContainerUtils.getHost("elasticsearch");
    Integer port = ContainerUtils.getPort("elasticsearch");
    String clusterName = ContainerUtils.getConfig("elasticsearch", "cluster-name");
            
    PreBuiltTransportClient pbtc = new PreBuiltTransportClient(
            Settings.builder().put("cluster.name", clusterName).build());
    TransportClient transportClient = pbtc
            .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
    
    IndexingService esService = ElasticSearchService.connect(transportClient).withIndex("test");
    
    assertNotNull( esService.getIndexes());
}
 
Example 3
Source File: ESClient.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * init
 * 
 * @param esAddrs
 * @param clusterName
 */
private void init(String[] esAddrs, String clusterName) {

    Settings settings = Settings.EMPTY;

    if (!StringHelper.isEmpty(clusterName)) {
        settings = Settings.builder().put("cluster.name", clusterName).build();
    }

    client = new PreBuiltTransportClient(settings);

    for (String esAddr : esAddrs) {
        String[] ipport = esAddr.split(":");
        client.addTransportAddress(new InetSocketTransportAddress(
                new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
    }
}
 
Example 4
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 5
Source File: SharedElasticsearchResource.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doInitialize(ResourceSpecifier specifier, Map<String, Object> additionalParams)
    throws ResourceInitializationException {

  int esPort = ConfigUtils.stringToInteger(esPortString, 9300);

  // Use the transport client
  Settings.Builder settings = Settings.builder();
  settings.put("cluster.name", esCluster);

  client = new PreBuiltTransportClient(settings.build());
  client.addTransportAddress(
      new InetSocketTransportAddress(new InetSocketAddress(esHost, esPort)));

  return client != null;
}
 
Example 6
Source File: IndexderAppConfig.java    From elasticactors with Apache License 2.0 6 votes vote down vote up
@Bean(name = "indexingElasticsearchClient")
public Client createElasticsearchClient() {
    String[] elasticsearchHosts = environment.getRequiredProperty("actor.indexing.elasticsearch.hosts", String[].class);
    Integer elasticsearchPort = environment.getProperty("actor.indexing.elasticsearch.port", Integer.class, 9300);
    String elasticsearchClusterName = environment.getProperty("actor.indexing.elasticsearch.cluster.name", String.class, "elasticsearch");

    logger.info("Creating elasticsearch client with hosts <{}>", Arrays.toString(elasticsearchHosts));

    Settings settings = Settings.builder()
            .put("cluster.name", elasticsearchClusterName).build();

    client = new PreBuiltTransportClient(settings);

    for (String elasticsearchHost : elasticsearchHosts) {
        try {
            client.addTransportAddress(new TransportAddress(InetAddress.getByName(elasticsearchHost), elasticsearchPort));
        } catch (UnknownHostException e) {
            throw new BeanCreationException("Could not add elasticsearch host <" + elasticsearchHost + "> to client configuration. Aborting", e);
        }
    }

    return client;
}
 
Example 7
Source File: ElasticSearchBulk.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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
     */

  }
 
Example 8
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))));
    }
}
 
Example 9
Source File: ElasticsearchTransportClient.java    From Raigad with Apache License 2.0 5 votes vote down vote up
/**
 * Hostname and port to talk to will be same server for now optionally we might want the IP to poll.
 * NOTE: This class shouldn't be a singleton and this shouldn't be cached.
 * This will work only if Elasticsearch runs.
 */
private ElasticsearchTransportClient(InetAddress host, IConfiguration configuration) {
    logger.info("Initializing client connection to {}", host.toString());

    Map<String, String> transportClientSettings = new HashMap<>();
    transportClientSettings.put("cluster.name", configuration.getAppName());

    client = new PreBuiltTransportClient(Settings.builder().put(transportClientSettings).build());
    client.addTransportAddress(new InetSocketTransportAddress(host, configuration.getTransportTcpPort()));

    nodeStatsRequestBuilder = client.admin().cluster().prepareNodesStats(configuration.getEsNodeName()).all();
}
 
Example 10
Source File: ESTest.java    From canal 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))));
    }
}
 
Example 11
Source File: ES7xTest.java    From canal 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))));
    }
}