我安装的elasticsearch版本是elasticsearch-6.2.3,6.3版本之后,elasticsearch内置集成了x-pack插件,只能使用部分功能,需要使用全部功能需要授权的,而通过SQL访问elasticsearch需要授权的部分(收费的),但是有大神已经破解了。
插件地址
https://github.com/NLPchina/elasticsearch-sql
安装步骤:
1.下载elasticsearch-sql zip包,直接下载到es根目录的plugin的目录下,要下载对应的elasticsearch对应的 Elasticsearch-sql插件版本,如果不知道可以在 Elasticsearch-sql官网查看,地址是:https://github.com/NLPchina/elasticsearch-sql
./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/6.2.3.0/elasticsearch-sql-6.2.3.0.zip
2. 5.0以上版本还需要下载: wget https://github.com/NLPchina/elasticsearch-sql/releases/download/5.4.1.0/es-sql-site-standalone.zip,这个es-sql-site-standalone.zip没有版本之分,统一下载这个。
3.解压:unzip es-sql-site-standalone.zip d
到site-server目录下,cd site-server
执行,npm install express --save
4.启动,node node-server.js,
5.默认启动是以8080端口启动的,需要修改端口号的在site_configuration.json配置文件中修改
cd es-sql-site/site-server vim site_configuration.json
注意:es-sql 5.x版本以后需要node及npm的安装,可以通过node -v和npm -v 命令查看是否安装的npm和node
启动后访问遇到的问题:
启动后的通过IP+配置的端口访问web页面
1.遇到以下的问题,是因为访问elasticsearch的地址可端口不对导致的,
Error:"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /_sql</pre>\n</body>\n</html>\n"
在页面的右上角修改IP和端口:
2.遇到一下问题需要修改elasticsearch配置文件,添加cors:
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-origin: "/.*/"
http.cors.allow-headers: WWW-Authenticate,X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
通过JDBC访问elasticsearch
elasticsearch安装完elasticsearch-SQL插件后,可以通过java中JDBC访问elasticsearch,直接写SQL语句可以查询es数据库
直接上代码:
依赖有:
注意,这个elasticsearch-sql底层是通过Druid实现的,elasticsearch-sql这个jar中的Druid的版本是1.0.15,如果你的项目中有引用其他版本的Druid包,有可能出现冲突,最好保持在1.0.15。