如何从 datastax ClusterBuilder 构建 Phantom CassandraConnection

How to build a Phantom CassandraConnection from datastax ClusterBuilder

我在 datastax 驱动程序上编写了以下代码来建立 cassandra 连接。

  val cluster = Cluster.builder()
     .withCompression(ProtocolOptions.Compression.LZ4)
     .addContactPoints(List("a", "b").asJava)
     .withCredentials("foo", "bar")
     .withPort(1111)
     .withProtocolVersion(ProtocolVersion.V4)
     .withPoolingOptions(new PoolingOptions()
        .setConnectionsPerHost(HostDistance.LOCAL, 1, 12)
        .setConnectionsPerHost(HostDistance.REMOTE, 1, 12)
        .setMaxRequestsPerConnection(HostDistance.LOCAL, 1028)
        .setMaxRequestsPerConnection(HostDistance.REMOTE, 1028)
     )
     .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.QUORUM))

我正在尝试迁移此代码以开始使用 PhantomDSL。

我想构建一个 PhantomDSL CassandraConnection,其中包含上述所有选项。

我在这里查看了代码

https://github.com/outworkers/phantom/blob/develop/phantom-connectors/src/main/scala/com/outworkers/phantom/connectors/CassandraConnection.scala

尝试过

val phantomConnection = new CassandraConnection("foo", cluster, false)

我得到的错误是

[error] MyConnection.scala:37: type mismatch;
[error]  found   : com.datastax.driver.core.Cluster.Builder
[error]  required: com.outworkers.phantom.connectors.ClusterBuilder
[error]     (which expands to)  com.datastax.driver.core.Cluster.Builder => com.datastax.driver.core.Cluster.Builder
[error] Error occurred in an application involving default arguments.
[error]       new CassandraConnection("foo", cluster, false)
[error]                                       ^

只需使用本机 _.withClusterBuilder 方法即可实现您想要的效果。很抱歉回复晚了,我们会监控这个标签的问题,但出于某种原因,这从来没有出现在我们的任何收件箱中。

object Connector {
  val default: CassandraConnection = ContactPoint.local
    .withClusterBuilder(
      _.withCompression(ProtocolOptions.Compression.LZ4)
       .withCredentials("foo", "bar")
       .withPort(1111)
       .withProtocolVersion(ProtocolVersion.V4) 
    ).noHeartbeat().keySpace(
      KeySpace("phantom").ifNotExists().`with`(
        replication eqs SimpleStrategy.replication_factor(1)
      )
    )
}