使用sql jdbc 连接elasticsearch 6.3版本

最近公司要做后端接口调用,数据存储在es,我的打算是用sql查es,用springboot提供接口调用,es6.3版本开始支持sql查询,好了,废话不多bb,开始咯。

首先先创建一个java项目,pom文件中添加如下依赖:

<repositories>
  <repository>
    <id>elastic.co</id>
    <url>https://artifacts.elastic.co/maven</url>
  </repository>
</repositories>

<dependency>
  <groupId>org.elasticsearch.plugin</groupId>
  <artifactId>jdbc</artifactId>
  <version>6.3.0</version>
</dependency>

接下来 上代码

import java.sql.*;
import java.util.Properties;

public class EsTest {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";
        Class.forName(driver).newInstance();
        String address = "jdbc:es://127.0.0.1:9200";
        Properties connectionProperties = new Properties();
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(address, connectionProperties);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery("SELECT * FROM flower");
        while (results.next()) {
            System.out.println(results.getString(1));
            System.out.println(results.getString(2));
            System.out.println(results.getString(3));
            System.out.println(results.getString(4));
            System.out.println(results.getString(5));
            System.out.println(results.getString(6));

        }

        //关闭资源
        results.close();
        statement.close();
        connection.close();

    }
}

运行一波发现:

Exception in thread "main" java.sql.SQLInvalidAuthorizationSpecException: current license is non-compliant for [jdbc]

卧槽,报错了,心凉了一半,不过没关系,网上肯定一大堆解决办法啦,看了一会发现,报错的原因是需要es的白金许可证才能使用jdbc功能,白金许可证是啥玩意呢,说白了就是
破解一波x-pack,接下来,x-pack破解开始:

我们的object是,替换x-pack-core-6.3.0.jar包。

首先进入es/modules/x-pack/x-pack-core,找到x-pack-core-6.3.0.jar,解压开jar包,我就直接在当前文件夹操作的,创建两个java文件,分别命名为LicenseVerifier.java和XPackBuild.java。

LicenseVerifier.java文件内容

package org.elasticsearch.license;
 
import java.nio.*;
import org.elasticsearch.common.bytes.*;
import java.util.*;
import java.security.*;
import org.elasticsearch.common.xcontent.*;
import org.apache.lucene.util.*;
import org.elasticsearch.core.internal.io.*;
import java.io.*;
 
 
public class LicenseVerifier
{
    public static boolean verifyLicense(final License license, final byte[] encryptedPublicKeyData) {
       return true;
    }
    
    public static boolean verifyLicense(final License license) {
         return true;
    }
}

XPackBuild.java文件内容:

package org.elasticsearch.xpack.core;
 
import org.elasticsearch.common.io.*;
import java.net.*;
import org.elasticsearch.common.*;
import java.nio.file.*;
import java.io.*;
import java.util.jar.*;
 
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() {
        final URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
        try {
            return PathUtils.get(url.toURI());
        }
        catch (URISyntaxException bogus) {
            throw new RuntimeException(bogus);
        }
    }
    
    XPackBuild(final String shortHash, final String date) {
        this.shortHash = shortHash;
        this.date = date;
    }
    
    public String shortHash() {
        return this.shortHash;
    }
    
    public String date() {
        return this.date;
    }
    
    static {
        final Path path = getElasticsearchCodebase();
        String shortHash = null;
        String date = null;
        Label_0157: {
           
            shortHash = "Unknown";
            date = "Unknown";
        }
        CURRENT = new XPackBuild(shortHash, date);
    }
}

你要问我啥意思,我的解释是,改源码呗,本来不能连接jdbc,改完就能了。

编辑完这两个文件,编译一波:

javac -cp "x-pack-core-6.3.0.jar路径:lucene-core-7.3.1.jar路径:elasticsearch-6.3.0.jar路径:elasticsearch-core-6.3.0.jar路径" LicenseVerifier.java路径

javac -cp "x-pack-core-6.3.0.jar路径:lucene-core-7.3.1.jar路径:elasticsearch-6.3.0.jar路径:elasticsearch-core-6.3.0.jar路径" XPackBuild.java路径

其中x-pack-core-6.3.0.jar路径就是我们打开的jar包路径,后边三个jar包的路径在es/lib下面,这命令要是打不明白,那我就没办法啦。

编译完生成了两个class文件,复制LicenseVerifier.class到/elasticsearch-6.3.0/modules/x-pack/x-pack-core/org/elasticsearch/license/下,把XPackBuild.class复制到/elasticsearch-6.3.0/modules/x-pack/x-pack-core/org/elasticsearch/xpack/core/下。然后把文件夹,注意是文件夹重新打包,不管你把文件夹搞到哪里,你把里面的文件打成jar包并且命名为x-pack-core-6.3.0.jar就行。命令是jar -cvf x-pack-core-6.3.0.jar ./*

这时候你的jar包替换完成,我没有遇到权限问题,如果有用户权限问题,自己chmod一下。
然后启动es服务。

接下来一步是去es官网获取license 证书,网址:https://license.elastic.co/registration,
填写你的邮箱地址,国家选中国,给你发证书。
下载下来后是一个json文件,证书正常是一年有效期,这怎么能难倒我们呢,我看大哥们直接都改成了50年,我直接copy了一波,哈哈。
只需要修改json文件中两个字段值

将 "type":"basic" 替换为 "type":"platinum"    # 基础班变更为铂金版
将 "expiry_date_in_millis":1561420799999 替换为 "expiry_date_in_millis":3107746200000# 1年变为50年

改完字段值,最好把json文件改个名字,要不太丑了,改成了license.json
看到网上网友很多都报一个错误:

Cannot install a [PLATINUM] license unless TLS is configured or security is disabled

我是没遇到,遇到的话就在elasticsearch.yml新增一句:

xpack.security.enabled: false

这时候证书也有了,服务也起了,接下来就是把证书发送给es集群,让它白金50年。
命令:

curl -XPUT   'http://localhost:9200/_xpack/license?acknowledge=true' -d @license.json

执行完你会发现:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

还报错,都临门一脚了,我肯定不能忍,发现是由于版本问题,并不能识别json的问题,改过之后的命令:

curl -H "Content-Type:application/json" -XPUT 'http://localhost:9200/_xpack/license?acknowledge=true' -d @license.json

意思就是让es知道我传给它的是一个json,然后看一波证书时间:http://localhost:9200/_license

{
    "license": {
        "status": "active",
        "uid": "17744624-170d-4a6f-a2fd-3ef31138fbcb",
        "type": "platinum",
        "issue_date": "2019-03-05T00:00:00.000Z",
        "issue_date_in_millis": 1551744000000,
        "expiry_date": "2068-06-24T06:50:00.000Z",
        "expiry_date_in_millis": 3107746200000,
        "max_nodes": 100,
        "issued_to": "王 一 (zdcy)",
        "issuer": "Web Form",
        "start_date_in_millis": 1551744000000
    }
}

卧槽,白金尊贵会员,牛皮。

再重新跑一波程序,成功,牛皮!
在这里插入图片描述
我弄的es集群是单机版的,我觉得生产环境,就是把jar包替换一下,然后把证书发送给集群也照样可以用的,如果不行就重新注册一个证书。现在es用sql查询已经实现,接下来我就要集成springboot接口调用了,我们下篇再见。

我是皮蛋,拜拜。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值