【问题标题】:What is the right approach to migrating QueryOptions to the new Cassandra Java driver v4.14?将 QueryOptions 迁移到新的 Cassandra Java 驱动程序 v4.14 的正确方法是什么?
【发布时间】:2023-02-09 11:10:40
【问题描述】:
我正在从 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);
提前致谢!
【问题讨论】:
标签:
java
cassandra
datastax-java-driver
【解决方案1】:
使用驱动程序 4.x 键配置键在application.conf 中定义。只要文件在类路径中,它就会加载属性,这是无需更改代码即可设置应用程序的最佳方式。 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))
// 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());
}
可以在here 找到很多 4.x 代码并用作参考。