Skip to content

Commit 79f7716

Browse files
amitnemaAmit Nema
authored andcommitted
RestLowLevelClientAppIT and elasticsearch JDBC module added.
1 parent 592fac3 commit 79f7716

File tree

8 files changed

+352
-110
lines changed

8 files changed

+352
-110
lines changed

elasticsearch-jdbc/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Elasticsearch JDBC
2+
This contains the general purpose solutions for elasticsearch SQL through elasticsearch-JDBC.
3+
4+
## Build
5+
mvn clean verify
6+
7+
## Prerequisite
8+
9+
> ⓘ
10+
*JDBC client requires a Platinum subscription. Please ensure that you have a Trial or Platinum license installed on your cluster before proceeding.
11+
(Reference: [jdbc-client](https://www.elastic.co/downloads/jdbc-client))*
12+
13+
### Activate the trial license
14+
* Activate the security by editing following lines inside elasticsearch.yml file.
15+
```yaml
16+
xpack.graph.enabled: true
17+
xpack.ml.enabled: true
18+
xpack.monitoring.enabled: true
19+
xpack.security.enabled: true
20+
xpack.watcher.enabled: true
21+
xpack.security.transport.ssl.enabled: true
22+
```
23+
24+
* Set the password [setup-passwords](https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-passwords.html)
25+
```bash
26+
bin/elasticsearch-setup-passwords interactive
27+
```
28+
29+
* Setup the elasticsearch password inside file kibana.yml (kibana config).
30+
```yaml
31+
elasticsearch.username: "username"
32+
elasticsearch.password: "password"
33+
```
34+
* Start a 30-day trial license.
35+
36+
The following example starts a 30-day trial license. The acknowledge parameter is required as you are initiating a license that will expire.
37+
```bash
38+
POST /_license/start_trial?acknowledge=true
39+
```
40+

elasticsearch-jdbc/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.apn.elasticsearch</groupId>
8+
<artifactId>elasticsearch-elixir</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>elasticsearch-jdbc</artifactId>
12+
<name>Elasticsearch Jdbc</name>
13+
<description>
14+
<![CDATA[This module contains the solution to access elastic search engine via elastic-search SQL JDBC client API.]]></description>
15+
16+
<repositories>
17+
<repository>
18+
<id>elastic.co</id>
19+
<url>https://artifacts.elastic.co/maven</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.elasticsearch.plugin</groupId>
26+
<artifactId>x-pack-sql-jdbc</artifactId>
27+
<version>${elasticsearch.version}</version>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.apn.elasticsearch.jdbc;
2+
3+
import org.elasticsearch.xpack.sql.jdbc.EsDataSource;
4+
5+
import java.sql.Connection;
6+
import java.sql.SQLException;
7+
import java.util.Properties;
8+
9+
/**
10+
* To get connection from Elasticsearch JDBC.
11+
*
12+
* @author Amit Nema
13+
*/
14+
public class JDBCClient {
15+
16+
private final String host;
17+
private final int port;
18+
private final String username;
19+
private final String password;
20+
21+
public JDBCClient(final String host, final int port, final String username, final String password) {
22+
this.host = host;
23+
this.port = port;
24+
this.username = username;
25+
this.password = password;
26+
}
27+
28+
public Connection getConnection() throws SQLException {
29+
String address = "jdbc:es://" + String.join(":", host, String.valueOf(port));
30+
Properties connectionProperties = new Properties();
31+
connectionProperties.setProperty("user", username);
32+
connectionProperties.setProperty("password", password);
33+
return getConnection(address, connectionProperties);
34+
}
35+
36+
public Connection getConnection(final String url, final Properties props) throws SQLException {
37+
EsDataSource dataSource = new EsDataSource();
38+
dataSource.setUrl(url);
39+
dataSource.setProperties(props);
40+
return dataSource.getConnection();
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.apn.elasticsearch.jdbc;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.sql.*;
8+
9+
import static org.junit.Assert.assertNotNull;
10+
11+
/**
12+
* Test {@link JDBCClient} class.
13+
* This test required a elasticsearch cluster to be setup locally.
14+
*
15+
* @Amit Nema
16+
*/
17+
public class JDBCClientTest {
18+
19+
private JDBCClient jdbcClient;
20+
21+
@Before
22+
public void setUp() {
23+
jdbcClient = new JDBCClient("localhost", 9200, "elastic", "elastic");
24+
}
25+
26+
@After
27+
public void tearDown() {
28+
jdbcClient =null;
29+
}
30+
31+
@Test
32+
public void executeQuery() throws SQLException {
33+
StringBuilder outTable = new StringBuilder();
34+
35+
try (Connection conn = jdbcClient.getConnection()) {
36+
try (Statement statement = conn.createStatement()) {
37+
try (ResultSet resultSet = statement.executeQuery("select * from product")) {
38+
assertNotNull(resultSet);
39+
//Get column Names
40+
ResultSetMetaData metadata = resultSet.getMetaData();
41+
int columnCount = metadata.getColumnCount();
42+
for (int i = 1; i <= columnCount; i++) {
43+
outTable.append(metadata.getColumnName(i));
44+
if (i != columnCount)
45+
outTable.append(", ");
46+
}
47+
outTable.append("\n");
48+
//Get Row Data
49+
while (resultSet.next()) {
50+
for (int i = 1; i <= columnCount; i++) {
51+
outTable.append(resultSet.getString(i));
52+
if (i != columnCount)
53+
outTable.append(", ");
54+
}
55+
outTable.append("\n");
56+
}
57+
assertNotNull(outTable);
58+
}
59+
}
60+
}
61+
}
62+
}
Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package org.apn.elasticsearch;
22

3-
import java.io.Closeable;
4-
import java.io.IOException;
5-
import java.text.MessageFormat;
6-
import java.util.Objects;
7-
83
import org.apache.commons.logging.Log;
94
import org.apache.commons.logging.LogFactory;
105
import org.apache.http.HttpHost;
@@ -17,52 +12,62 @@
1712
import org.elasticsearch.client.Response;
1813
import org.elasticsearch.client.RestClient;
1914

15+
import java.io.Closeable;
16+
import java.io.IOException;
17+
import java.text.MessageFormat;
18+
import java.util.Objects;
19+
2020
/**
2121
* The Class deals with Elasticsearch common usecases via
2222
* {@link RestClient}</code>.
23-
*
24-
* @author amit.nema
2523
*
24+
* @author amit.nema
2625
*/
2726
public class RestLowLevelClientApp implements Closeable {
2827

29-
private static final Log LOGGER = LogFactory.getLog(RestLowLevelClientApp.class);
30-
private final RestClient client;
31-
private final String index;
32-
private final String type;
28+
private static final Log LOGGER = LogFactory.getLog(RestLowLevelClientApp.class);
29+
private final RestClient client;
30+
31+
public RestLowLevelClientApp(final HttpHost... hosts) {
32+
client = RestClient.builder(hosts).build();
33+
}
3334

34-
public RestLowLevelClientApp(final HttpHost[] hosts, final String index, final String type) {
35-
this.index = index;
36-
this.type = type;
37-
client = RestClient.builder(hosts).build();
38-
}
35+
@Override
36+
public void close() throws IOException {
37+
if (Objects.nonNull(client))
38+
client.close();
39+
}
3940

40-
@Override
41-
public void close() throws IOException {
42-
if (Objects.nonNull(client))
43-
client.close();
44-
}
41+
public void findById(final String index, final String id) throws IOException {
42+
final Request req = new Request(HttpGet.METHOD_NAME, String.join("/", index, "_search"));
43+
req.addParameter("pretty", "false");
44+
req.setJsonEntity("{\"query\":{\"match\":{\"" + "_id" + "\":\"" + id + "\"}}}");
45+
final Response resp = client.performRequest(req);
46+
LOGGER.info("***** source *****\n" + resp.getRequestLine() + "\n***** ***** *****");
47+
final String responseBody = EntityUtils.toString(resp.getEntity());
48+
LOGGER.info(responseBody);
49+
}
4550

46-
public void findById(final String id) throws IOException {
47-
final Request req = new Request(HttpGet.METHOD_NAME, String.join("/", index, type, "_search"));
48-
req.addParameter("pretty", "false");
49-
req.setJsonEntity("{\"query\":{\"match\":{\"" + "_id" + "\":\"" + id + "\"}}}");
50-
final Response resp = client.performRequest(req);
51-
LOGGER.info("***** source *****\n" + resp.getRequestLine() + "\n***** ***** *****");
52-
final String responseBody = EntityUtils.toString(resp.getEntity());
53-
LOGGER.info(responseBody);
54-
}
51+
public void findByIdSQL(final String index, final String id) throws IOException {
52+
final Request req = new Request(HttpGet.METHOD_NAME, "_sql");
53+
req.addParameter("pretty", "false");
54+
req.setJsonEntity("{\"query\":\"select * from " + index + " where ProductID = " + id + "\"}");
55+
final Response resp = client.performRequest(req);
56+
LOGGER.info("***** source *****\n" + resp.getRequestLine() + "\n***** ***** *****");
57+
final String responseBody = EntityUtils.toString(resp.getEntity());
58+
LOGGER.info(responseBody);
59+
}
5560

56-
public boolean indexExists() throws IOException {
57-
final Request req = new Request(HttpHead.METHOD_NAME, MessageFormat.format("/{0}", index));
58-
final Response response = client.performRequest(req);
59-
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
60-
}
61+
public boolean indexExists(final String index) throws IOException {
62+
final Request req = new Request(HttpHead.METHOD_NAME, MessageFormat.format("/{0}", index));
63+
final Response response = client.performRequest(req);
64+
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
65+
}
6166

62-
public boolean refreshIndex() throws IOException {
63-
final Request req = new Request(HttpPost.METHOD_NAME, String.join("/", index, type, "_refresh"));
64-
req.setJsonEntity("{}");
65-
final Response response = client.performRequest(req);
66-
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
67-
}
67+
public boolean refreshIndex(final String index) throws IOException {
68+
final Request req = new Request(HttpPost.METHOD_NAME, String.join("/", index, "_refresh"));
69+
req.setJsonEntity("{}");
70+
final Response response = client.performRequest(req);
71+
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
72+
}
6873
}

0 commit comments

Comments
 (0)