通过JDBC连接Elasticsearch 7.6.2

1、添加maven依赖

        elastic.co

        https://artifacts.elastic.co/maven

   

    org.elasticsearch.plugin

    x-pack-sql-jdbc

    7.6.2

2、使用测试代码连接

public static void main(String[] args) {

    try {

        Connection connection = DriverManager.getConnection("jdbc:es://http://172.16.62.121:19200");// 将ES-IP换位ES服务器的IP

        Statement statement = connection.createStatement();

        ResultSet results = statement.executeQuery("select * from vmware_vm_day_perf LIMIT 5");

        System.out.println(results);

        while (results.next()) {

            System.out.println(results.getString("vmName"));

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

如果未关闭授权验证,需要添加用户名和密码参数

关闭用户名密码验证的方式:

在ES的配置文件在elasticsearch.yml新增中

xpack.security.enabled: false

3、破解x-pack-core

白金版支持JDBC连接,基础版是不支持的,你可以申请一个白金版,也可以像我一样破解x-pack-core

接下来操作是以7.6.2版本为例,其他版本基本一样

x-pack-core.7.6.2.jar位置:$ES_HOME/modules/x-pack-core/x-pack-core.7.6.2.jar

$ES_HOME为Elasticsearch的安装目录

随便找一个目录创建两个java文件 分别是:

LicenseVerifier.java

package org.elasticsearch.license;

/**

*  * Responsible for verifying signed licenses

*   */

public class LicenseVerifier {

    /**

*      * verifies the license content with the signature using the packaged

*           * public key

*                * @param license to verify

*                     * @return true if valid, false otherwise

*                          */

    public static boolean verifyLicense(final License license, byte[] publicKeyData) {

        return true;

    }

    public static boolean verifyLicense(final License license) {

        return true;

    }

}

XPackBuild.java

package org.elasticsearch.xpack.core;

import org.elasticsearch.common.SuppressForbidden;

import org.elasticsearch.common.io.PathUtils;

import java.io.IOException;

import java.net.URISyntaxException;

import java.net.URL;

import java.nio.file.Files;

import java.nio.file.Path;

import java.util.jar.JarInputStream;

import java.util.jar.Manifest;

public class XPackBuild {

    public static final XPackBuild CURRENT;

    static {

        CURRENT = new XPackBuild("Unknown", "Unknown");

    }

    /**

*      * Returns path to xpack codebase path

*           */

    @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 bogus) {

            throw new RuntimeException(bogus);

        }

    }

    private String shortHash;

    private String date;

    XPackBuild(String shortHash, String date) {

        this.shortHash = shortHash;

        this.date = date;

    }

    public String shortHash() {

        return shortHash;

    }

    public String date() {

        return date;

    }

}

重新编译

javac -cp "/data/work/elasticsearch-7.6.2_master/lib/elasticsearch-7.6.2.jar:/data/work/elasticsearch-7.6.2_master/lib/lucene-core-8.4.0.jar:/data/work/elasticsearch-7.6.2_master/modules/x-pack-core/x-pack-core-7.6.2.jar" LicenseVerifier.java

javac -cp "/data/work/elasticsearch-7.6.2_master/lib/elasticsearch-7.6.2.jar:/data/work/elasticsearch-7.6.2_master/lib/lucene-core-8.4.0.jar:/data/work/elasticsearch-7.6.2_master/modules/x-pack-core/x-pack-core-7.6.2.jar:/data/work/elasticsearch-7.6.2_master/lib/elasticsearch-core-7.6.2.jar" XPackBuild.java

生成对应的.class文件

-rw-r--r-- 1 root    root     410 6月   2 09:42 LicenseVerifier.class

-rw-r--r-- 1 root    root     588 6月   2 09:41 LicenseVerifier.java

-rw-r--r-- 1 root    root    1464 6月   2 09:42 XPackBuild.class

-rw-r--r-- 1 root    root    1232 6月   2 09:41 XPackBuild.java

通过压缩软件将生成的.class文件分别替换到对应的目录下

org\elasticsearch\license\LicenseVerifier.class

org\elasticsearch\xpack\core\XPackBuild.class

然后用新的jar替换$ES_HOME/modules/x-pack-core/x-pack-core.7.6.2.jar

更新许可证

官方申请地址:https://register.elastic.co/marvel_register

邮箱和国家要正确填写,其他随便

根据你邮件收到的地址下载,下载后更名为license.json

将 "type":"basic" 替换为 "type":"platinum" # 基础班变更为白金版

将 "expiry_date_in_millis":的时间戳修改,具体想修改多长时间随意,但是一定要时间戳。

上传license文件

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

重启Elasticsearch服务

上传后查看证书时间http://ES-IP:9200/_license

你可能感兴趣的:(通过JDBC连接Elasticsearch 7.6.2)