|
@ -1,5 +1,11 @@
|
|
|
package com.yihu.jw.elasticsearch;
|
|
|
|
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
|
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
|
|
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
|
|
import com.alibaba.druid.sql.parser.ParserException;
|
|
|
import com.alibaba.druid.sql.parser.SQLExprParser;
|
|
|
import com.alibaba.druid.sql.parser.Token;
|
|
|
import org.apache.http.HttpHost;
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
@ -7,11 +13,20 @@ import org.apache.http.client.CredentialsProvider;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
|
|
import org.elasticsearch.action.get.GetRequest;
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.nlpcn.es4sql.domain.Where;
|
|
|
import org.nlpcn.es4sql.exception.SqlParseException;
|
|
|
import org.nlpcn.es4sql.parse.ElasticSqlExprParser;
|
|
|
import org.nlpcn.es4sql.parse.SqlParser;
|
|
|
import org.nlpcn.es4sql.parse.WhereParser;
|
|
|
import org.nlpcn.es4sql.query.maker.QueryMaker;
|
|
|
|
|
|
/**
|
|
|
* Created by yeshijie on 2022/6/15.
|
|
@ -20,7 +35,7 @@ public class TestEs7 {
|
|
|
|
|
|
public static void main(String[] args) throws Exception{
|
|
|
|
|
|
String host = "172.26.0.56";
|
|
|
String host = "127.0.0.1";
|
|
|
String userName = "elastic";
|
|
|
String password = "elastic";
|
|
|
|
|
@ -29,6 +44,7 @@ public class TestEs7 {
|
|
|
for(int i=0;i<hosts.length;i++) {
|
|
|
httpHosts[i] = new HttpHost(hosts[i], 9200, "http");
|
|
|
}
|
|
|
createQueryBuilderBySql("select * from ");
|
|
|
//设置密码
|
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
|
|
@ -54,9 +70,45 @@ public class TestEs7 {
|
|
|
|
|
|
|
|
|
String index = "body_health_data";
|
|
|
GetIndexRequest request = new GetIndexRequest(index);
|
|
|
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
|
|
|
System.out.println(exists);
|
|
|
/*GetIndexRequest request = new GetIndexRequest(index);
|
|
|
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);*/
|
|
|
GetRequest getRequest = new GetRequest("body_health_data","1");
|
|
|
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
|
|
|
System.out.println(documentFields);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static BoolQueryBuilder createQueryBuilderBySql(String sql) {
|
|
|
BoolQueryBuilder boolQuery = null;
|
|
|
try {
|
|
|
SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql);
|
|
|
SqlParser sqlParser = new SqlParser();
|
|
|
MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery();
|
|
|
WhereParser whereParser = new WhereParser(sqlParser, query);
|
|
|
Where where = whereParser.findWhere();
|
|
|
if (where != null) {
|
|
|
boolQuery = QueryMaker.explan(where);
|
|
|
}
|
|
|
} catch (SqlParseException e) {
|
|
|
// logger.info("ReadES.createQueryBuilderByExpress-Exception,"+e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return boolQuery;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证sql
|
|
|
*
|
|
|
* @param sql sql查询语句
|
|
|
* @return and (a=1 and b=1) or (c=1 and d=1)
|
|
|
*/
|
|
|
private static SQLExpr toSqlExpr(String sql) {
|
|
|
SQLExprParser parser = new ElasticSqlExprParser(sql);
|
|
|
SQLExpr expr = parser.expr();
|
|
|
|
|
|
if (parser.getLexer().token() != Token.EOF) {
|
|
|
throw new ParserException("illegal sql expr : " + sql);
|
|
|
}
|
|
|
return expr;
|
|
|
}
|
|
|
}
|