我的以下代码有异常,原因是


  java.lang.IllegalArgumentException:预期的类com.datastax.driver.core.PlainTextAuthProvider(由advanced.auth-provider.class指定)是com.datastax.oss.driver.api.core.auth.AuthProvider的子类型。


import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder;
import org.asynchttpclient.netty.ssl.DefaultSslEngineFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Host;

import javax.net.ssl.SSLContext;
import java.security.KeyStore;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.net.ssl.TrustManagerFactory;
import com.datastax.driver.core.JdkSSLOptions;

import java.util.*;
import java.util.stream.Collectors;
import java.net.InetSocketAddress;
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;

    private void test_cqlsession(String trustStoreLocation, String password, List<String> ips) {

        ProgrammaticDriverConfigLoaderBuilder configBuilder = DriverConfigLoader
                .programmaticBuilder()
                .withClass(DefaultDriverOption.AUTH_PROVIDER_CLASS, PlainTextAuthProvider.class)
                .withString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME, USER_NAME)
                .withString(DefaultDriverOption.AUTH_PROVIDER_PASSWORD, password)
                .withInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS, 32768);

        if (trustStoreLocation != null) {
            configBuilder
                    .withClass(DefaultDriverOption.SSL_ENGINE_FACTORY_CLASS, DefaultSslEngineFactory.class)
                    .withString(DefaultDriverOption.SSL_TRUSTSTORE_PATH, trustStoreLocation)
                    .withString(DefaultDriverOption.SSL_TRUSTSTORE_PASSWORD, password);
        }

        CqlSession session = CqlSession.builder()
                .withConfigLoader(configBuilder.build())
                .addContactEndPoints(
                        Arrays.stream(ips.toArray())
                        .map(host -> new DefaultEndPoint(
                                InetSocketAddress.createUnresolved(host.toString(), 9042)))
                        .collect(Collectors.toList())
                )
                .withLocalDatacenter(DATA_CENTER)
                .build();

    .
    .
    .

}


我没有找到如何使用此示例。

最佳答案

您有一个使用2个版本的Java驱动程序的项目:3.x和4.x。您需要删除对驱动程序3.x的依赖,其中定义了PlainTextAuthProvider

另外,代替:

.withClass(DefaultDriverOption.AUTH_PROVIDER_CLASS, PlainTextAuthProvider.class)
                .withString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME, USER_NAME)
                .withString(DefaultDriverOption.AUTH_PROVIDER_PASSWORD, password)


在配置中,只需使用:

CqlSession session =
    CqlSession.builder()
        .withAuthCredentials("user", "pass")


documentation中所述。

10-02 22:05