使用jdbc连接ES6.3

ES6.3自带了sql查询,但是官网上的JDBC连接es6.3文档写的很马虎,本人实现后以作记录,首先安装官网安装ES6.3.

安装后先建立index

PUT /library/book/_bulk?refresh
{"index":{"_id": "Leviathan Wakes"}}
{"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
{"index":{"_id": "Hyperion"}}
{"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
{"index":{"_id": "Dune"}}
{"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}

maven依赖

The JDBC driver can be obtained either by downloading it from the elastic.co site or by using a Maven-compatible tool with the following dependency:


  org.elasticsearch.plugin
  jdbc
  6.3.0

from artifacts.elastic.co/maven by adding it to the repositories list:


  
    elastic.co
    https://artifacts.elastic.co/maven
  


es的jdb jar

https://artifacts.elastic.co/maven/org/elasticsearch/plugin/jdbc/6.3.0/jdbc-6.3.0.jar


es的jdbc jar依赖

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-jdbc-with-dependencies-6.3.0.zip


代码如下


package com.es;

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

/**
 * Created by Administrator on 2018/6/16 0016.
 */
public class EsSQL {
    public static void main(String args[]) throws SQLException {

        String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";
        try {
            Class.forName(driver).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        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 name, page_count FROM library ORDER BY page_count DESC LIMIT 1");
        while (results.next()) {
            System.out.println(results.getString(1));
        }

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


    }
}



 但是 还是会报错 ,报错信息如下


Exception in thread "main" java.sql.SQLInvalidAuthorizationSpecException: current license is non-compliant for [jdbc]
	at org.elasticsearch.xpack.sql.client.shared.JreHttpUrlConnection$SqlExceptionType.asException(JreHttpUrlConnection.java:306)
	at org.elasticsearch.xpack.sql.client.shared.JreHttpUrlConnection.parserError(JreHttpUrlConnection.java:183)
	at org.elasticsearch.xpack.sql.client.shared.JreHttpUrlConnection.request(JreHttpUrlConnection.java:158)
	at org.elasticsearch.xpack.sql.client.HttpClient.lambda$post$0(HttpClient.java:101)
	at org.elasticsearch.xpack.sql.client.shared.JreHttpUrlConnection.http(JreHttpUrlConnection.java:62)
	at org.elasticsearch.xpack.sql.client.HttpClient.lambda$post$1(HttpClient.java:100)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.elasticsearch.xpack.sql.client.HttpClient.post(HttpClient.java:99)
	at org.elasticsearch.xpack.sql.client.HttpClient.query(HttpClient.java:77)
	at org.elasticsearch.xpack.sql.jdbc.net.client.JdbcHttpClient.query(JdbcHttpClient.java:51)
	at org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcStatement.initResultSet(JdbcStatement.java:162)
	at org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcStatement.execute(JdbcStatement.java:153)
	at org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:42)
	at com.es.EsSQL.main(EsSQL.java:32)

是因为

 JDBC driver required a platinum license,解决方案需要按以下步骤破解x-pack,并更新为白金许可

官网对应的 证书类型和相应权限

https://www.elastic.co/subscriptions 


破解x-pack 6.3.0

1.在F:\ES6.3\6.3.0\modules\x-pack\x-pack-core目录下找到x-pack-core-6.3.0.jar

2.解压jar包,然后找到如下两个class文件,使用luyten反编译

org/elasticsearch/license/LicenseVerifier.class
org/elasticsearch/xpack/core/XPackBuild.class

3.将反编译后的java 代码复制到自己的IDE中,按照同样的包名创建pack(可以直接创建如下两个文件,省略第二部) 

(1)LicenseVerifier 中有两个静态方法,这就是验证授权文件是否有效的方法,我们把它修改为全部返回true.

修改后的如下


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;
    }
}

2)XPackBuild 中 最后一个静态代码块中 try的部分全部删除,这部分会验证jar包是否被修改,修改后的如下

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


4.编译这两个文件 

我们不需要编译整个项目,只需要编译这两个文件,所以要把依赖添加到classpath中,依赖也与之前有所变化,之前只需要x-pack 包本身,现在需要引入 elasticsearch 6.3.0 中 lib 目录下的jar包 以及 x-pack-core-6.3.0.jar 本身

javac -cp "/home/zhu/x-pack-core-6.3.0/x-pack-core-6.3.0.jar:/home/zhu/x-pack-core-6.3.0/lucene-core-7.3.1.jar:/home/zhu/x-pack-core-6.3.0/elasticsearch-6.3.0.jar:/home/zhu/x-pack-core-6.3.0/elasticsearch-core-6.3.0.jar" /home/zhu/x-pack-core-6.3.0/LicenseVerifier.java
javac -cp "/home/zhu/x-pack-core-6.3.0/x-pack-core-6.3.0.jar:/home/zhu/x-pack-core-6.3.0/lucene-core-7.3.1.jar:/home/zhu/x-pack-core-6.3.0/elasticsearch-6.3.0.jar:/home/zhu/x-pack-core-6.3.0/elasticsearch-core-6.3.0.jar" /home/zhu/x-pack-core-6.3.0/XPackBuild.java 

 

cd /home/zhu/x-pack-core-6.3.0/

cp LicenseVerifier.class  ./x-pack-core-6.3.0/org/elasticsearch/license/

 cp XPackBuild.class ./x-pack-core-6.3.0/org/elasticsearch/xpack/core/


5.使用重新编译的两个class文件替换原有的class文件,然后重新打jar包

jar -cvf x-pack-core-6.3.0.jar ./*

ES获取证书

获取 license 证书 

https://license.elastic.co/registration

此证书的时间为1年使用时间,你可以通过下面网站进行换算http://tool.chinaz.com/Tools/unixtime.aspx,目前我申请了一个50 年的时间

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


通过 http://127.0.0.1:9200/_license 查看当前license


{
  "license" : {
    "status" : "active",
    "uid" : "f89447e5-436d-4ba5-bef2-b249f4b64770",
    "type" : "basic",
    "issue_date" : "2018-06-16T13:16:24.422Z",
    "issue_date_in_millis" : 1529154984422,
    "max_nodes" : 1000,
    "issued_to" : "es6.3",
    "issuer" : "elasticsearch",
    "start_date_in_millis" : -1
  }
}


替换 license

curl -XPUT -u elastic:changeme 'http://127.0.0.1:9200/_xpack/license?acknowledge=true' -d @li-xiang-d28260d9-6c96-4dd2-92dc-2f14a9787903-v5.json

也可以在kibana界面上操作

使用jdbc连接ES6.3_第1张图片



在安装新的证书时会报错

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

原因: 
除非配置了TLS或禁用安全性,否则无法安装[白金]许可证。

解决方法: 
elasticsearch.yml新增:

xpack.security.enabled: false



上传新的证书后再次查看

http://127.0.0.1:9200/_license

{
  "license" : {
    "status" : "active",
    "uid" : "3d7e3257-ce82-4b90-872d-f3206018e3cd",
    "type" : "platinum",
    "issue_date" : "2018-06-24T00:00:00.000Z",
    "issue_date_in_millis" : 1529798400000,
    "expiry_date" : "2068-06-24T06:50:00.000Z",
    "expiry_date_in_millis" : 3107746200000,
    "max_nodes" : 100,
    "issued_to" : "zhu shangjin (ps)",
    "issuer" : "Web Form",
    "start_date_in_millis" : 1529798400000
  }
}

已经是白金版的了,有效期50年

然后就可以用jdbc链接es了


你可能感兴趣的:(CDH,Java和Jvm)