Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK版本报错 在jdbc实现中 #1247

Open
leviYX opened this issue Mar 26, 2024 · 2 comments
Open

JDK版本报错 在jdbc实现中 #1247

leviYX opened this issue Mar 26, 2024 · 2 comments

Comments

@leviYX
Copy link

leviYX commented Mar 26, 2024

1、我的配置文件pom如下:

UTF-8 UTF-8 2.3.7.RELEASE 8.8.0 1.8 1.8
<dependencies>

    <dependency>
        <groupId>co.elastic.clients</groupId>
        <artifactId>elasticsearch-java</artifactId>
        <version>8.8.0</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>8.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>8.8.0</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch-preallocate</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.unboundid</groupId>
        <artifactId>unboundid-ldapsdk</artifactId>
        <version>6.0.7</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api -->

    <dependency>
        <groupId>com.wenge</groupId>
        <artifactId>elasticsearch-sql</artifactId>
        <version>${es.version}</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/elasticsearch-sql-8.8.0.0.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>com.wenge</groupId>
        <artifactId>guava</artifactId>
        <version>32.0.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/guava-32.0.0-jre.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>com.wenge</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.15</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/druid-1.2.15.jar</systemPath>
    </dependency>
</dependencies>

2、java代码如下,基本是按照源码包的单元测试复制的

@test
public void testJDBCWithParameter() throws Exception {
DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(createElasticsearchClient());
Connection connection = dds.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT * from " + INDEX_NAME );
ResultSet resultSet = ps.executeQuery();

    ResultSetMetaData metaData = resultSet.getMetaData();


    List<String> result = new ArrayList<String>();
    String scrollId = null;
    while (resultSet.next()) {
        scrollId = resultSet.getString("_scroll_id");
        result.add(resultSet.getString("lastname") + "," + resultSet.getInt("age") + "," + resultSet.getString("gender") + "," + resultSet.getString("gender.keyword"));
    }

    ps.close();
    connection.close();
    dds.close();
}
public static Client createElasticsearchClient() throws UnknownHostException {
    return new ElasticsearchRestClient(new ElasticsearchClient(getElasticsearchTransport(getRestClient())));
}
private static ElasticsearchTransport getElasticsearchTransport(RestClient restClient) {
    TransportOptions.Builder transportOptionsBuilder = new RestClientOptions(RequestOptions.DEFAULT).toBuilder();

    ContentType jsonContentType = Version.VERSION == null ? ContentType.APPLICATION_JSON
            : ContentType.create("application/vnd.elasticsearch+json",
            new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));

    Consumer<String> setHeaderIfNotPresent = header -> {
        if (transportOptionsBuilder.build().headers().stream().noneMatch((h) -> h.getKey().equalsIgnoreCase(header))) {
            transportOptionsBuilder.addHeader(header, jsonContentType.toString());
        }
    };

    setHeaderIfNotPresent.accept("Content-Type");
    setHeaderIfNotPresent.accept("Accept");

    TransportOptions transportOptionsWithHeader = transportOptionsBuilder.build();
    return new RestClientTransport(restClient, new JacksonJsonpMapper(), transportOptionsWithHeader);
}

private static RestClient getRestClient() throws UnknownHostException {
    InetSocketAddress address = new InetSocketAddress("172.16.10.26",9200);
    String hostPort = String.format("http://%s:%s", address.getHostString(), address.getPort());

    RestClientBuilder builder = RestClient.builder(HttpHost.create(hostPort));
    builder.setHttpClientConfigCallback(clientBuilder -> {
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        requestConfigBuilder.setConnectTimeout(10 * 1000);

        int socketTimeout = 90 * 1000;
        requestConfigBuilder.setSocketTimeout(socketTimeout);
        requestConfigBuilder.setConnectionRequestTimeout(socketTimeout);
        clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

        return clientBuilder;
    });
    return builder.build();
}

然后执行单元测试报错如下,createElasticsearchClient()方法的返回类型org.elasticsearch.client.internal.Client;的jdk编译版本不一样,编译是jdk17,我是运行在jdk8中的,这个是为啥呢
java: 无法访问org.elasticsearch.client.internal.Client
错误的类文件: /D:/Program Files (x86)/apache-maven-3.6.1-bin/apache-maven-3.6.1/repo/org/elasticsearch/elasticsearch/8.8.0/elasticsearch-8.8.0.jar!/org/elasticsearch/client/internal/Client.class
类文件具有错误的版本 61.0, 应为 52.0
请删除该文件或确保该文件位于正确的类路径子目录中。

@leviYX
Copy link
Author

leviYX commented Mar 26, 2024

需要JDK环境升级到JDK17吗。但是我看test测试类,里面也是jdk8

@shi-yuan
Copy link
Member

需要升级的,因为elasticsearch的jar包是用高版本编译打包的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants