3
3
import org .apache .logging .log4j .LogManager ;
4
4
import org .apache .logging .log4j .Logger ;
5
5
import org .elasticsearch .client .internal .node .NodeClient ;
6
+ import org .elasticsearch .rest .RestChannel ;
6
7
import org .elasticsearch .rest .RestResponse ;
7
8
import org .elasticsearch .xcontent .XContentParseException ;
8
9
import org .elasticsearch .xcontent .XContentParser ;
13
14
import org .elasticsearch .rest .RestRequest ;
14
15
import org .elasticsearch .rest .RestStatus ;
15
16
import org .nlpcn .es4sql .SearchDao ;
16
- import org .nlpcn .es4sql .exception .SqlParseException ;
17
17
import org .nlpcn .es4sql .query .QueryAction ;
18
18
19
19
import java .io .IOException ;
20
- import java .sql .SQLFeatureNotSupportedException ;
21
20
import java .util .Arrays ;
22
21
import java .util .Collections ;
23
22
import java .util .HashMap ;
24
23
import java .util .HashSet ;
25
24
import java .util .List ;
26
25
import java .util .Map ;
26
+ import java .util .Optional ;
27
27
import java .util .Set ;
28
+ import java .util .concurrent .ExecutorService ;
28
29
29
30
import static org .elasticsearch .rest .RestRequest .Method .GET ;
30
31
import static org .elasticsearch .rest .RestRequest .Method .POST ;
@@ -56,27 +57,39 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
56
57
// LOGGER.warn("Please use json format params, like: {\"sql\":\"SELECT * FROM test\"}");
57
58
}
58
59
59
- String sql = request .param ("sql" );
60
+ String sql = Optional .ofNullable (request .param ("sql" )).orElseGet (() -> request .content ().utf8ToString ());
61
+ boolean useThreadPool = request .paramAsBoolean ("useThreadPool" , false );
60
62
61
- if (sql == null ) {
62
- sql = request .content ().utf8ToString ();
63
+ if (useThreadPool ) {
64
+ ExecutorService executor = client .threadPool ().executor ("nlpcn_sql" );
65
+ return channel -> executor .execute (() -> doSqlRequest (request , client , sql , channel ));
63
66
}
67
+ return channel -> doSqlRequest (request , client , sql , channel );
68
+ }
69
+
70
+ @ Override
71
+ protected Set <String > responseParams () {
72
+ Set <String > responseParams = new HashSet <>(super .responseParams ());
73
+ responseParams .addAll (Arrays .asList ("sql" , "flat" , "separator" , "_score" , "_type" , "_id" , "_scroll_id" , "newLine" , "format" , "showHeader" , "quote" , "useThreadPool" ));
74
+ return Collections .unmodifiableSet (responseParams );
75
+ }
76
+
77
+ private void doSqlRequest (RestRequest request , NodeClient client , String sql , RestChannel channel ) {
64
78
try {
65
79
SearchDao searchDao = new SearchDao (client );
66
- QueryAction queryAction = null ;
67
80
68
- queryAction = searchDao .explain (sql );//zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
81
+ //zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
82
+ QueryAction queryAction = searchDao .explain (sql );
69
83
70
84
// TODO add unit tests to explain. (rest level?)
71
85
if (request .path ().endsWith ("/explain" )) {
72
86
final String jsonExplanation = queryAction .explain ().explain ();
73
- return channel -> channel .sendResponse (new RestResponse (RestStatus .OK , XContentType .JSON .mediaType (), jsonExplanation ));
87
+ channel .sendResponse (new RestResponse (RestStatus .OK , XContentType .JSON .mediaType (), jsonExplanation ));
74
88
} else {
75
89
Map <String , String > params = request .params ();
76
90
77
91
//zhongshu-comment 生成一个负责用rest方式查询es的对象RestExecutor,返回的实现类是:ElasticDefaultRestExecutor
78
92
RestExecutor restExecutor = ActionRequestRestExecuterFactory .createExecutor (params .get ("format" ));
79
- final QueryAction finalQueryAction = queryAction ;
80
93
//doing this hack because elasticsearch throws exception for un-consumed props
81
94
Map <String , String > additionalParams = new HashMap <>();
82
95
for (String paramName : responseParams ()) {
@@ -87,19 +100,15 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
87
100
//zhongshu-comment restExecutor.execute()方法里会调用es查询的相关rest api
88
101
//zhongshu-comment restExecutor.execute()方法的第1、4个参数是框架传进来的参数,第2、3个参数是可以自己生成的参数,所以要多注重一点
89
102
//zhongshu-comment 默认调用的是ElasticDefaultRestExecutor这个子类
90
- //todo 这是什么语法:搜索java8 -> lambda表达式:https://blog.csdn.net/ioriogami/article/details/12782141
91
- return channel -> restExecutor .execute (client , additionalParams , finalQueryAction , channel );
103
+ restExecutor .execute (client , additionalParams , queryAction , channel );
104
+ }
105
+ } catch (Exception e ) {
106
+ try {
107
+ channel .sendResponse (new RestResponse (channel , e ));
108
+ } catch (Exception inner ) {
109
+ inner .addSuppressed (e );
110
+ LOGGER .error ("failed to send failure response" , inner );
92
111
}
93
- } catch (SqlParseException | SQLFeatureNotSupportedException e ) {
94
- e .printStackTrace ();
95
112
}
96
- return null ;
97
- }
98
-
99
- @ Override
100
- protected Set <String > responseParams () {
101
- Set <String > responseParams = new HashSet <>(super .responseParams ());
102
- responseParams .addAll (Arrays .asList ("sql" , "flat" , "separator" , "_score" , "_type" , "_id" , "_scroll_id" , "newLine" , "format" , "showHeader" , "quote" ));
103
- return Collections .unmodifiableSet (responseParams );
104
113
}
105
114
}
0 commit comments