datastax cassandra java驱动程序plaintextauthprovider示例

n1bvdmb6  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(508)

下面的代码有一个异常,由
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();

    .
    .
    .

}

我没有找到一个如何使用这个的例子。

v1uwarro

v1uwarro1#

您有一个使用两个版本的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")

如文件所述。

相关问题