Elasticsearch 的查询语言 (DSL) 语法很复杂,查询的功能 (filter/query/match/agg/geo) 各种各样,不管是通过 restful API 还是通过 java 的 API 进行操作,都非常不方便。而 Elasticsearch-SQL 插件可以使用传统 SQL 语法查询 Elasticsearch,使用起来非常不错。
Elasticsearch-SQL 项目地址:https://github.com/NLPchina/elasticsearch-sql
Elasticsearch-SQL 实现的功能: 1).插件式的安装。 2).SQL 语法查询。 3).超越 SQL 之外的查询。 4).对 JDBC 方式的支持。
我们使用的 es 版本是 7.4.1,如果你用的是不同版本,可以在 https://github.com/NLPchina/elasticsearch-sql 找到支持 切换 elsearch 用户,打开安装目录,执行以下命令安装插件
[root@bogon es]## su elsearch
[elsearch@bogon es]## cd /home/es/elasticsearch-7.4.1
[elsearch@bogon es]## ./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/7.4.1.0/elasticsearch-sql-7.4.1.0.zip
如果成功,命令行打印如下信息:
需要重启 elasticsearch 服务器,不然使用的时候会报如下错误:
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"No endpoint or operation is available at [sql]"}],"type":"illegal_argument_exception","reason":"No endpoint or operation is available at [sql]"},"status":400}
[root@bogon root]## ps -ef | grep elasticsearch #找到进程号
[root@bogon root]## kill -9 进程号
[root@bogon root]## su elastic
[elastic@bogon root]$ ES_PATH_CONF=/home/es/elasticsearch/config sh /home/es/elasticsearch-7.4.1/bin/elasticsearch -d
安装成功以后我们就可以通过 SQL 查询 ES 了。 1).先灌入一些数据:
POST /tvs/sales/_bulk
{ "index": {}}
{ "price" : 1000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-10-28" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 3000, "color" : "绿色", "brand" : "小米", "sold_date" : "2016-05-18" }
{ "index": {}}
{ "price" : 1500, "color" : "蓝色", "brand" : "TCL", "sold_date" : "2016-07-02" }
{ "index": {}}
{ "price" : 1200, "color" : "绿色", "brand" : "TCL", "sold_date" : "2016-08-19" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 8000, "color" : "红色", "brand" : "三星", "sold_date" : "2017-01-01" }
{ "index": {}}
{ "price" : 2500, "color" : "蓝色", "brand" : "小米", "sold_date" : "2017-02-12" }
2).使用方式,这里简单介绍两种:
①.在 kibana 界面的 Dev Tools 里面输入:GET _sql?sql=select * from tvs where price > 1000
②.在浏览器地址栏输入:http://ip:9200/_sql?sql=select * from tvs where price > 1000
1).下载扩展 ZIP 包:https://github.com/shi-yuan/elasticsearch-sql-site-chrome 2).解压。 3).在 chrome 浏览器输入:chrome://extensions/ 4).开启开发者模式。 5).加载已解压的扩展程序。 6).使用时点击扩展图标即可,可以将 SQL 语句翻译成 DSL 。
1).目前遇到一个问题,安装 Elasticsearch-SQL 插件后启动 Elasticsearch 报错:
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalArgumentException: Cannot replace existing handler for [/_sql] for method: POST
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.4.1.jar:7.4.1]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.4.1.jar:7.4.1]
Caused by: java.lang.IllegalArgumentException: Cannot replace existing handler for [/_sql] for method: POST
at org.elasticsearch.rest.MethodHandlers.addMethods(MethodHandlers.java:52) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.rest.RestController.lambda$registerHandler$1(RestController.java:156) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.common.path.PathTrie$TrieNode.insertOrUpdate(PathTrie.java:167) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.common.path.PathTrie$TrieNode.access$400(PathTrie.java:76) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.common.path.PathTrie.insertOrUpdate(PathTrie.java:316) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.rest.RestController.registerHandler(RestController.java:155) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.plugin.nlpcn.RestSqlAction.<init>(RestSqlAction.java:35) ~[?:?]
at org.elasticsearch.plugin.nlpcn.SqlPlug.getRestHandlers(SqlPlug.java:35) ~[?:?]
at org.elasticsearch.action.ActionModule.initRestHandlers(ActionModule.java:692) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.node.Node.<init>(Node.java:609) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.node.Node.<init>(Node.java:255) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:221) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:221) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.4.1.jar:7.4.1]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.4.1.jar:7.4.1]
... 6 more
[2019-10-31T09:38:03,170][INFO ][o.e.x.m.p.NativeController] [localhost.localdomain] Native controller process has stopped - no new native processes can be started
目前暂时没有找到解决方案,如果有知道的朋友还请赐教,临时解决办法,删除安装目录/Plugins 下的 sql 目录