33import org .apache .logging .log4j .LogManager ;
44import org .apache .logging .log4j .Logger ;
55import org .elasticsearch .client .internal .node .NodeClient ;
6+ import org .elasticsearch .rest .RestChannel ;
67import org .elasticsearch .rest .RestResponse ;
78import org .elasticsearch .xcontent .XContentParseException ;
89import org .elasticsearch .xcontent .XContentParser ;
1314import org .elasticsearch .rest .RestRequest ;
1415import org .elasticsearch .rest .RestStatus ;
1516import org .nlpcn .es4sql .SearchDao ;
16- import org .nlpcn .es4sql .exception .SqlParseException ;
1717import org .nlpcn .es4sql .query .QueryAction ;
1818
1919import java .io .IOException ;
20- import java .sql .SQLFeatureNotSupportedException ;
2120import java .util .Arrays ;
2221import java .util .Collections ;
2322import java .util .HashMap ;
2423import java .util .HashSet ;
2524import java .util .List ;
2625import java .util .Map ;
26+ import java .util .Optional ;
2727import java .util .Set ;
28+ import java .util .concurrent .ExecutorService ;
2829
2930import static org .elasticsearch .rest .RestRequest .Method .GET ;
3031import static org .elasticsearch .rest .RestRequest .Method .POST ;
@@ -56,27 +57,39 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
5657 // LOGGER.warn("Please use json format params, like: {\"sql\":\"SELECT * FROM test\"}");
5758 }
5859
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 );
6062
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 ));
6366 }
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 ) {
6478 try {
6579 SearchDao searchDao = new SearchDao (client );
66- QueryAction queryAction = null ;
6780
68- queryAction = searchDao .explain (sql );//zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
81+ //zhongshu-comment 语法解析,将sql字符串解析为一个Java查询对象
82+ QueryAction queryAction = searchDao .explain (sql );
6983
7084 // TODO add unit tests to explain. (rest level?)
7185 if (request .path ().endsWith ("/explain" )) {
7286 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 ));
7488 } else {
7589 Map <String , String > params = request .params ();
7690
7791 //zhongshu-comment 生成一个负责用rest方式查询es的对象RestExecutor,返回的实现类是:ElasticDefaultRestExecutor
7892 RestExecutor restExecutor = ActionRequestRestExecuterFactory .createExecutor (params .get ("format" ));
79- final QueryAction finalQueryAction = queryAction ;
8093 //doing this hack because elasticsearch throws exception for un-consumed props
8194 Map <String , String > additionalParams = new HashMap <>();
8295 for (String paramName : responseParams ()) {
@@ -87,19 +100,15 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
87100 //zhongshu-comment restExecutor.execute()方法里会调用es查询的相关rest api
88101 //zhongshu-comment restExecutor.execute()方法的第1、4个参数是框架传进来的参数,第2、3个参数是可以自己生成的参数,所以要多注重一点
89102 //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 );
92111 }
93- } catch (SqlParseException | SQLFeatureNotSupportedException e ) {
94- e .printStackTrace ();
95112 }
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 );
104113 }
105114}
0 commit comments