Skip to content

Commit 1d0b034

Browse files
committed
es 7.17.24 support
1 parent be52264 commit 1d0b034

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jdk:
44
- openjdk8
55

66
before_install:
7-
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.23-amd64.deb && sudo dpkg -i --force-confnew elasticsearch-7.17.23-amd64.deb
7+
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.24-amd64.deb && sudo dpkg -i --force-confnew elasticsearch-7.17.24-amd64.deb
88
- sudo cp ./src/test/resources/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
99
- sudo cat /etc/elasticsearch/elasticsearch.yml
1010
- 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>7.17.23.0</version>
6+
<version>7.17.24.0</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>
@@ -44,7 +44,7 @@
4444
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4545
<runSuite>**/MainTestSuite.class</runSuite>
4646
<elasticsearch.plugin.name>sql</elasticsearch.plugin.name>
47-
<elasticsearch.version>7.17.23</elasticsearch.version>
47+
<elasticsearch.version>7.17.24</elasticsearch.version>
4848
<elasticsearch.plugin.classname>org.elasticsearch.plugin.nlpcn.SqlPlug</elasticsearch.plugin.classname>
4949
<druid.version>1.2.15</druid.version>
5050
<guava.version>30.1.1-jre</guava.version>

src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ protected ElasticJoinExecutor(JoinRequestBuilder requestBuilder) {
4949
&& (secondTableReturnedField == null || secondTableReturnedField.size() == 0);
5050
}
5151

52-
public void sendResponse(RestChannel channel){
53-
try {
54-
XContentBuilder builder = ElasticUtils.hitsAsXContentBuilder(results,metaResults);
55-
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, builder);
56-
channel.sendResponse(bytesRestResponse);
57-
} catch (IOException e) {
58-
e.printStackTrace();
59-
}
52+
public void sendResponse(RestChannel channel) throws IOException {
53+
XContentBuilder builder = ElasticUtils.hitsAsXContentBuilder(results, metaResults);
54+
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, builder);
55+
channel.sendResponse(bytesRestResponse);
6056
}
6157

6258
@Override

src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.logging.log4j.LogManager;
44
import org.apache.logging.log4j.Logger;
55
import org.elasticsearch.client.node.NodeClient;
6+
import org.elasticsearch.rest.RestChannel;
67
import org.elasticsearch.xcontent.XContentParser;
78
import org.elasticsearch.xcontent.XContentType;
89
import org.elasticsearch.plugin.nlpcn.executors.ActionRequestRestExecuterFactory;
@@ -12,18 +13,18 @@
1213
import org.elasticsearch.rest.RestRequest;
1314
import org.elasticsearch.rest.RestStatus;
1415
import org.nlpcn.es4sql.SearchDao;
15-
import org.nlpcn.es4sql.exception.SqlParseException;
1616
import org.nlpcn.es4sql.query.QueryAction;
1717

1818
import java.io.IOException;
19-
import java.sql.SQLFeatureNotSupportedException;
2019
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.HashMap;
2322
import java.util.HashSet;
2423
import java.util.List;
2524
import java.util.Map;
25+
import java.util.Optional;
2626
import java.util.Set;
27+
import java.util.concurrent.ExecutorService;
2728

2829
import static org.elasticsearch.rest.RestRequest.Method.GET;
2930
import static org.elasticsearch.rest.RestRequest.Method.POST;
@@ -55,27 +56,39 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
5556
// LOGGER.warn("Please use json format params, like: {\"sql\":\"SELECT * FROM test\"}");
5657
}
5758

58-
String sql = request.param("sql");
59+
String sql = Optional.ofNullable(request.param("sql")).orElseGet(() -> request.content().utf8ToString());
60+
boolean useThreadPool = request.paramAsBoolean("useThreadPool", false);
5961

60-
if (sql == null) {
61-
sql = request.content().utf8ToString();
62+
if (useThreadPool) {
63+
ExecutorService executor = client.threadPool().executor("nlpcn_sql");
64+
return channel -> executor.execute(() -> doSqlRequest(request, client, sql, channel));
6265
}
66+
return channel -> doSqlRequest(request, client, sql, channel);
67+
}
68+
69+
@Override
70+
protected Set<String> responseParams() {
71+
Set<String> responseParams = new HashSet<>(super.responseParams());
72+
responseParams.addAll(Arrays.asList("sql", "flat", "separator", "_score", "_type", "_id", "_scroll_id", "newLine", "format", "showHeader", "quote", "useThreadPool"));
73+
return Collections.unmodifiableSet(responseParams);
74+
}
75+
76+
private void doSqlRequest(RestRequest request, NodeClient client, String sql, RestChannel channel) {
6377
try {
6478
SearchDao searchDao = new SearchDao(client);
65-
QueryAction queryAction = null;
6679

67-
queryAction = searchDao.explain(sql);//zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
80+
//zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
81+
QueryAction queryAction = searchDao.explain(sql);
6882

6983
// TODO add unit tests to explain. (rest level?)
7084
if (request.path().endsWith("/explain")) {
7185
final String jsonExplanation = queryAction.explain().explain();
72-
return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, XContentType.JSON.mediaType(), jsonExplanation));
86+
channel.sendResponse(new BytesRestResponse(RestStatus.OK, XContentType.JSON.mediaType(), jsonExplanation));
7387
} else {
7488
Map<String, String> params = request.params();
7589

7690
//zhongshu-comment 生成一个负责用rest方式查询es的对象RestExecutor,返回的实现类是:ElasticDefaultRestExecutor
7791
RestExecutor restExecutor = ActionRequestRestExecuterFactory.createExecutor(params.get("format"));
78-
final QueryAction finalQueryAction = queryAction;
7992
//doing this hack because elasticsearch throws exception for un-consumed props
8093
Map<String, String> additionalParams = new HashMap<>();
8194
for (String paramName : responseParams()) {
@@ -86,19 +99,15 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
8699
//zhongshu-comment restExecutor.execute()方法里会调用es查询的相关rest api
87100
//zhongshu-comment restExecutor.execute()方法的第1、4个参数是框架传进来的参数,第2、3个参数是可以自己生成的参数,所以要多注重一点
88101
//zhongshu-comment 默认调用的是ElasticDefaultRestExecutor这个子类
89-
//todo 这是什么语法:搜索java8 -> lambda表达式:https://blog.csdn.net/ioriogami/article/details/12782141
90-
return channel -> restExecutor.execute(client, additionalParams, finalQueryAction, channel);
102+
restExecutor.execute(client, additionalParams, queryAction, channel);
103+
}
104+
} catch (Exception e) {
105+
try {
106+
channel.sendResponse(new BytesRestResponse(channel, e));
107+
} catch (Exception inner) {
108+
inner.addSuppressed(e);
109+
LOGGER.error("failed to send failure response", inner);
91110
}
92-
} catch (SqlParseException | SQLFeatureNotSupportedException e) {
93-
e.printStackTrace();
94111
}
95-
return null;
96-
}
97-
98-
@Override
99-
protected Set<String> responseParams() {
100-
Set<String> responseParams = new HashSet<>(super.responseParams());
101-
responseParams.addAll(Arrays.asList("sql", "flat", "separator", "_score", "_type", "_id", "_scroll_id", "newLine", "format", "showHeader", "quote"));
102-
return Collections.unmodifiableSet(responseParams);
103112
}
104113
}

src/main/java/org/elasticsearch/plugin/nlpcn/SqlPlug.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.plugins.Plugin;
1313
import org.elasticsearch.rest.RestController;
1414
import org.elasticsearch.rest.RestHandler;
15+
import org.elasticsearch.threadpool.ExecutorBuilder;
16+
import org.elasticsearch.threadpool.FixedExecutorBuilder;
1517

1618
import java.util.Collection;
1719
import java.util.Collections;
@@ -47,4 +49,9 @@ protected void configure() {
4749
}
4850
});
4951
}
52+
53+
@Override
54+
public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
55+
return Collections.singletonList(new FixedExecutorBuilder(settings, "nlpcn_sql", 10, 100, "thread_pool.nlpcn_sql"));
56+
}
5057
}

src/main/java/org/elasticsearch/plugin/nlpcn/executors/ElasticDefaultRestExecutor.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,9 @@ public String execute(Client client, Map<String, String> params, QueryAction que
107107

108108
}
109109

110-
private void sendDefaultResponse(SearchHits hits, RestChannel channel) {
111-
try {
112-
XContentBuilder builder = ElasticUtils.hitsAsXContentBuilder(hits, new MetaSearchResult());
113-
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, builder);
114-
channel.sendResponse(bytesRestResponse);
115-
} catch (IOException e) {
116-
e.printStackTrace();
117-
}
110+
private void sendDefaultResponse(SearchHits hits, RestChannel channel) throws IOException {
111+
XContentBuilder builder = ElasticUtils.hitsAsXContentBuilder(hits, new MetaSearchResult());
112+
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, builder);
113+
channel.sendResponse(bytesRestResponse);
118114
}
119115
}

0 commit comments

Comments
 (0)