How to fetch data using java client


(Ajit) #1

Hi,

I have to fetch index data using java client in java application. Can you suggest me which client should I use. That client should support user name and password because we are using security now. Previously I had used transport client for connectivity with elasticsearch with user name and password. But that time we had installed searchguard for security. Means search guard supports transport client.
Now in our case (Read only rest) which client will support.
I have attached searchguard code with transport client.

try {

			Settings settings = Settings.builder()
					.put("cluster.name", "searchguard_demo")
					
					.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMCERT_FILEPATH,"C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\spock.crtfull.pem")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMKEY_FILEPATH, "C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\spock.key.pem")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMTRUSTEDCAS_FILEPATH, "C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\root-ca.pem")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENFORCE_HOSTNAME_VERIFICATION, "false")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENABLED, "true")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_ENABLED, "true")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_PEMCERT_FILEPATH, "C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\spock.crtfull.pem")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_PEMKEY_FILEPATH, "C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\spock.key.pem")
					.put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_PEMTRUSTEDCAS_FILEPATH, "C:\\Users\\c-ajitb\\Desktop\\SearchguardSpockFiles\\root-ca.pem")
					
					.build();
			System.out.println("Build Setting Obj Successfull");
			TransportClient transclient = new PreBuiltTransportClient(settings,SearchGuardPlugin.class).addTransportAddress(new TransportAddress(InetAddress.getByName("172.21.153.176"), 9300));
					
			System.out.println("Connected");
			
			transclient.threadPool().getThreadContext().putHeader("Authorization", "Basic "+encodeBasicHeader("testuser", "testuser"));
			
			GetResponse getResponse = transclient.prepareGet("cap_log", "cap", "*").get();
			System.out.println("Response="+getResponse.getSource());
			transclient.close();
		
	}catch (Exception e) {
		e.printStackTrace();
	}

(Simone Scarduzio) #2

You are using the transport client, this is deprecated, use the high level rest client instead as shown here.


(Ajit) #3

Can you please provide java client code to connect to elasticsearch with username and password and index name combination. From java code I have to connect to ES and want to fetch data. But security does not allow me to connect. If possible please specify jar file names.


(Simone Scarduzio) #4

@ajit let’s we move this discussion to the above mentioned other thread because @Akhilesh is asking exactly the same stuff.


(Simone Scarduzio) #5