Elasticsearch7 connect using jdbc

Elasticsearch7.0.1 uses jdbc connection
Add maven dependencies to the project
<repositories>  
<repository>  
<id>elastic.co</id>  
<url>https://artifacts.elastic.co/maven</url>  
</repository>  
</repositories>  
<dependency>  
  <groupId>org.elasticsearch.plugin</groupId>  
  <artifactId>x-pack-sql-jdbc</artifactId>  
  <version>7.0.1</version>  
</dependency>  
Connect using test code
package com.hts;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.Statement;  
import java.util.Properties;  

public class ESJdbcTest {  

static String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";  
static String elasticsearchAddress = "192.168.79.128:9200";  

public static Properties connectionProperties(){  
Properties properties = new Properties();  
        //如果集群设置了密码
        //properties.put("user", "test_admin");  
        //properties.put("password", "x-pack-test-password");  
return properties;  
}  
public static void main(String[] args) {  

String address = "jdbc:es://http://" + elasticsearchAddress;  
Properties connectionProperties = connectionProperties();  
try {
Connection connection = DriverManager.getConnection(address, connectionProperties);  
Statement statement = connection.createStatement();  
ResultSet results = statement.executeQuery(
"SELECT firstname, account_number FROM bank ORDER BY account_number DESC LIMIT 5");  
while(results.next()){  
System.out.println(results.getString("firstname"));  
}  
}catch (Exception e){  
e.printStackTrace();  
}  
 
}  

}  

Exception: The error of Exception in thread "main" java.sql.SQLInvalidAuthorizationSpecException: current license is non-compliant for [jdbc] needs to be cracked and the platinum license needs to be updated;

  • Find x-pack-core-7.0.1.jar in the /elasticsearch-7.0.1/modules/x-pack-core directory and copy it out

  • Create two new java files named LicenseVerifier.java and XPackBuild.java, paste the following code

    ①: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);
    }
    }

  • To compile these two java files using javac, you need to introduce import dependencies and use the -cp parameter (the corresponding jar packages are available in the elasticsearch installation package)

    javac -cp “F:\es\x-pack-core-7.0.1.jar;F:\es\lucene-core-8.0.0.jar;F:\es\elasticsearch-7.0.1.jar;F:\es\elasticsearch-core-7.0.1.jar” F:esLicenseVerifier.java
    javac -cp “F:\es\x-pack-core-7.0.1.jar;F:\es\lucene-core-8.0.0.jar;F:\es\elasticsearch-7.0.1.jar;F:\es\elasticsearch-core-7.0.1.jar” F:esXPackBuild.java

  • Overwrite the generated LicenseVerifier.class over the LicenseVerifier.class under the directory org/elasticsearch/license in the copied x-pack-core-7.0.1.jar, and overwrite the generated XPackBuild.class over the org/elasticsearch/xpack/core directory XPackBuild.class under

  • Copy the overwritten x-pack-core-7.0.1.jar back to the /elasticsearch-7.0.1/modules/x-pack-core directory to overwrite the original

  • Obtain license certificate
    ①: https://license.elastic.co/registration Fill in some username, email address (important, get the download link), select China for Country, fill in other information at will, click Send
    ② Open the address obtained by the email, and download The later file is renamed license.json ③Modify


    the contents of the file and change the two attributes to the following
    Replace "type": "basic" with "type": "platinum" # Change the basic class to platinum version and
    "expiry_date_in_millis" :1561420799999 is replaced by
    ④ Use curl to replace the license (license.json refers to the certificate after downloading the modified attributes, to enable the elasticsearch service)
    curl -XPUT 'http://127.0.0.1:9200/_xpack/licenseacknowledge=true' - d @license.json
    insert image description here

    ⑤You may encounter the error of Cannot install a [PLATINUM] license unless TLS is configured or security is disabled. The solution is added in elasticsearch.yml:
    xpack.security.enabled: false ⑥Check
    the certificate time after uploading http://127.0. 0.1:9200/_license

    insert image description here

Guess you like

Origin blog.csdn.net/m0_54853503/article/details/124241580