Skip to content

Commit 5d370f1

Browse files
committed
es 6.4.1 support
1 parent 84071e7 commit 5d370f1

7 files changed

+71
-91
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ before_install:
1717
# update to java 8
1818
- sudo update-java-alternatives -s java-8-oracle
1919
- export JAVA_HOME=/usr/lib/jvm/java-8-oracle
20-
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.deb && sudo dpkg -i --force-confnew elasticsearch-6.4.0.deb
20+
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.deb && sudo dpkg -i --force-confnew elasticsearch-6.4.1.deb
2121
- sudo cp ./src/test/resources/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
2222
- sudo cat /etc/elasticsearch/elasticsearch.yml
2323
- sudo java -version

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.nlpcn</groupId>
55
<artifactId>elasticsearch-sql</artifactId>
6-
<version>6.4.0.1</version>
6+
<version>6.4.1.0</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>
@@ -45,7 +45,7 @@
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4646
<runSuite>**/MainTestSuite.class</runSuite>
4747
<elasticsearch.plugin.name>sql</elasticsearch.plugin.name>
48-
<elasticsearch.version>6.4.0</elasticsearch.version>
48+
<elasticsearch.version>6.4.1</elasticsearch.version>
4949
<elasticsearch.plugin.classname>org.elasticsearch.plugin.nlpcn.SqlPlug</elasticsearch.plugin.classname>
5050
</properties>
5151

src/main/java/com/alibaba/druid/pool/ElasticSearchConnection.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,30 @@
11
package com.alibaba.druid.pool;
22

33
import org.elasticsearch.client.Client;
4-
import org.elasticsearch.client.transport.TransportClient;
5-
import org.elasticsearch.common.settings.Settings;
6-
import org.elasticsearch.common.transport.TransportAddress;
7-
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
84

95
import java.io.InputStream;
106
import java.io.Reader;
117
import java.math.BigDecimal;
12-
import java.net.InetAddress;
138
import java.net.URL;
14-
import java.net.UnknownHostException;
159
import java.sql.*;
16-
import java.sql.Date;
17-
import java.util.*;
10+
import java.util.Calendar;
11+
import java.util.Map;
12+
import java.util.Properties;
1813
import java.util.concurrent.Executor;
1914

2015
/**
2116
* Created by allwefantasy on 8/30/16.
2217
*/
2318
public class ElasticSearchConnection implements Connection {
2419

25-
private Client client;
26-
//关闭标识
27-
private boolean closeStatus = true;
28-
29-
public ElasticSearchConnection(String jdbcUrl, Properties info) {
20+
private final Client client;
3021

31-
Settings.Builder builder = Settings.builder();
32-
info.forEach((k, v) -> builder.put(k.toString(), v.toString()));
33-
Settings settings = builder.build();
34-
try {
35-
TransportClient transportClient = new PreBuiltXPackTransportClient(settings);
36-
37-
String hostAndPortArrayStr = jdbcUrl.split("/")[2];
38-
String[] hostAndPortArray = hostAndPortArrayStr.split(",");
22+
// 关闭标识
23+
private boolean closeStatus = true;
3924

40-
for (String hostAndPort : hostAndPortArray) {
41-
String host = hostAndPort.split(":")[0];
42-
String port = hostAndPort.split(":")[1];
43-
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host), Integer.parseInt(port)));
44-
}
45-
client = transportClient;
46-
closeStatus = false;
47-
} catch (UnknownHostException e) {
48-
e.printStackTrace();
49-
}
25+
public ElasticSearchConnection(Client client) {
26+
this.client = client;
27+
this.closeStatus = false;
5028
}
5129

5230
public Client getClient() {
@@ -589,9 +567,7 @@ public void rollback() throws SQLException {
589567

590568
@Override
591569
public void close() throws SQLException {
592-
this.getClient().close();
593570
closeStatus = true;
594-
595571
}
596572

597573
@Override
@@ -631,7 +607,7 @@ public void setTransactionIsolation(int level) throws SQLException {
631607

632608
@Override
633609
public int getTransactionIsolation() throws SQLException {
634-
return 0;
610+
return Connection.TRANSACTION_NONE;
635611
}
636612

637613
@Override

src/main/java/com/alibaba/druid/pool/ElasticSearchDruidDataSource.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import com.alibaba.druid.util.*;
2525
import com.alibaba.druid.wall.WallFilter;
2626
import com.alibaba.druid.wall.WallProviderStatValue;
27+
import org.elasticsearch.client.Client;
28+
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.common.transport.TransportAddress;
30+
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
2731

2832

2933
import javax.management.JMException;
@@ -37,6 +41,8 @@
3741
import javax.sql.ConnectionEventListener;
3842
import javax.sql.PooledConnection;
3943

44+
import java.net.InetAddress;
45+
import java.net.UnknownHostException;
4046
import java.security.AccessController;
4147
import java.security.PrivilegedAction;
4248
import java.sql.Connection;
@@ -115,6 +121,9 @@ public class ElasticSearchDruidDataSource extends DruidDataSource {
115121

116122
private boolean logDifferentThread = true;
117123

124+
// elasticsearch client
125+
private volatile Client client;
126+
118127
public ElasticSearchDruidDataSource() {
119128
this(false);
120129
}
@@ -655,7 +664,31 @@ public Connection createPhysicalConnection() throws SQLException {
655664

656665
@Override
657666
public Connection createPhysicalConnection(String url, Properties info) throws SQLException {
658-
Connection conn = new ElasticSearchConnection(url, info);
667+
if (client == null) {
668+
synchronized (this) {
669+
if (client == null) {
670+
Settings.Builder builder = Settings.builder();
671+
info.forEach((k, v) -> builder.put(k.toString(), v.toString()));
672+
673+
String[] hostAndPortArray = url.split("/")[2].split(",");
674+
int length = hostAndPortArray.length;
675+
TransportAddress[] addresses = new TransportAddress[length];
676+
try {
677+
String[] hostAndPortArr;
678+
for (int i = 0; i < length; ++i) {
679+
hostAndPortArr = hostAndPortArray[i].split(":");
680+
addresses[i] = new TransportAddress(InetAddress.getByName(hostAndPortArr[0]), Integer.parseInt(hostAndPortArr[1]));
681+
}
682+
} catch (UnknownHostException e) {
683+
throw new SQLException(e);
684+
}
685+
686+
client = new PreBuiltXPackTransportClient(builder.build()).addTransportAddresses(addresses);
687+
}
688+
}
689+
}
690+
691+
Connection conn = new ElasticSearchConnection(client);
659692
createCount.incrementAndGet();
660693

661694
return conn;
@@ -1279,6 +1312,12 @@ public void close() {
12791312
}
12801313
}
12811314
poolingCount = 0;
1315+
1316+
// close elasticsearch client
1317+
if (this.client != null) {
1318+
this.client.close();
1319+
}
1320+
12821321
unregisterMbean();
12831322

12841323
enable = false;

src/main/java/com/alibaba/druid/pool/ElasticSearchDruidPooledPreparedStatement.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
*/
2323
public class ElasticSearchDruidPooledPreparedStatement extends DruidPooledPreparedStatement {
2424

25-
26-
Client client = null;
25+
private final Client client;
2726

2827
public ElasticSearchDruidPooledPreparedStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException {
2928
super(conn, holder);
@@ -41,8 +40,6 @@ public ResultSet executeQuery() throws SQLException {
4140

4241
conn.beforeExecute();
4342
try {
44-
45-
4643
ObjectResult extractor = getObjectResult(true, getSql(), false, false, true);
4744
List<String> headers = extractor.getHeaders();
4845
List<List<Object>> lines = extractor.getLines();

src/main/java/com/alibaba/druid/pool/ElasticSearchResultSet.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ public class ElasticSearchResultSet implements ResultSet {
2121
private List<Object> current = null;
2222
private List<String> headers = null;
2323

24-
2524
private ResultSetMetaData metaData;
2625

2726
public ElasticSearchResultSet(Statement statement, final List<String> headers, final List<List<Object>> lines) {
2827
this.rows = lines;
2928
this.headers = headers;
3029
metaData = new ElasticSearchResultSetMetaDataBase(headers);
31-
3230
}
3331

3432
@Override
@@ -63,12 +61,12 @@ public boolean wasNull() throws SQLException {
6361

6462
@Override
6563
public String getString(int columnIndex) throws SQLException {
66-
return (String) current.get(columnIndex);
64+
return (String) current.get(columnIndex - 1);
6765
}
6866

6967
@Override
7068
public boolean getBoolean(int columnIndex) throws SQLException {
71-
return (Boolean) current.get(columnIndex);
69+
return (Boolean) current.get(columnIndex - 1);
7270
}
7371

7472
@Override
@@ -78,27 +76,27 @@ public byte getByte(int columnIndex) throws SQLException {
7876

7977
@Override
8078
public short getShort(int columnIndex) throws SQLException {
81-
return ((Short) current.get(columnIndex));
79+
return ((Short) current.get(columnIndex - 1));
8280
}
8381

8482
@Override
8583
public int getInt(int columnIndex) throws SQLException {
86-
return ((Integer) current.get(columnIndex));
84+
return ((Integer) current.get(columnIndex - 1));
8785
}
8886

8987
@Override
9088
public long getLong(int columnIndex) throws SQLException {
91-
return (Long) current.get(columnIndex);
89+
return (Long) current.get(columnIndex - 1);
9290
}
9391

9492
@Override
9593
public float getFloat(int columnIndex) throws SQLException {
96-
return (Float) current.get(columnIndex);
94+
return (Float) current.get(columnIndex - 1);
9795
}
9896

9997
@Override
10098
public double getDouble(int columnIndex) throws SQLException {
101-
return (Double) current.get(columnIndex);
99+
return (Double) current.get(columnIndex - 1);
102100
}
103101

104102
@Override
@@ -113,17 +111,17 @@ public byte[] getBytes(int columnIndex) throws SQLException {
113111

114112
@Override
115113
public Date getDate(int columnIndex) throws SQLException {
116-
return (Date) current.get(columnIndex);
114+
return (Date) current.get(columnIndex - 1);
117115
}
118116

119117
@Override
120118
public Time getTime(int columnIndex) throws SQLException {
121-
return (Time) current.get(columnIndex);
119+
return (Time) current.get(columnIndex - 1);
122120
}
123121

124122
@Override
125123
public Timestamp getTimestamp(int columnIndex) throws SQLException {
126-
return (Timestamp) current.get(columnIndex);
124+
return (Timestamp) current.get(columnIndex - 1);
127125
}
128126

129127
@Override
@@ -243,7 +241,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
243241

244242
@Override
245243
public Object getObject(int columnIndex) throws SQLException {
246-
return current.get(columnIndex);
244+
return current.get(columnIndex - 1);
247245

248246
}
249247

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,22 @@
11
package com.alibaba.druid.pool;
22

3-
import java.sql.SQLException;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
73
import com.alibaba.druid.util.jdbc.ResultSetMetaDataBase;
84

5+
import java.util.List;
6+
97
/**
108
* Created by allwefantasy on 8/31/16.
119
*/
1210
public class ElasticSearchResultSetMetaDataBase extends ResultSetMetaDataBase {
1311

14-
private final List<ColumnMetaData> columns = super.getColumns();
15-
1612
public ElasticSearchResultSetMetaDataBase(List<String> headers) {
17-
for(String column:headers){
18-
ColumnMetaData columnMetaData = new ColumnMetaData();
13+
ColumnMetaData columnMetaData;
14+
for (String column : headers) {
15+
columnMetaData = new ColumnMetaData();
1916
columnMetaData.setColumnLabel(column);
2017
columnMetaData.setColumnName(column);
21-
columns.add(columnMetaData);
22-
}
23-
}
24-
25-
@Override
26-
public List<ColumnMetaData> getColumns() {
27-
return columns;
28-
}
29-
30-
@Override
31-
public int findColumn(String columnName) throws SQLException {
32-
ColumnMetaData column;
33-
for (int i = 0; i < columns.size(); ++i) {
34-
column = columns.get(i);
35-
if (column.getColumnName().equals(columnName)) {
36-
return i + 1;
37-
}
18+
getColumns().add(columnMetaData);
3819
}
39-
40-
throw new SQLException("column '" + columnName + "' not found.");
41-
}
42-
43-
@Override
44-
public int getColumnCount() throws SQLException {
45-
return columns.size();
4620
}
4721

48-
@Override
49-
public ResultSetMetaDataBase.ColumnMetaData getColumn(int column) {
50-
return columns.get(column - 1);
51-
}
5222
}

0 commit comments

Comments
 (0)