Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Bonsai ElasticSearch on Heroku from a Java app

I have a Java application that is using ElasticSearch API to connect to Bonsai on Heroku. After deploying my app to Heroku I've found that it will be impossible to connect to a traditional ElasticSearch address 127.0.0.1:9300 via TransportClient.

So I've changed TransportAddress to the one below after reading one of the answers here.

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("http://juniper-325345373.eu-west-1.bonsaisearch.net/"), 80)); 

However now I'm getting a java.net.UnknownHostException: http://juniper-325345373.eu-west-1.bonsaisearch.net/: Name or service not known

How should I correctly define the address in order to connect to Bonsai ElasticSearch?

like image 494
samba Avatar asked Nov 24 '25 07:11

samba


1 Answers

I found very userful link on bonsai's web site. https://docs.bonsai.io/article/278-java

1. Extract your account data from URI

e.g. https://a1b2c3d4e:[email protected]

String username = a1b2c3d4e;
String password = 5f6g7h8i9;

2. Extract host address

String host = somehost-1234567.region-x-y.bonsaisearch.net;
String port = 443;

more information could be found here: https://docs.bonsai.io/article/94-connecting-to-bonsai

3. Implement credentials provider

  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

4. Implement RestHighLevelClient to connect to bonsai

RestHighLevelClient restClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost(host,port,"https"))
           .setHttpClientConfigCallback(httpAsyncClientBuilder -> 
    httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                       .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())));`
like image 172
Denis_dev Avatar answered Nov 26 '25 22:11

Denis_dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!