如何将QueryOptions迁移到新的Cassandra Java驱动程序v4.14的正确方法是什么?

huangapple go评论61阅读模式
英文:

What is the right approach to migrating QueryOptions to the new Cassandra Java driver v4.14?

问题

我正在从Datastax Cassandra Driver 1.9迁移到4.14.x版本。

我对如何迁移以下代码感兴趣:

Builder builder =
    Cluster.builder()
        .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM));

这个迁移的方式和上面的代码等效吗?

DriverConfigLoader driverConfigLoader = DriverConfigLoader.programmaticBuilder()
    .withString(DefaultDriverOption.REQUEST_CONSISTENCY, "LOCAL_QUORUM")
    .build();
final CqlSessionBuilder cqlSessionBuilder =
    CqlSession.builder()
        .withConfigLoader(driverConfigLoader);

谢谢!

英文:

I am migrating from Datastax Cassandra Driver 1.9 to 4.14.x

I would be interested how to migrate this code:

Builder builder =
        Cluster.builder()
            .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM));

Is that the right approach and the equivalent to the code above?

DriverConfigLoader driverConfigLoader = DriverConfigLoader.programmaticBuilder()
        .withString(DefaultDriverOption.REQUEST_CONSISTENCY, "LOCAL_QUORUM")
        .build();
    final CqlSessionBuilder cqlSessionBuilder =
        CqlSession.builder()
            .withConfigLoader(driverConfigLoader);

Thanks in advance!

答案1

得分: 1

以下是翻译好的内容:

With the drivers 4.x keys configuration keys be defined in application.conf. As long as the file is in the classpath it will load properties and it is the best way to setup your application without having to change your code. documentation

知道了这一点,如果你仍然想以编程方式进行配置,你确实采取了正确的方法:

DriverConfigLoader loader = DriverConfigLoader.programmaticBuilder()
        .withStringList(DefaultDriverOption.CONTACT_POINTS, Arrays.asList("127.0.0.1:9042"))
        .withString(DefaultDriverOption.REQUEST_CONSISTENCY, "LOCAL_QUORUM")
        .withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, "datacenter1")
        .withString(DefaultDriverOption.SESSION_KEYSPACE, KEYSPACE_NAME)
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(5))
        
        // 如果你想要使用执行配置文件进行覆盖
        .startProfile("slow")
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30))
        .endProfile()
        .build();
// 使用它来创建会话
try (CqlSession cqlSession = CqlSession.builder().withConfigLoader(loader).build()) {
    
    // 使用会话
    LOGGER.info("[OK] Connected to Keyspace {}", cqlSession.getKeyspace().get());
}

A lot of 4.x code can be found here and used as reference.

英文:

With the drivers 4.x keys configuration keys be defined in application.conf. As long as the file is in the classpath it will load properties and it is the best way to setup your application without having to change your code. documentation

Knowing this, if you still want to do configuration programmatically you indeed have the correct approach:

 DriverConfigLoader loader = DriverConfigLoader.programmaticBuilder()
        .withStringList(DefaultDriverOption.CONTACT_POINTS, Arrays.asList("127.0.0.1:9042"))
        .withString(DefaultDriverOption.REQUEST_CONSISTENCY, "LOCAL_QUORUM")
        .withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, "datacenter1")
        .withString(DefaultDriverOption.SESSION_KEYSPACE, KEYSPACE_NAME)
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(5))
        
        // If you want to override with an execution profile
        .startProfile("slow")
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30))
        .endProfile()
        .build();
    // Use it to create the session
    try (CqlSession cqlSession = CqlSession.builder().withConfigLoader(loader).build()) {
        
        // Use session
        LOGGER.info("[OK] Connected to Keyspace {}", cqlSession.getKeyspace().get());
    }

A lot of 4.x code can be found here and used as reference.

答案2

得分: 0

Cassandra Java driver 从版本4.0起完全重构,因此与早期版本不兼容,这意味着先前的API和类,如 QueryOptions,已被替换。

有多种配置驱动程序的方式,包括在您的示例中使用 programmaticBuilder(),它允许您以编程方式指定配置选项。

还有一个配置API,允许您在运行时使用动态配置文件或加载配置选项。

您的示例代码是以前版本中的编程构建器的直接翻译,它将有效。然而,我们认为在大多数情况下,开发人员应该使用应用程序配置属性文件(application.conf或其变体)来配置驱动程序。使用此选项,驱动程序配置与应用程序解耦。

这种方法的明显优势是您可以轻松重新配置驱动程序,而无需重新编译应用程序。

一个可用的参考配置文件(reference.conf,其中包含开发人员的大量内联注释。有关详细信息,请参阅配置Cassandra Java驱动程序。干杯!

英文:

The Cassandra Java driver was completely refactored from the ground up in version 4.0 so it does mean that it is not binary-compatible with earlier versions so the previous APIs and classes like QueryOptions have been replaced.

There are several ways to configure the driver including the programmaticBuilder() in your example which allows you to programmatically specify configuration options.

There is also a configuration API which allows you to use dynamic profiles or load configuration options at runtime.

Your example code is a direct translation of the programmatic builder in previous releases and it will work. However, our opinion is that in most cases, developers should configure the driver using an application configuration properties file (application.conf or its variations) placed in the application classpath. With this option, the driver configuration is de-coupled from your application.

The obvious advantage of this method is that you can easily reconfigure the driver without having to recompile your application.

There is a reference configuration file (reference.conf) available that has tons of in-line comments for developers. For details, see Configuring the Cassandra Java driver. Cheers!

huangapple
  • 本文由 发表于 2023年2月8日 20:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定