本文以ubuntu18.04、elasticsearch-7.1.1、DataGrip 2019.1.1为例
下载链接:https://www.elastic.co/cn/downloads/past-releases#jdbc-client
前往jetbrains官网下载对应平台的datagrip,安装即可
jetbrains官网:https://www.jetbrains.com/
datagrip的配置
#数据源用户密码入选
#es用户名[这里是默认]
Usr: elastic
#es密码[这里是默认]
Password: changme
URL: jdbc:es://localhost:9200
具体配置的动画如下:
配置完之后,笔者发现虽然链接上了es, 但是却无法看到ES的索引。报错如下:
Failed to retrieve table types.current license is non-compliant for [jdbc]
经过搜索发现,ES自带的license是basic版的,使用elasticsearch sql jdbc需要platinum版本,解决方案见下文。
如果你资金比较充裕或用户商业用途,请订阅官方渠道白金会员:https://www.elastic.co/cn/subscriptions
如果30试用即可满足你需求,启动kibana, 试用入口:Management->License management
!!!注意,该方式请勿用户商业用户,一记版权警告
该方式反编译(笔者使用的是idea)x-pack-core-x.x.x.jar的license认证相关的类,并篡改验证代码,修改后的类如下:
#LicenseVerifier.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.elasticsearch.license;
public class LicenseVerifier {
public LicenseVerifier() {
}
public static boolean verifyLicense(License license, byte[] publicKeyData) {
return true;
}
public static boolean verifyLicense(License license) {
return true;
}
}
#XPackBuild.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.elasticsearch.xpack.core;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.PathUtils;
public class XPackBuild {
public static final XPackBuild CURRENT;
private String shortHash;
private String date;
@SuppressForbidden(
reason = "looks up path of xpack.jar directly"
)
static Path getElasticsearchCodebase() {
URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
try {
return PathUtils.get(url.toURI());
} catch (URISyntaxException var2) {
throw new RuntimeException(var2);
}
}
XPackBuild(String shortHash, String date) {
this.shortHash = shortHash;
this.date = date;
}
public String shortHash() {
return this.shortHash;
}
public String date() {
return this.date;
}
static {
Path path = getElasticsearchCodebase();
String shortHash = null;
String date = null;
Label_0157:
{
shortHash = "Unknown";
date = "Unknown";
}
CURRENT = new XPackBuild(shortHash, date);
}
}
这两个类中依赖es安装目录下的 lib 、modules/x-pack-core两个目录下jar包。编译这两个类有多种方式:1)idea编译 2)javac命令编译
编译步骤:
这里以笔者上述提供的项目为例,编译过程如下:
cd elastic-parent
#编译java为class
javac -cp ".:./libraries/modules/x-pack-core/x-pack-core-7.1.1.jar:./libraries/lib/*" ./elastic-xpack-crack/src/main/java/org/elasticsearch/license/LicenseVerifier.java
javac -cp ".:./libraries/modules/x-pack-core/x-pack-core-7.1.1.jar:./libraries/lib/*" ./elastic-xpack-crack/src/main/java/org/elasticsearch/xpack/core/XPackBuild.java
#复制源jar包到临时目录
cp -rf ./libraries/modules/x-pack-core/x-pack-core-7.1.1.jar ./crack
cd crack
#解压jar包
jar -xf x-pack-core-7.1.1.jar
rm x-pack-core-7.1.1.jar
cd ../
#替换.class文件
mv -f elastic-xpack-crack/src/main/java/org/elasticsearch/license/LicenseVerifier.class crack/org/elasticsearch/license/
mv -f elastic-xpack-crack/src/main/java/org/elasticsearch/xpack/core/XPackBuild.class crack/org/elasticsearch/xpack/core/
#打包成jar包
jar -cf x-pack-core-7.1.1.jar ./crack/*
上述命令执行完成后,在crack目录就会生成一个新的x-pack-core-7.1.1.jar,用这个jar包替换es安装目录的下的modules/x-pack-core/x-pack-core-7.1.1.jar,然后重启ES服务。
申请地址:https://license.elastic.co/registration
申请成功后会收到一封邮件,附加license文件,修改至如下:
license.json
{
"license": {
"uid": "bc8b6fc7-dedf-4a1e-b5d3-xxxxx",
"type": "platinum",
"issue_date_in_millis": 1561852800000,
"expiry_date_in_millis": 1593561599999,
"max_nodes": 100,
"issued_to": "L Yo (HDU)",
"issuer": "Web Form",
"signature": "xxxxxxxx",
"start_date_in_millis": 1561852800000
}
}
注意:type由basic替换为platinum,expiry_date_in_millis时间戳到当前时间+1年即可,太大license会上传失败。
上传license之前,需要在elasticsearch.yml中添加如下配置:
xpack.security.enabled: true
打开kibana控制台, 进入如下界面, 上传license
上传成功后如下图所示:
再次打开DataGrip,就可以看到es中建立的索引了:
elasticsearch sql目前只支持一些比较简单的数据查询功能(select),有些SQL的语法还是不支持的,所以,对于es数据的操作还是建议在kibana控制台执行,比如:
elasticsearch sql正确示例:
select * from bank limit 20;
select * from bank where age >30;
select * from bank where account_number=1;
elasticsearch sql错误示例:
select B.* from bank B;
select * from bank limit 10,20;
delete from bank where account_number=1;